Class: Zmanim::HebrewCalendar::JewishDate

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/zmanim/hebrew_calendar/jewish_date.rb

Direct Known Subclasses

JewishCalendar

Constant Summary collapse

MONTHS =
%i(nissan iyar sivan tammuz av elul tishrei cheshvan kislev teves shevat adar adar_ii)
RD =
Date.new(1,1,1, Date::GREGORIAN)
JEWISH_EPOCH =
-1373429     # 1 Tishrei of year 1 (Shnas Tohu)
# Note that while the Gregorian year will be calculated as -3760, the actual year is -3761BCE
# This discrepancy is due to the presence of a year 0 in the Ruby implementation
CHALAKIM_PER_MINUTE =

1 Tishrei of year 1 (Shnas Tohu) Note that while the Gregorian year will be calculated as -3760, the actual year is -3761BCE This discrepancy is due to the presence of a year 0 in the Ruby implementation

18
CHALAKIM_PER_HOUR =
CHALAKIM_PER_MINUTE * 60
CHALAKIM_PER_DAY =
CHALAKIM_PER_HOUR * 24
CHALAKIM_PER_MONTH =
(CHALAKIM_PER_DAY * 29.5).to_i + 793
CHALAKIM_MOLAD_TOHU =
CHALAKIM_PER_DAY + (CHALAKIM_PER_HOUR * 5) + 204
CHESHVAN_KISLEV_KEVIAH =
%i(chaseirim kesidran shelaimim)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ JewishDate

Returns a new instance of JewishDate.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 24

def initialize(*args)
  if args.size == 0
    reset_date!
  elsif args.size == 3
    set_jewish_date(*args)
  elsif args.size == 1 && args.first.is_a?(Date)
    self.date = args.first
  elsif args.size == 1 && args.first.is_a?(Numeric)
    set_from_molad(args.first)
  else
    raise ArgumentError
  end
end

Instance Attribute Details

#day_of_weekObject (readonly)

Returns the value of attribute day_of_week.



22
23
24
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 22

def day_of_week
  @day_of_week
end

#gregorian_dateObject

Returns the value of attribute gregorian_date.



22
23
24
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 22

def gregorian_date
  @gregorian_date
end

#jewish_dayObject

Returns the value of attribute jewish_day.



22
23
24
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 22

def jewish_day
  @jewish_day
end

#jewish_monthObject

Returns the value of attribute jewish_month.



22
23
24
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 22

def jewish_month
  @jewish_month
end

#jewish_yearObject

Returns the value of attribute jewish_year.



22
23
24
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 22

def jewish_year
  @jewish_year
end

#molad_chalakimObject (readonly)

Returns the value of attribute molad_chalakim.



21
22
23
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 21

def molad_chalakim
  @molad_chalakim
end

#molad_hoursObject (readonly)

Returns the value of attribute molad_hours.



21
22
23
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 21

def molad_hours
  @molad_hours
end

#molad_minutesObject (readonly)

Returns the value of attribute molad_minutes.



21
22
23
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 21

def molad_minutes
  @molad_minutes
end

Class Method Details

.from_date(date) ⇒ Object



46
47
48
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 46

def self.from_date(date)
  new(date)
end

.from_jewish_date(year, month, day) ⇒ Object



42
43
44
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 42

def self.from_jewish_date(year, month, day)
  new(year, month, day)
end

.from_molad(molad) ⇒ Object



38
39
40
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 38

def self.from_molad(molad)
  new(molad)
end

Instance Method Details

#+(addend) ⇒ Object

Raises:

  • (ArgumentError)


135
136
137
138
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 135

def +(addend)
  raise ArgumentError unless addend.is_a?(Numeric)
  self.dup.forward!(addend)
end

#-(subtrahend) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 140

def -(subtrahend)
  if subtrahend.is_a?(Numeric)
    self.dup.back!(subtrahend)
  elsif subtrahend.is_a?(JewishDate)
    absolute_date - subtrahend.send(:absolute_date)
  elsif subtrahend.respond_to?(:to_date)
    gregorian_date - subtrahend.to_date
  else
    raise ArgumentError
  end
end

#<=>(other) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 152

def <=>(other)
  if other.is_a?(JewishDate)
    gregorian_date <=> other.gregorian_date
  else
    gregorian_date <=> other
  end
end

#back!(decrement = 1) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 109

def back!(decrement=1)
  return forward!(-decrement) if decrement < 0
  if decrement > 500
    self.date = gregorian_date - decrement
    return self
  end
  days_of_year = sorted_days_in_jewish_year
  y, m, d = jewish_year, jewish_month, jewish_day
  d -= decrement
  while d <= 0 do
    m -= 1
    m = days_of_year.length if m == 0
    if m == 6
      y -= 1
      days_of_year = sorted_days_in_jewish_year(y)
    end
    days_in_month = days_of_year.assoc(m)[1]
    d += days_in_month
  end
  @gregorian_date -= decrement
  @absolute_date -= decrement
  reset_day_of_week
  @jewish_year, @jewish_month, @jewish_day = y, m, d
  self
end

#cheshvan_kislev_kviah(year = jewish_year) ⇒ Object



274
275
276
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 274

def cheshvan_kislev_kviah(year=jewish_year)
  CHESHVAN_KISLEV_KEVIAH[(days_in_jewish_year(year) % 10) - 3]
end

#cheshvan_long?(year = jewish_year) ⇒ Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 258

def cheshvan_long?(year=jewish_year)
  days_in_jewish_year(year) % 10 == 5
end

#cheshvan_short?(year = jewish_year) ⇒ Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 262

def cheshvan_short?(year=jewish_year)
  !cheshvan_long?(year)
end

#date=(date) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 55

def date=(date)
  @gregorian_date = date.gregorian
  @absolute_date = gregorian_date_to_abs_date(gregorian_date)
  reset_day_of_week
  @molad_hours = @molad_minutes = @molad_chalakim = 0
  @jewish_year, @jewish_month, @jewish_day = jewish_date_from_abs_date(@absolute_date)
end

#day_number_of_jewish_year(year = jewish_year, month = jewish_month, day = jewish_day) ⇒ Object



249
250
251
252
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 249

def day_number_of_jewish_year(year=jewish_year, month=jewish_month, day=jewish_day)
  month_index = month_number_from_tishrei(year, month) - 1
  day + sorted_months_in_jewish_year(year)[0...month_index].sum{|m| days_in_jewish_month(m, year)}
end

#days_in_gregorian_month(month = gregorian_month, year = gregorian_year) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 188

def days_in_gregorian_month(month=gregorian_month, year=gregorian_year)
  case month
    when 2
      gregorian_leap_year?(year) ? 29 : 28
    when 4, 6, 9, 11
      30
    else
      31
  end
end

#days_in_gregorian_year(year = gregorian_year) ⇒ Object



184
185
186
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 184

def days_in_gregorian_year(year=gregorian_year)
  gregorian_leap_year?(year) ? 366 : 365
end

#days_in_jewish_month(month = jewish_month, year = jewish_year) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 237

def days_in_jewish_month(month=jewish_month, year=jewish_year)
  m = jewish_month_name(month)
  if %i(iyar tammuz elul teves adar_ii).include?(m) ||
      (m == :cheshvan && cheshvan_short?(year)) ||
      (m == :kislev && kislev_short?(year)) ||
      (m == :adar && !jewish_leap_year?(year))
    29
  else
    30
  end
end

#days_in_jewish_year(year = jewish_year) ⇒ Object



215
216
217
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 215

def days_in_jewish_year(year=jewish_year)
  jewish_calendar_elapsed_days(year+1) - jewish_calendar_elapsed_days(year)
end

#forward!(increment = 1) ⇒ Object



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/zmanim/hebrew_calendar/jewish_date.rb', line 84

def forward!(increment=1)
  return back!(-increment) if increment < 0
  if increment > 500
    self.date = gregorian_date + increment
    return self
  end
  days_of_year = sorted_days_in_jewish_year
  y, m, d = jewish_year, jewish_month, jewish_day
  d += increment
  while d > (days_in_month = days_of_year.assoc(m)[1]) do
    d -= days_in_month
    m += 1
    m = 1 if m > days_of_year.length
    if m == 7
      y += 1
      days_of_year = sorted_days_in_jewish_year(y)
    end
  end
  @gregorian_date += increment
  @absolute_date += increment
  reset_day_of_week
  @jewish_year, @jewish_month, @jewish_day = y, m, d
  self
end

#gregorian_dayObject



180
181
182
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 180

def gregorian_day
  gregorian_date.day
end

#gregorian_day=(day) ⇒ Object



176
177
178
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 176

def gregorian_day=(day)
  set_gregorian_date(gregorian_year, gregorian_month, day)
end

#gregorian_leap_year?(year = gregorian_year) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 199

def gregorian_leap_year?(year=gregorian_year)
  (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
end

#gregorian_monthObject



172
173
174
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 172

def gregorian_month
  gregorian_date.month
end

#gregorian_month=(month) ⇒ Object



168
169
170
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 168

def gregorian_month=(month)
  set_gregorian_date(gregorian_year, month, gregorian_day)
end

#gregorian_yearObject



164
165
166
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 164

def gregorian_year
  gregorian_date.year
end

#gregorian_year=(year) ⇒ Object



160
161
162
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 160

def gregorian_year=(year)
  set_gregorian_date(year, gregorian_month, gregorian_day)
end

#jewish_leap_year?(year = jewish_year) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 254

def jewish_leap_year?(year=jewish_year)
  ((7 * year) + 1) % 19 < 7
end

#jewish_month_from_name(month_name) ⇒ Object



283
284
285
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 283

def jewish_month_from_name(month_name)
  MONTHS.index(month_name) + 1
end

#jewish_month_name(month = jewish_month) ⇒ Object



287
288
289
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 287

def jewish_month_name(month=jewish_month)
  MONTHS[month - 1]
end

#kislev_long?(year = jewish_year) ⇒ Boolean

Returns:

  • (Boolean)


266
267
268
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 266

def kislev_long?(year=jewish_year)
  !kislev_short?(year)
end

#kislev_short?(year = jewish_year) ⇒ Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 270

def kislev_short?(year=jewish_year)
  days_in_jewish_year(year) % 10 == 3
end

#molad(month = jewish_month, year = jewish_year) ⇒ Object

Returns a new JewishDate as the molad for given month



279
280
281
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 279

def molad(month=jewish_month, year=jewish_year)
  self.class.from_molad(chalakim_since_molad_tohu(year, month))
end

#months_in_jewish_year(year = jewish_year) ⇒ Object



219
220
221
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 219

def months_in_jewish_year(year=jewish_year)
  jewish_leap_year?(year) ? 13 : 12
end

#reset_date!Object



50
51
52
53
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 50

def reset_date!
  self.date = Date.today
  self
end

#set_gregorian_date(year, month, day) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 63

def set_gregorian_date(year, month, day)
  raise ArgumentError if year < -3760 || month < 1 || month > 12 || day < 1 || day > 31
  if day > (max_days = days_in_gregorian_month(month, year))
    day = max_days
  end
  self.date = Date.new(year, month, day, Date::GREGORIAN)
end

#set_jewish_date(year, month, day, hours = 0, minutes = 0, chalakim = 0) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 71

def set_jewish_date(year, month, day, hours=0, minutes=0, chalakim=0)
  raise ArgumentError if year < 1 || month < 1 || month > 13 || day < 1 || day > 30 ||
      hours < 0 || hours > 23 || minutes < 0 || minutes > 59  || chalakim < 0 || chalakim > 17
  if month > (max_months = months_in_jewish_year(year))
    month = max_months
  end
  if day > (max_days = days_in_jewish_month(month, year))
    day = max_days
  end
  self.date = gregorian_date_from_abs_date(jewish_date_to_abs_date(year, month, day))
  @molad_hours, @molad_minutes, @molad_chalakim = hours, minutes, chalakim
end

#sorted_days_in_jewish_year(year = jewish_year) ⇒ Object

Returns the number of days in each jewish month for a given jewish year in chronological order

sorted_days_in_jewish_year(5779)
=> [[7, 30], [8, 29], [9, 30], [10, 29], [11, 30], [12, 30], [13, 29], [1, 30], [2, 29], [3, 30], [4, 29], [5, 30], [6, 29]]


233
234
235
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 233

def sorted_days_in_jewish_year(year=jewish_year)
  sorted_months_in_jewish_year(year).map{|month| [month, days_in_jewish_month(month, year)]}
end

#sorted_months_in_jewish_year(year = jewish_year) ⇒ Object

Returns the list of jewish months for a given jewish year in chronological order

sorted_months_in_jewish_year(5779)
=> [7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6]


226
227
228
# File 'lib/zmanim/hebrew_calendar/jewish_date.rb', line 226

def sorted_months_in_jewish_year(year=jewish_year)
  (1..months_in_jewish_year(year)).sort_by{|y| [y >= 7 ? 0 : 1, y]}
end