class Hijri::Date

Constants

ABBR_DAYNAMES
ABBR_MONTHNAMES
DAYNAMES
MONTHNAMES

Attributes

day[R]
mday[R]
mon[R]
month[R]
year[R]

Public Class Methods

_parse(str, comp=true) click to toggle source
# File lib/hijri/format.rb, line 1026
def self._parse(str, comp=true)
  str = str.dup

  e = Format::Bag.new

  e._comp = comp

  str.gsub!(/[^-+',.\/:@[:alnum:]\[\]]+/, ' ')

  _parse_time(str, e) # || _parse_beat(str, e)
  _parse_day(str, e)

  _parse_eu(str, e)     ||
  _parse_us(str, e)     ||
  _parse_iso(str, e)    ||
  _parse_jis(str, e)    ||
  _parse_vms(str, e)    ||
  _parse_sla(str, e)    ||
  _parse_dot(str, e)    ||
  _parse_iso2(str, e)   ||
  _parse_year(str, e)   ||
  _parse_mon(str, e)    ||
  _parse_mday(str, e)   ||
  _parse_ddd(str, e)

  if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/i, ' ')
    if e.year
e.year = -e.year + 1
    end
  end

  if str.sub!(/\A\s*(\d{1,2})\s*\z/, ' ')
    if e.hour && !e.mday
v = $1.to_i
if (1..31) === v
  e.mday = v
end
    end
    if e.mday && !e.hour
v = $1.to_i
if (0..24) === v
  e.hour = v
end
    end
  end

  if e._comp
    if e.cwyear
if e.cwyear >= 0 && e.cwyear <= 99
  e.cwyear += if e.cwyear >= 69
        then 1900 else 2000 end
end
    end
    if e.year
if e.year >= 0 && e.year <= 99
  e.year += if e.year >= 69
      then 1900 else 2000 end
end
    end
  end

  e.offset ||= zone_to_diff(e.zone) if e.zone

  e.to_hash
end
_strptime(str, fmt='%F') click to toggle source
# File lib/hijri/format.rb, line 574
def self._strptime(str, fmt='%F')
  str = str.dup
  e = Format::Bag.new
  return unless _strptime_i(str, fmt, e)

  if e._cent
    if e.cwyear
e.cwyear += e._cent * 100
    end
    if e.year
e.  year += e._cent * 100
    end
  end

  if e._merid
    if e.hour
e.hour %= 12
e.hour += e._merid
    end
  end

  unless str.empty?
    e.leftover = str
  end

  e.to_hash
end
new(year=1, month=1, day=1) click to toggle source
# File lib/hijri/date.rb, line 35
def initialize(year=1, month=1, day=1)
  if valid_date?(year, month, day)
    @year, @month, @day = year, month, day
  else
    raise ArgumentError, "Invalid Date"
  end
end
today() click to toggle source
# File lib/hijri/date.rb, line 29
def today
  date = ::Date.today
  date.to_hijri
end

Private Class Methods

s3e(e, y, m, d, bc=false) click to toggle source
# File lib/hijri/format.rb, line 602
def self.s3e(e, y, m, d, bc=false)
  unless String === m
    m = m.to_s
  end

  if y && m && !d
    y, m, d = d, y, m
  end

  if y == nil
    if d && d.size > 2
y = d
d = nil
    end
    if d && d[0,1] == "'"
y = d
d = nil
    end
  end

  if y
    y.scan(/(\d+)(.+)?/)
    if $2
y, d = d, $1
    end
  end

  if m
    if m[0,1] == "'" || m.size > 2
y, m, d = m, d, y # us -> be
    end
  end

  if d
    if d[0,1] == "'" || d.size > 2
y, d = d, y
    end
  end

  if y
    y =~ /([-+])?(\d+)/
    if $1 || $2.size > 2
c = false
    end
    iy = $&.to_i
    if bc
iy = -iy + 1
    end
    e.year = iy
  end

  if m
    m =~ /\d+/
    e.mon = $&.to_i
  end

  if d
    d =~ /\d+/
    e.mday = $&.to_i
  end

  if c != nil
    e._comp = c
  end

end

Public Instance Methods

<=>(date) click to toggle source
# File lib/hijri/date.rb, line 55
def <=>(date)
  # Make sure the date is a Hijri::Date instance
  date = date.to_hijri
  if self.to_s == date.to_s
    return 0
  elsif @year > date.year ||
       (@year == date.year && @month > date.month) ||
       (@year == date.year && @month == date.month && @day > date.day)
    return 1
  else
    return -1
  end
end
abs()
Alias for: to_abs
asctime() click to toggle source

alias_method :format, :strftime

# File lib/hijri/format.rb, line 343
def asctime() strftime('%c') end
Also aliased as: ctime
change(kargs) click to toggle source
# File lib/hijri/date.rb, line 43
def change(kargs)
  # Remove nil values
  kargs.reject!{|k,v| v.nil?}
  @year  = kargs.fetch :year , year
  @month = kargs.fetch :month, month
  @day   = kargs.fetch :day  , day
end
ctime()
Alias for: asctime
cweek(week_start=0)
Alias for: weeknum
iso8601() click to toggle source
# File lib/hijri/format.rb, line 347
def iso8601() strftime('%F') end
jisx0301() click to toggle source
# File lib/hijri/format.rb, line 359
def jisx0301
  if jd < 2405160
    iso8601
  else
    case jd
    when 2405160...2419614
g = 'M%02d' % (year - 1867)
    when 2419614...2424875
g = 'T%02d' % (year - 1911)
    when 2424875...2447535
g = 'S%02d' % (year - 1925)
    else
g = 'H%02d' % (year - 1988)
    end
    g + strftime('.%m.%d')
  end
end
rfc2822() click to toggle source
# File lib/hijri/format.rb, line 353
def rfc2822() strftime('%a, %-d %b %Y %T %z') end
Also aliased as: rfc822
rfc3339() click to toggle source
# File lib/hijri/format.rb, line 349
def rfc3339() iso8601 end
rfc822()
Alias for: rfc2822
strftime(fmt='%F') click to toggle source
# File lib/hijri/format.rb, line 217
  def strftime(fmt='%F')
    fmt.gsub(/%([-_0^#]+)?(\d+)?([EO]?(?::{1,3}z|.))/m) do
    f = {}
    m = $&
    s, w, c = $1, $2, $3
    if s
      s.scan(/./) do |k|
        case k
        when '-'; f[:p] = '-'
        when '_'; f[:p] = "\s"
        when '0'; f[:p] = '0'
        when '^'; f[:u] = true
        when '#'; f[:x] = true
        end
      end
    end
    if w
      f[:w] = w.to_i
    end
    case c
    when 'A'; emit_ad(DAYNAMES[wday], 0, f)
    when 'a'; emit_ad(ABBR_DAYNAMES[wday], 0, f)
    when 'B'; emit_ad(MONTHNAMES[mon], 0, f)
    when 'b'; emit_ad(ABBR_MONTHNAMES[mon], 0, f)
    when 'C', 'EC'; emit_sn((year / 100).floor, 2, f)
    when 'c', 'Ec'; emit_a(strftime('%a %b %e %H:%M:%S %Y'), 0, f)
    when 'D'; emit_a(strftime('%m/%d/%y'), 0, f)
    when 'd', 'Od'; emit_n(mday, 2, f)
    when 'e', 'Oe'; emit_a(mday, 2, f)
    when 'F'
      if m == '%F'
        format('%.4d-%02d-%02d', year, mon, mday) # 4p
      else
        emit_a(strftime('%Y-%m-%d'), 0, f)
      end
    when 'G'; emit_sn(cwyear, 4, f)
    when 'g'; emit_n(cwyear % 100, 2, f)
    when 'H', 'OH'; emit_n(hour, 2, f)
    when 'h'; emit_ad(strftime('%b'), 0, f)
    when 'I', 'OI'; emit_n((hour % 12).nonzero? || 12, 2, f)
    when 'j'; emit_n(yday, 3, f)
    when 'k'; emit_a(hour, 2, f)
    when 'L'
      f[:p] = nil
      w = f[:w] || 3
      u = 10**w
      emit_n((sec_fraction * u).floor, w, f)
    when 'l'; emit_a((hour % 12).nonzero? || 12, 2, f)
    when 'M', 'OM'; emit_n(min, 2, f)
    when 'm', 'Om'; emit_n(mon, 2, f)
    when 'N'
      f[:p] = nil
      w = f[:w] || 9
      u = 10**w
      emit_n((sec_fraction * u).floor, w, f)
    when 'n'; emit_a("\n", 0, f)
    when 'P'; emit_ad(strftime('%p').downcase, 0, f)
    when 'p'; emit_au(if hour < 12 then 'AM' else 'PM' end, 0, f)
  when 'Q'
    s = ((ajd - UNIX_EPOCH_IN_AJD) / MILLISECONDS_IN_DAY).round
    emit_sn(s, 1, f)
  when 'R'; emit_a(strftime('%H:%M'), 0, f)
  when 'r'; emit_a(strftime('%I:%M:%S %p'), 0, f)
  when 'S', 'OS'; emit_n(sec, 2, f)
  when 's'
    s = ((ajd - UNIX_EPOCH_IN_AJD) / SECONDS_IN_DAY).round
    emit_sn(s, 1, f)
  when 'T'
    if m == '%T'
      format('%02d:%02d:%02d', hour, min, sec) # 4p
    else
      emit_a(strftime('%H:%M:%S'), 0, f)
    end
  when 't'; emit_a("\t", 0, f)
  when 'U', 'W', 'OU', 'OW'
    emit_n(if c[-1,1] == 'U' then wnum0 else wnum1 end, 2, f)
  when 'u', 'Ou'; emit_n(cwday, 1, f)
  when 'V', 'OV'; emit_n(cweek, 2, f)
  when 'v'; emit_a(strftime('%e-%b-%Y'), 0, f)
  when 'w', 'Ow'; emit_n(wday, 1, f)
  when 'X', 'EX'; emit_a(strftime('%H:%M:%S'), 0, f)
  when 'x', 'Ex'; emit_a(strftime('%m/%d/%y'), 0, f)
  when 'Y', 'EY'; emit_sn(year, 4, f)
  when 'y', 'Ey', 'Oy'; emit_n(year % 100, 2, f)
  when 'Z'; emit_au(strftime('%:z'), 0, f)
  when /\A(:{0,3})z/
    t = $1.size
    sign = if offset < 0 then -1 else +1 end
      fr = offset.abs
      ss = fr.div(SECONDS_IN_DAY) # 4p
      hh, ss = ss.divmod(3600)
      mm, ss = ss.divmod(60)
      if t == 3
        if    ss.nonzero? then t =  2
        elsif mm.nonzero? then t =  1
        else                   t = -1
        end
      end
      case t
      when -1
        tail = []
        sep = ''
      when 0
        f[:w] -= 2 if f[:w]
        tail = ['%02d' % mm]
        sep = ''
      when 1
        f[:w] -= 3 if f[:w]
        tail = ['%02d' % mm]
        sep = ':'
      when 2
        f[:w] -= 6 if f[:w]
        tail = ['%02d' % mm, '%02d' % ss]
        sep = ':'
      end
      ([emit_z(sign * hh, 2, f)] + tail).join(sep)
    when '%'; emit_a('%', 0, f)
    when '+'; emit_a(strftime('%a %b %e %H:%M:%S %Z %Y'), 0, f)
    else
      m
    end
  end
end
to_abs() click to toggle source
# File lib/hijri/date.rb, line 69
def to_abs
  month_days = 29 * (month - 1) # days on this year
  nonleap_year_days  = 354 * (year - 1)
  leap_year_days = (3 + (11 * year)) / 30.0
  this_year  = (month / 2.0).to_i

  return (day + month_days + this_year + nonleap_year_days + leap_year_days + Hijri::ISLAMIC_EPOCH).to_i
end
Also aliased as: abs
to_greo(adjust=0) click to toggle source
# File lib/hijri/date.rb, line 79
def to_greo(adjust=0)
  ::Date.new(*Converter.hijri_to_greo(self)) + adjust
end
to_hijri() click to toggle source

Just to have a consistent Interface.

# File lib/hijri/date.rb, line 84
def to_hijri
  self
end
to_s() click to toggle source
# File lib/hijri/date.rb, line 51
def to_s
  format('%.4d-%02d-%02d', year, month, day)
end
valid_date?(year, month, day) click to toggle source
# File lib/hijri/date.rb, line 88
def valid_date?(year, month, day)
  return false unless (1..INFINITY).cover?(year)
  return false unless (1..12).cover?(month)
  return false unless (1..30).cover?(day)
  return true
end
wday() click to toggle source
# File lib/hijri/date.rb, line 99
def wday
  (((year * AVERAGE_YEARS_DAYS) + yday) % 7).floor
end
weeknum(week_start=0) click to toggle source
# File lib/hijri/date.rb, line 103
def weeknum(week_start=0)
  yday / 7
end
Also aliased as: cweek
yday() click to toggle source
# File lib/hijri/date.rb, line 95
def yday
  (((month - 1) * AVERAGE_MONTH_DAYS) + day).floor
end