Module: Hijri::Converter

Defined in:
lib/hijri/converter.rb

Class Method Summary collapse

Class Method Details

.absolute_to_greo(abs) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hijri/converter.rb', line 65

def absolute_to_greo(abs)
  # Computes the Gregorian date from the absolute date.
  # Search forward year by year from approximate year
  year = (abs / 366.0 + 0.5).to_i
  while abs >= greo_to_absolute(year + 1, 1, 1)
    year += 1
  end
  # Search forward month by month from January
  month = 1
  while abs > greo_to_absolute(year, month, last_day_of_gregorian_month(month, year))
    month += 1
  end
  day = abs - greo_to_absolute(year, month, 1) + 1

  return [year, month, day]
end

.absolute_to_hijri(abs) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/hijri/converter.rb', line 82

def absolute_to_hijri(abs)
  # Computes the Islamic date from the absolute date.
  if abs <= ISLAMIC_EPOCH
    # Date is pre-Islamic
    month = 0
    day = 0
    year = 0
  elsif

    # Search forward year by year from approximate year
    year = ((abs - ISLAMIC_EPOCH) / 355.0).to_i
    while abs >= hijri_to_absolute(year+1, 1, 1)
      year += 1
    end

    # Search forward month by month from Muharram
    month = 1
    while abs > hijri_to_absolute(year, month, last_day_of_islamic_month(month, year))
      month += 1
    end

    day = abs - hijri_to_absolute(year, month, 1) + 1
  end

  return [year, month, day]
end

.greo_to_absolute(year, month, day) ⇒ Object

Absolute Methods



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hijri/converter.rb', line 50

def greo_to_absolute(year, month, day)
  # Computes the absolute date from the Gregorian date.
  d = day
  (month - 1).downto(1) do |m|
    d += last_day_of_gregorian_month(m, year)
  end

  return (d +                      # days this year
          365 * (year - 1) +       # days in previous years ignoring leap days
          (year - 1) / 4.0 -       # Julian leap days before this year...
          (year - 1) / 100.0 +     # ...minus prior century years...
          (year - 1) / 400.0       # ...plus prior years divisible by 400
          ).to_i
end

.greo_to_hijri(greg) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/hijri/converter.rb', line 11

def self.greo_to_hijri greg
  results = absolute_to_hijri(greo_to_absolute(greg.year, greg.month, greg.day))
  if greg.is_a? ::DateTime
    results.push greg.hour, greg.minute, greg.second, greg.zone
  end
  results
end

.hijri_to_absolute(year, month, day) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/hijri/converter.rb', line 31

def hijri_to_absolute(year, month, day)
  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 + ISLAMIC_EPOCH).to_i
end

.hijri_to_greo(hijri) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/hijri/converter.rb', line 3

def self.hijri_to_greo hijri
  results = absolute_to_greo(hijri_to_absolute(hijri.year, hijri.month, hijri.day))
  if hijri.is_a? DateTime
    results.push hijri.hour, hijri.minute, hijri.second, hijri.zone
  end
  results
end

.islamic_leap_year?(year) ⇒ Boolean

Hijri Methods

Returns:

  • (Boolean)


22
23
24
# File 'lib/hijri/converter.rb', line 22

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

.last_day_of_gregorian_month(month, year) ⇒ Object

Gregorian Methods



41
42
43
44
45
46
47
# File 'lib/hijri/converter.rb', line 41

def last_day_of_gregorian_month(month, year)
  # Compute the last date of the month for the Gregorian calendar.
  if month == 2
    return 29 if (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
  end
  return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1]
end

.last_day_of_islamic_month(month, year) ⇒ Object



26
27
28
29
# File 'lib/hijri/converter.rb', line 26

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