class JalaliDate

Attributes

day[RW]
g_day[R]
g_month[R]
g_year[R]
hour[RW]
min[RW]
month[RW]
sec[RW]
year[RW]

Public Class Methods

leap?(y) click to toggle source

Accpets a four digit number as the jalaliyear and returns true if that particular year is a leap year in jalali calendar otherwise it returns false.

# File lib/jalalidate.rb, line 79
def leap?(y)
  [6,22,17,13,9,5,1,30].include?(y%33) ? true : false
end
new(*args) click to toggle source

Can be initialized in two ways:

  • First by feeding 3 arguments for Jalali Date, year,month and day.

  • The Second way to initializes is to pass a normal Ruby Date object, it'll be converted to jalali automatically.

Example:

jdate = JalaliDate.new(Date.today)
other_jdate = JalaliDate.new(1388,9,17)
# File lib/jalalidate.rb, line 34
def initialize *args
  if (args.size == 1) && (args.first.is_a?(Date))
    year,month,day = gregorian_to_jalali(args.first.year, args.first.month, args.first.day)
  elsif (args.size == 1) && (args.first.is_a?(Time) || args.first.is_a?(DateTime))
    year,month,day = gregorian_to_jalali(args.first.year, args.first.month, args.first.day)
    @hour = args.first.hour || 0
    @min  = args.first.min  || 0
    @sec  = args.first.sec  || 0
    @zone = args.first.zone || "UTC"
    @utc_offset = args.first.utc_offset  || 0
  else
    year,month,day,hour,min,sec,zone,utc_offset = args
  end

  raise ArgumentError, "invalid arguments or invalid Jalali date" unless self.class.valid?(year,month,day)
  @year  = year
  @month = month
  @day   = day
  @hour ||=  hour || 0
  @min  ||=  min  || 0
  @sec  ||=  sec  || 0
  @zone ||=  zone || "UTC"
  @utc_offset ||= utc_offset || 0
  @g_year, @g_month, @g_day = jalali_to_gregorian(year,month,day)
end
today() click to toggle source

Return a JalaliDate object representing today's date in calendar

# File lib/jalalidate.rb, line 63
def today
  JalaliDate.new(Date.today)
end
tomorrow() click to toggle source

Return a JalaliDate object representing tomorrow's date in calendar

# File lib/jalalidate.rb, line 73
def tomorrow
  JalaliDate.new(Date.today + 1)
end
valid?(y,m,d) click to toggle source

Accpets three numbers for year (4 digit), month and day in jalali calendar and checks if it's a valid date according to jalali calendar or not.

# File lib/jalalidate.rb, line 85
def valid?(y,m,d)
  ( y.class == Integer && y > 0 &&
    m.class == Integer && (1..12).include?(m) &&
    d.class == Integer &&
    (
      ((1..JDaysInMonth[m-1]).include?(d)) || (d == 30 && m == 12 && leap?(y))
    )
  ) ? true : false
end
yesterday() click to toggle source

Return a JalaliDate object representing yesterday's date in calendar

# File lib/jalalidate.rb, line 68
def yesterday
  JalaliDate.new(Date.today - 1)
end

Public Instance Methods

+(days) click to toggle source

Adds n days to the current JalaliDate object

# File lib/jalalidate.rb, line 125
def +(days)
  self.class.new( to_g + days )
end
-(days) click to toggle source

Subtracts n days from the current JalaliDate object

# File lib/jalalidate.rb, line 130
def -(days)
  self.class.new( to_g - days )
end
<<(months) click to toggle source

Move JalaliDate object backward by n months

# File lib/jalalidate.rb, line 160
def <<(months)
  self >> -months
end
<=>(other) click to toggle source

Compares two JalaliDate objects. acts like Date#<=>

# File lib/jalalidate.rb, line 146
def <=>(other)
  to_g <=> other.to_g
end
>>(months) click to toggle source

Move JalaliDate object forward by n months

# File lib/jalalidate.rb, line 151
def >>(months)
  y, m = (@year * 12 + (@month - 1) + months).divmod(12)
  m,   = (m + 1)                    .divmod(1)
  d = @day
  d -= 1 until self.class.valid?(y, m, d)
  self.class.new(y,m,d)
end
downto(min, &block) click to toggle source

Step backward one day at a time until we reach min (inclusive), yielding each date as we go. See upto for the example.

# File lib/jalalidate.rb, line 196
def downto(min, &block)
  step(min, -1, &block)
end
format(format_str = '%Y/%m/%d')
Alias for: strftime
inspect() click to toggle source

Return internal object state as a programmer-readable string.

# File lib/jalalidate.rb, line 120
def inspect
  "#<#{self.class}:#{self.object_id}, :year => #{@year}, :month => #{@month}, :day => #{@day} >"
end
jwday() click to toggle source

Get the jalali week day of this date. Saturday is day-of-week 0; Friday is day-of-week 6.

# File lib/jalalidate.rb, line 211
def jwday
  (to_g.wday + 1) % 7
end
leap?() click to toggle source

Is this a leap year?

# File lib/jalalidate.rb, line 201
def leap?
  self.class.leap?(@year)
end
next(n=1) click to toggle source

Return the next day for the current JalaliDate object

# File lib/jalalidate.rb, line 135
def next(n=1)
  self + n
end
Also aliased as: succ
previous(n=1) click to toggle source

Return the previous day for the current JalaliDate object

# File lib/jalalidate.rb, line 141
def previous(n=1)
  self - n
end
step(limit, step=1) { |da| ... } click to toggle source

Step the current date forward step days at a time (or backward, if step is negative) until we reach limit (inclusive), yielding the resultant date at each step.

Example:

jdate = JalaliDate.new(Date.today)
jdate.step(Date.today+10, 2) do |jd|
  puts jd.to_s
end
# File lib/jalalidate.rb, line 172
def step(limit, step=1)
  da = self
  op = %w(- <= >=)[step <=> 0]
  while da.__send__(op, limit)
    yield da
    da += step
  end
  self
end
strftime(format_str = '%Y/%m/%d') click to toggle source

Formats time according to the directives in the given format string. Any text not listed as a directive will be passed through to the output string.

Format meanings:

%a

The abbreviated weekday name (۳ش)

%A

The full weekday name (یکشنبه)

%b

The month name (اردیبهشت)

%B

The month name in pinglish (Ordibehesht)

%d

Day of the month (01..31)

%e

Day of the month (1..31)

%j

Day of the year (1..366)

%m

Month of the year (1..12)

%n

Month of the year (01..12)

%w

Day of the week (Sunday is 0, 0..6)

%x

Preferred representation for the date alone, no time in format YY/M/D

%y

Year without a century (00..99)

%Y

Year with century

%H

Hour of the day, 24-hour clock (00..23)

%I

Hour of the day, 12-hour clock (01..12)

%M

Minute of the hour (00..59)

%p

Meridian indicator (“بعد از ظهر” or “قبل از ظهر”)

%S

Second of the minute (00..60)

%X

Preferred representation for the time alone, no date

%Z

Time zone name

%%

Literal %'' character

Example:

d = JalaliDate.today
d.strftime("Printed on %Y/%m/%d")   #=> "Printed on 87/5/26
# File lib/jalalidate.rb, line 252
def strftime(format_str = '%Y/%m/%d')
  clean_fmt = format_str.gsub(/%{2}/, "SUBSTITUTION_MARKER").
    gsub(/%a/, PERSIAN_ABBR_WEEKDAY_NAMES[wday]).
    gsub(/%A/, PERSIAN_WEEKDAY_NAMES[wday]).
    gsub(/%b/, PERSIAN_MONTH_NAMES[@month]).
    gsub(/%B/, PERSIAN_MONTH_NAMES_PINGLISH[@month]).
    gsub(/%d/, ("%02d" % @day).to_s).
    gsub(/%e/, @day.to_s).
    gsub(/%m/, @month.to_s).
          gsub(/%n/, ("%02d" % @month).to_s).
    gsub(/%Y/, @year.to_s).
    gsub(/%y/, @year.to_s.slice(2,2)).
    gsub(/%j/, yday.to_s).
    gsub(/%H/, ("%02d" % @hour).to_s).
    gsub(/%I/, ("%02d" % ((@hour>=12) ? @hour-12 : @hour)).to_s).
    gsub(/%M/, ("%02d" % @min).to_s).
    gsub(/%S/, ("%02d" % @sec).to_s).
    gsub(/%p/, (@hour>=12 ? "بعد از ظهر" : "قبل از ظهر")).
    gsub(/%w/, wday.to_s).
    gsub(/%Z/, @zone).
    gsub(/%X/, [("%02d" % @hour),("%02d" % @min),("%02d" % @sec)].join(":")).
    gsub(/%x/, [@year.to_s.slice(2,2),@month,@day].join("/")).
    gsub(/#{"SUBSTITUTION_MARKER"}/, '%')
end
Also aliased as: format
succ(n=1)
Alias for: next
to_a() click to toggle source

Returns an array in a format like this: [y,m,d]

# File lib/jalalidate.rb, line 115
def to_a
  [@year,@month,@day]
end
to_g()
Alias for: to_gregorian
to_gregorian() click to toggle source

Converts a JalaiDate object to Ruby Date object

# File lib/jalalidate.rb, line 99
def to_gregorian
  Date.new(@g_year,@g_month,@g_day)
end
Also aliased as: to_g
to_hash() click to toggle source

Returns a hash in a format like this: {:year => @year, :month => @month, :day => @day}

# File lib/jalalidate.rb, line 110
def to_hash
  {:year => @year, :month => @month, :day => @day}
end
to_s() click to toggle source

Returns a string represtation of the JalaliDate object in format like this: y/m/d

# File lib/jalalidate.rb, line 105
def to_s
  [@year,@month,@day].join("/")
end
upto(max, &block) click to toggle source

Step forward one day at a time until we reach max (inclusive), yielding each date as we go.

Example:

jdate = JalaliDate.new(Date.today)
days_string = ""
jdate.upto(jdate+5) do |jd|
  days_string += jd.day.to_s
end
# File lib/jalalidate.rb, line 190
def upto(max, &block)
  step(max, +1, &block)
end
wday() click to toggle source

Get the week day of this date. Sunday is day-of-week 0; Saturday is day-of-week 6.

# File lib/jalalidate.rb, line 206
def wday
  to_g.wday
end
yday() click to toggle source

Get the day-of-the-year of this date. Farvardin 1 is day-of-the-year 1

# File lib/jalalidate.rb, line 217
def yday
  m = (@month-2 < 0) ? 0 : @month-2
  (@month==1) ? @day : @day + JDaysInMonth[0..m].inject(0) {|sum, n| sum + n }
end