Class: Hijri::Date

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/hijri/date.rb

Direct Known Subclasses

DateTime

Constant Summary collapse

MONTHNAMES_EN =
%w(Muharram Safar Rabia-Awwal Rabia-Thani Jumaada-Awal Jumaada-Thani Rajab Sha'ban Ramadan Shawwal Dhul-Qi'dah Dhul-Hijjah)
DAYNAMES =
%w(as-Sabt al-Ahad al-Ithnayn ath-Thalaathaa al-Arba'aa' al-Khamis al-Jumu'ah)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year = 1, month = 1, day = 1) ⇒ Date

Returns a new instance of Date.



17
18
19
# File 'lib/hijri/date.rb', line 17

def initialize(year=1, month=1, day=1)
   @year, @month, @day = year, month, day
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



6
7
8
# File 'lib/hijri/date.rb', line 6

def day
  @day
end

#monthObject

Returns the value of attribute month.



6
7
8
# File 'lib/hijri/date.rb', line 6

def month
  @month
end

#yearObject

Returns the value of attribute year.



6
7
8
# File 'lib/hijri/date.rb', line 6

def year
  @year
end

Class Method Details

.todayObject



11
12
13
14
# File 'lib/hijri/date.rb', line 11

def today
  date = ::Date.today
  date.to_hijri
end

Instance Method Details

#<=>(date) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hijri/date.rb', line 34

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

#islamic_leap_year?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/hijri/date.rb', line 21

def islamic_leap_year?
  return (((((11 * self.year) + 14) % 30) < 11) ? true : false)
end

#last_day_of_islamic_monthObject



25
26
27
28
# File 'lib/hijri/date.rb', line 25

def last_day_of_islamic_month
  # Last day in month during year on the Islamic calendar.
  return ((self.month % 2 == 1) || (self.month == 12 && islamic_leap_year?) ? 30 : 29)
end

#to_absObject



46
47
48
49
50
51
52
53
# File 'lib/hijri/date.rb', line 46

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

#to_greoObject



55
56
57
# File 'lib/hijri/date.rb', line 55

def to_greo
  ::Date.new *Converter.hijri_to_greo(self)
end

#to_hijriObject

Just to have a consistent Interface.



60
61
62
# File 'lib/hijri/date.rb', line 60

def to_hijri
  self
end

#to_sObject



30
31
32
# File 'lib/hijri/date.rb', line 30

def to_s
  "#{@year}-#{sprintf('%02d', @month)}-#{sprintf('%02d', @day)}"
end