Class: HebrewDate

Inherits:
Delegator
  • Object
show all
Extended by:
HebrewDateSupport::HolidayMethods::ClassMethods
Includes:
Comparable, HebrewDateSupport::HolidayMethods, HebrewDateSupport::ParshaMethods
Defined in:
lib/hebrew_date.rb

Overview

A class that represents a date in both the Gregorian and Hebrew calendars simultaneously. Note that you may call any Date methods on this class and it should respond accordingly.

Constant Summary collapse

HEBREW_EPOCH =
-1373429
HEBREW_MONTH_NAMES =
['Nissan', 'Iyar', 'Sivan', 'Tammuz', 'Av', 'Elul',
'Tishrei', 'Cheshvan', 'Kislev', 'Teves', 'Shvat',
'Adar', 'Adar II']

Constants included from HebrewDateSupport::HolidayMethods::ClassMethods

HebrewDateSupport::HolidayMethods::ClassMethods::HOLIDAYS

Constants included from HebrewDateSupport::ParshaMethods

HebrewDateSupport::ParshaMethods::MON_LONG, HebrewDateSupport::ParshaMethods::MON_LONG_LEAP, HebrewDateSupport::ParshaMethods::MON_LONG_LEAP_ISRAEL, HebrewDateSupport::ParshaMethods::MON_SHORT, HebrewDateSupport::ParshaMethods::MON_SHORT_LEAP, HebrewDateSupport::ParshaMethods::MON_SHORT_LEAP_ISRAEL, HebrewDateSupport::ParshaMethods::PARSHA_NAMES, HebrewDateSupport::ParshaMethods::SAT_LONG, HebrewDateSupport::ParshaMethods::SAT_LONG_LEAP, HebrewDateSupport::ParshaMethods::SAT_SHORT, HebrewDateSupport::ParshaMethods::SAT_SHORT_LEAP, HebrewDateSupport::ParshaMethods::THU_LONG, HebrewDateSupport::ParshaMethods::THU_LONG_LEAP, HebrewDateSupport::ParshaMethods::THU_NORMAL, HebrewDateSupport::ParshaMethods::THU_NORMAL_ISRAEL, HebrewDateSupport::ParshaMethods::THU_SHORT_LEAP

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HebrewDateSupport::HolidayMethods::ClassMethods

from_holiday

Methods included from HebrewDateSupport::HolidayMethods

#candle_lighting_day?, #first_day_yom_tov?, #havdala_day?, #holiday, #omer, #omer_to_s, #rosh_chodesh?, #second_day_yom_tov?

Methods included from HebrewDateSupport::ParshaMethods

#parsha

Constructor Details

#initialize(year_or_date_object = nil, month = nil, date = nil) ⇒ HebrewDate

Returns a new instance of HebrewDate.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hebrew_date.rb', line 65

def initialize(year_or_date_object=nil, month=nil, date=nil)
  @abs_date = 0
  skip_hebrew_date = false
  if year_or_date_object === true
    skip_hebrew_date = true
    year_or_date_object = nil
  end

  if month && date
    @year = year_or_date_object
    @month = month
    @date = date
  else
    date = year_or_date_object || Date.today
    @year = date.year
    @month = date.month
    @date = date.mday
  end
  @israeli = self.class.israeli
  @ashkenaz = self.class.ashkenaz
  @debug = self.class.debug
  set_date(@year, @month, @date) unless skip_hebrew_date
  super(Date.new)
end

Class Attribute Details

.ashkenazBoolean

Returns Whether to use Ashkenazi pronunciation.

Returns:

  • (Boolean)

    Whether to use Ashkenazi pronunciation.



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

def ashkenaz
  @ashkenaz
end

.debugObject



40
41
42
# File 'lib/hebrew_date.rb', line 40

def debug
  @debug
end

.israeliBoolean

Returns Whether to use Israeli parsha/holiday scheme.

Returns:

  • (Boolean)

    Whether to use Israeli parsha/holiday scheme.



33
34
35
# File 'lib/hebrew_date.rb', line 33

def israeli
  @israeli
end

.replace_saturdayBoolean

Returns If true, replaces “Saturday” or “Sat” in #strftime with “Shabbat” or “Shabbos” depending on the #ashkenaz value.

Returns:

  • (Boolean)

    If true, replaces “Saturday” or “Sat” in #strftime with “Shabbat” or “Shabbos” depending on the #ashkenaz value.



37
38
39
# File 'lib/hebrew_date.rb', line 37

def replace_saturday
  @replace_saturday
end

Instance Attribute Details

#abs_dateObject (readonly)



63
64
65
# File 'lib/hebrew_date.rb', line 63

def abs_date
  @abs_date
end

#dateInteger (readonly)

Returns the current Gregorian day of the month (1-31).

Returns:

  • (Integer)

    the current Gregorian day of the month (1-31).



51
52
53
# File 'lib/hebrew_date.rb', line 51

def date
  @date
end

#hebrew_dateInteger (readonly)

Returns the current Hebrew date (1-30).

Returns:

  • (Integer)

    the current Hebrew date (1-30).



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

def hebrew_date
  @hebrew_date
end

#hebrew_monthInteger (readonly)

Returns the current Hebrew month (1-13).

Returns:

  • (Integer)

    the current Hebrew month (1-13).



57
58
59
# File 'lib/hebrew_date.rb', line 57

def hebrew_month
  @hebrew_month
end

#hebrew_yearInteger (readonly)

Returns the current Hebrew year (e.g. 5775).

Returns:

  • (Integer)

    the current Hebrew year (e.g. 5775).



54
55
56
# File 'lib/hebrew_date.rb', line 54

def hebrew_year
  @hebrew_year
end

#monthInteger (readonly)

Returns the current Gregorian month (1-12).

Returns:

  • (Integer)

    the current Gregorian month (1-12).



48
49
50
# File 'lib/hebrew_date.rb', line 48

def month
  @month
end

#yearInteger (readonly)

Returns the current Gregorian year (e.g. 2008).

Returns:

  • (Integer)

    the current Gregorian year (e.g. 2008).



45
46
47
# File 'lib/hebrew_date.rb', line 45

def year
  @year
end

Class Method Details

.day_name(day) ⇒ String

Get the name of the given day (1-7).

Parameters:

  • day (Integer)

Returns:

  • (String)


471
472
473
474
475
476
477
# File 'lib/hebrew_date.rb', line 471

def day_name(day)
  if self.replace_saturday && day == 7
    self.ashkenaz ? 'Shabbos' : 'Shabbat'
  else
    Date::DAYNAMES[day - 1]
  end
end

.hebrew_leap_year?(year) ⇒ Boolean

Is this a Hebrew leap year?

Parameters:

  • year (Integer)

    the Hebrew year.

Returns:

  • (Boolean)


457
458
459
# File 'lib/hebrew_date.rb', line 457

def hebrew_leap_year?(year)
  (((7 * year) + 1).remainder(19)) < 7
end

.hebrew_month_to_s(month, year = nil) ⇒ String

Get the name of the given Hebrew month.

Parameters:

  • month (Integer)
  • year (Integer) (defaults to: nil)

Returns:

  • (String)


361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/hebrew_date.rb', line 361

def self.hebrew_month_to_s(month, year=nil)
  year ||= self.new.hebrew_year
  if hebrew_leap_year?(year) && month == 12
    'Adar I'
  else
    name = HEBREW_MONTH_NAMES[month - 1]
    if name == 'Teves' && !self.ashkenaz
      name = 'Tevet'
    end
    name
  end
end

.last_month_of_hebrew_year(year) ⇒ Integer

The last month in the Hebrew year (12 or 13).

Parameters:

  • year (Integer)

    the Hebrew year.

Returns:

  • (Integer)


464
465
466
# File 'lib/hebrew_date.rb', line 464

def last_month_of_hebrew_year(year)
  hebrew_leap_year?(year) ? 13 : 12
end

.new_from_hebrew(year, month, date) ⇒ Object

Create a HebrewDate with initialized Hebrew date values.

Parameters:

  • year (Integer)

    the Hebrew year (e.g. 5774)

  • month (Integer)

    the Hebrew month (1-13)

  • date (Integer)

    the day of the Hebrew month (1-30)



167
168
169
170
171
172
173
174
# File 'lib/hebrew_date.rb', line 167

def self.new_from_hebrew(year, month, date)
  d = self.new(true)
  @year = 1
  @month = 1
  @date = 1
  d.set_hebrew_date(year, month, date, true)
  d
end

Instance Method Details

#+(other) ⇒ HebrewDate

Parameters:

  • other (Fixnum)

Returns:



115
116
117
118
119
120
121
# File 'lib/hebrew_date.rb', line 115

def +(other)
  date = self.clone
  (1..other).each do |i|
    date.forward
  end
  date
end

#-(other) ⇒ HebrewDate

Parameters:

  • other (Fixnum)

Returns:



125
126
127
128
129
130
131
# File 'lib/hebrew_date.rb', line 125

def -(other)
  date = self.clone
  (1..other).each do |i|
    date.back
  end
  date
end

#<=>(other) ⇒ Integer

Comparison operator.

Parameters:

Returns:

  • (Integer)

    -1, 0, or 1



136
137
138
# File 'lib/hebrew_date.rb', line 136

def <=>(other)
  self.to_date <=> other.to_date
end

#__getobj__Object



104
105
106
# File 'lib/hebrew_date.rb', line 104

def __getobj__
  self.to_date
end

#__setobj__(obj) ⇒ Object



109
110
111
# File 'lib/hebrew_date.rb', line 109

def __setobj__(obj)
  # do nothing
end

#backHebrewDate

Move back one day.

Returns:

  • (HebrewDate)

    the same HebrewDate object again, for chaining.



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/hebrew_date.rb', line 314

def back
  # Change Gregorian date
  if @date == 1
    if @month == 1
      @month = 12
      @year -= 1
    else
      @month -= 1
    end

    # change to last day of previous month
    @date = last_day_of_month
  else
    @date -= 1
  end

  # Change Hebrew date
  if @hebrew_date == 1
    if @hebrew_month == 1 # Nisan
      @hebrew_month = last_month_of_hebrew_year
    elsif @hebrew_month == 7 # Rosh Hashana
      @hebrew_year -= 1
      @hebrew_month -= 1
    else
      @hebrew_month -= 1
    end
    @hebrew_date = last_day_of_hebrew_month
  else
    @hebrew_date -= 1
  end

  # Change the absolute date
  @abs_date -= 1

  self
end

#dayInteger

Get the day of the week.

Returns:

  • (Integer)

    the day of the week, 1-7.



429
430
431
# File 'lib/hebrew_date.rb', line 429

def day
  to_date.strftime('%w').to_i + 1
end

#downto(min, &block) ⇒ Enumerator|self

This method is equivalent to step(min, -1){|date| …}.

Parameters:

  • min (Integer)

Returns:

  • (Enumerator|self)


150
151
152
153
# File 'lib/hebrew_date.rb', line 150

def downto(min, &block)
  return to_enum(:downto, min) unless block_given?
  self.step(min, -1, &block)
end

#forwardHebrewDate

Move forward one day.

Returns:

  • (HebrewDate)

    the same HebrewDate object again, for chaining.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/hebrew_date.rb', line 243

def forward
  # Change Gregorian date
  if @date == last_day_of_month
    if @month == 12
      # last day of year
      @year += 1
      @month = 1
      @date = 1
    else
      @month += 1
      @date = 1
    end
  else
    # if not last day of month
    @date += 1
  end

  # Change Hebrew date
  if @hebrew_date == last_day_of_hebrew_month
    if @hebrew_month == 6
      # last day of Elul (i.e. last day of Hebrew year)
      @hebrew_year += 1
      @hebrew_month += 1
      @hebrew_date = 1
    elsif @hebrew_month == last_month_of_hebrew_year
      # last day of Adar or Adar II
      @hebrew_month = 1
      @hebrew_date = 1
    else
      @hebrew_month += 1
      @hebrew_date = 1
    end
  else
    # not last day of month
    @hebrew_date += 1
  end

  # increment the absolute date
  @abs_date += 1

  self
end

#hebrew_leap_year?Boolean

Is this a Hebrew leap year?

Returns:

  • (Boolean)


489
490
491
# File 'lib/hebrew_date.rb', line 489

def hebrew_leap_year?
  self.class.hebrew_leap_year?(@hebrew_year)
end

#hebrew_month_to_sString

Get the name of the current Hebrew month.

Returns:

  • (String)


353
354
355
# File 'lib/hebrew_date.rb', line 353

def hebrew_month_to_s
  self.class.hebrew_month_to_s(@hebrew_month, @hebrew_year)
end

#inspectObject



91
92
93
# File 'lib/hebrew_date.rb', line 91

def inspect
  strftime('*Y-*-m-*-d (%Y-%-m-%-d)')
end

#last_day_of_hebrew_month(month = nil) ⇒ Integer

Last day of the current Hebrew month.

Parameters:

  • month (Integer) (defaults to: nil)

    Used internally.

Returns:

  • (Integer)


496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/hebrew_date.rb', line 496

def last_day_of_hebrew_month(month=nil)
  month ||= @hebrew_month
  if month == 2 ||
    month == 4 ||
    month == 6 ||
    (month == 8 && !_cheshvan_long?) ||
    (month == 9 && _kislev_short?) ||
    month == 10 ||
    (month == 12 && !hebrew_leap_year?) ||
    month == 13
    29
  else
    30
  end
end

#last_day_of_month(month = nil) ⇒ Integer

The last day of the Gregorian month.

Parameters:

  • month (Integer) (defaults to: nil)

    Used internally.

Returns:

  • (Integer)


436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/hebrew_date.rb', line 436

def last_day_of_month(month=nil)
  month ||= @month
  case month
    when 2
      if (((@year.remainder(4)) == 0) && ((@year.remainder(100)) != 0)) ||
        ((@year.remainder(400)) == 0)
        29
      else
        28
      end
    when 4, 6, 9, 11
      30
    else
      31
  end
end

#last_month_of_hebrew_yearInteger

The last month in this Hebrew year (12 or 13).

Returns:

  • (Integer)


483
484
485
# File 'lib/hebrew_date.rb', line 483

def last_month_of_hebrew_year
  self.class.last_month_of_hebrew_year(@hebrew_year)
end

#nextHebrewDate Also known as: succ

This does not modify the current date, but creates a new one.

Returns:



288
289
290
# File 'lib/hebrew_date.rb', line 288

def next
  self + 1
end

#next_hebrew_monthHebrewDate

This does not modify the current date, but creates a new one.

Returns:



294
295
296
297
298
299
# File 'lib/hebrew_date.rb', line 294

def next_hebrew_month
  current_month = self.hebrew_month
  date = self.clone
  date.forward while date.hebrew_month == current_month
  date
end

#prev_hebrew_monthHebrewDate

This does not modify the current date, but creates a new one.

Returns:



303
304
305
306
307
308
# File 'lib/hebrew_date.rb', line 303

def prev_hebrew_month
  current_month = self.hebrew_month
  date = self.clone
  date.back while date.hebrew_month == current_month
  date
end

#set_date(year, month, date) ⇒ void

This method returns an undefined value.

Set the Gregorian date of this HebrewDate.

Parameters:

  • year (Integer)

    the Gregorian year (e.g. 2004)

  • month (Integer)

    the Gregorian month (1-12)

  • date (Integer)

    the Gregorian day of month (1-31)



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/hebrew_date.rb', line 181

def set_date(year, month, date)
  # precond should be 1->12 anyways, but just in case...
  return "Illegal value for year: #{year}" if year < 0
  return "Illegal value for month: #{month}" if month > 12 || month < 0
  return "Illegal value for date: #{date}" if date < 0

  # make sure date is a valid date for the given month.
  # If not, set to last day of month
  last_day = last_day_of_month(month)
  date = last_day if date > last_day

  @year = year
  @month = month
  @date = date

# init the Hebrew date
  @abs_date = _date_to_abs_date(@year, @month, @date)
  _abs_date_to_hebrew_date!
end

#set_hebrew_date(year, month, date, auto_set = false) ⇒ void

This method returns an undefined value.

Set the Hebrew date.

Parameters:

  • year (Integer)

    the Hebrew year (e.g. 2008)

  • month (Integer)

    the Hebrew month (1-13)

  • date (Integer)

    the Hebrew day of month (1-30)

  • auto_set (Boolean) (defaults to: false)

    Used internally.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/hebrew_date.rb', line 207

def set_hebrew_date(year, month, date, auto_set=false)
  if auto_set
    @hebrew_year = year
    @hebrew_month = month
    @hebrew_date = date
  end
  return "Illegal value for Hebrew year: #{year}" if year < 0
  if month < 0 || month > last_month_of_hebrew_year
    return "Illegal value for Hebrew month: #{month}"
  end
  return "Illegal value for Hebrew date: #{date}" if date < 0

  # make sure date is valid for this month;
  # otherwise, set to last day of month
  last_day = last_day_of_hebrew_month(month)
  date = last_day if date > last_day

  @hebrew_year = year
  @hebrew_month = month
  @hebrew_date = date

	# reset gregorian date
  @abs_date =
    _hebrew_date_to_abs_date(@hebrew_year, @hebrew_month, @hebrew_date)
  _abs_date_to_date!

end

#shabbos?Boolean Also known as: shabbat?

Returns:

  • (Boolean)


375
376
377
# File 'lib/hebrew_date.rb', line 375

def shabbos?
  self.saturday?
end

#step(limit, step = 1, &block) ⇒ Object

Iterates evaluation of the given block, which takes a HebrewDate object. The limit should be a Date or HebrewDate object.



142
143
144
145
# File 'lib/hebrew_date.rb', line 142

def step(limit, step=1, &block)
  return to_enum(:step, limit, step) unless block_given?
  (self..limit).step(step) { |date| yield(date) }
end

#strftime(format) ⇒ String

Extend the Date strftime method by replacing Hebrew fields. You can denote Hebrew fields by using the * flag. There is one extra flag, %.b, which adds a period after the 3-letter month name except for May. Also note that ::replace_saturday will replace the %A, %^A, %a and %^a flags with Shabbat/Shabbos. Supported flags are:

* *Y - Hebrew year
* *m - Hebrew month, zero-padded
* *_m - Hebrew month, blank-padded
* *-m - Hebrew month, no-padded
* *B - Hebrew month, full name
* *^B - Hebrew month, full name uppercase
* *b - Hebrew month, 3 letters
* *^b - Hebrew month, 3 letters uppercase
* *h - same as %*b
* *d - Hebrew day of month, zero-padded
* *-d - Hebrew day of month, no-padded
* *e - Hebrew day of month, blank-padded
* %.b - Gregorian month, 3 letter name, followed by a period, except for
   May

Parameters:

  • format (String)

Returns:

  • (String)


403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/hebrew_date.rb', line 403

def strftime(format)
  format = format.gsub('*Y', @hebrew_year.to_s)
    .gsub('*m', @hebrew_month.to_s.rjust(2, '0'))
    .gsub('*_m', @hebrew_month.to_s.rjust(2, ' '))
    .gsub('*-m', @hebrew_month.to_s)
    .gsub('*B', hebrew_month_to_s)
    .gsub('*^B', hebrew_month_to_s.upcase)
    .gsub('*b', hebrew_month_to_s[0, 3])
    .gsub('*^b', hebrew_month_to_s[0, 3].upcase)
    .gsub('*h', hebrew_month_to_s[0, 3])
    .gsub('*d', @hebrew_date.to_s.rjust(2, '0'))
    .gsub('*-d', @hebrew_date.to_s)
    .gsub('*e', @hebrew_date.to_s.rjust(2, ' '))
    .gsub('%.b', self.month == 5 ? '%b' : '%b.')
  if self.class.replace_saturday && self.day == 7
    shab_name = self.class.ashkenaz ? 'Shabbos' : 'Shabbat'
    format = format.gsub('%A', shab_name)
      .gsub('%^A', shab_name.upcase)
      .gsub('%a', shab_name[0..2])
      .gsub('%^a', shab_name[0..2].upcase)
  end
  super(format)
end

#to_dateDate

Return a Ruby Date corresponding to the Gregorian date.

Returns:

  • (Date)


237
238
239
# File 'lib/hebrew_date.rb', line 237

def to_date
  Date.new(@year, @month, @date)
end

#upto(max, &block) ⇒ Enumerator|self

This method is equivalent to step(max, 1){ |date| …}.

Parameters:

  • max (Integer)

Returns:

  • (Enumerator|self)


158
159
160
161
# File 'lib/hebrew_date.rb', line 158

def upto(max, &block)
  return to_enum(:upto, max) unless block_given?
  self.step(max, 1, &block)
end