Module: Hijri::Converter

Defined in:
lib/hijri/converter.rb

Class Method Summary collapse

Class Method Details

.absolute_to_greo(abs) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hijri/converter.rb', line 76

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
    break if month == 12
  end
  day = abs - greo_to_absolute(year, month, 1) + 1
  day = adjust_day(year, month, day)

  return [year, month, day]
end

.absolute_to_hijri(abs) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hijri/converter.rb', line 96

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

.adjust_day(year, month, day) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/hijri/converter.rb', line 50

def adjust_day(year, month, day)
  last_day = last_day_of_gregorian_month(month, year)
  if last_day > day
    day
  else
    last_day
  end
end

.greo_to_absolute(year, month, day) ⇒ Object

Absolute Methods



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hijri/converter.rb', line 60

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



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

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



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

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
10
# 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)


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

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

.last_day_of_gregorian_month(month, year) ⇒ Object

Gregorian Methods



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

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



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

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