Class: HebrewDate
- Inherits:
-
Delegator
- Object
- Delegator
- HebrewDate
- 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::Parshiot
HebrewDateSupport::Parshiot::MON_LONG, HebrewDateSupport::Parshiot::MON_LONG_LEAP, HebrewDateSupport::Parshiot::MON_LONG_LEAP_ISRAEL, HebrewDateSupport::Parshiot::MON_SHORT, HebrewDateSupport::Parshiot::MON_SHORT_LEAP, HebrewDateSupport::Parshiot::MON_SHORT_LEAP_ISRAEL, HebrewDateSupport::Parshiot::PARSHA_NAMES, HebrewDateSupport::Parshiot::SAT_LONG, HebrewDateSupport::Parshiot::SAT_LONG_LEAP, HebrewDateSupport::Parshiot::SAT_SHORT, HebrewDateSupport::Parshiot::SAT_SHORT_LEAP, HebrewDateSupport::Parshiot::THU_LONG, HebrewDateSupport::Parshiot::THU_LONG_LEAP, HebrewDateSupport::Parshiot::THU_NORMAL, HebrewDateSupport::Parshiot::THU_NORMAL_ISRAEL, HebrewDateSupport::Parshiot::THU_SHORT_LEAP
Class Attribute Summary collapse
-
.ashkenaz ⇒ Boolean
Whether to use Ashkenazi pronunciation.
- .debug ⇒ Object
-
.israeli ⇒ Boolean
Whether to use Israeli parsha/holiday scheme.
Instance Attribute Summary collapse
- #abs_date ⇒ Object readonly
-
#date ⇒ Integer
readonly
The current Gregorian day of the month (1-31).
-
#hebrew_date ⇒ Integer
readonly
The current Hebrew date (1-30).
-
#hebrew_month ⇒ Integer
readonly
The current Hebrew month (1-13).
-
#hebrew_year ⇒ Integer
readonly
The current Hebrew year (e.g. 5775).
-
#month ⇒ Integer
readonly
The current Gregorian month (1-12).
-
#year ⇒ Integer
readonly
The current Gregorian year (e.g. 2008).
Class Method Summary collapse
-
.new_from_hebrew(year, month, date) ⇒ Object
Create a HebrewDate with initialized Hebrew date values.
Instance Method Summary collapse
- #__getobj__ ⇒ Object
- #__setobj__(obj) ⇒ Object
-
#back ⇒ void
Move back one day.
-
#clone ⇒ HebrewDate
Get a clone of this object.
-
#day ⇒ Integer
Get the day of the week.
-
#forward ⇒ void
Move forward one day.
-
#hebrew_leap_year?(year = nil) ⇒ Boolean
Is this a Hebrew leap year?.
-
#hebrew_month_to_s ⇒ String
Get the name of the current Hebrew month.
-
#initialize(year_or_date_object = nil, month = nil, date = nil) ⇒ HebrewDate
constructor
A new instance of HebrewDate.
- #inspect ⇒ Object
-
#last_day_of_hebrew_month(month = nil) ⇒ Integer
Last day of the current Hebrew month.
-
#last_day_of_month(month = nil) ⇒ Integer
The last day of the Gregorian month.
-
#last_month_of_hebrew_year ⇒ Integer
The last month in this Hebrew year (12 or 13).
-
#set_date(year, month, date) ⇒ void
Set the Gregorian date of this HebrewDate.
-
#set_hebrew_date(year, month, date) ⇒ void
Set the Hebrew date.
-
#strftime(format) ⇒ String
Extend the Date strftime method by replacing Hebrew fields.
-
#to_date ⇒ Date
Return a Ruby Date corresponding to the Gregorian date.
Methods included from HebrewDateSupport::Holidays
#candle_lighting_day?, #first_day_yom_tov?, #havdala_day?, #holiday, #omer, #omer_to_s, #rosh_chodesh?
Methods included from HebrewDateSupport::Parshiot
Constructor Details
#initialize(year_or_date_object = nil, month = nil, date = nil) ⇒ HebrewDate
Returns a new instance of HebrewDate.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/hebrew_date.rb', line 59 def initialize(year_or_date_object=nil, month=nil, date=nil) @abs_date = 0 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) super(Date.new) end |
Class Attribute Details
.ashkenaz ⇒ Boolean
Returns Whether to use Ashkenazi pronunciation.
28 29 30 |
# File 'lib/hebrew_date.rb', line 28 def ashkenaz @ashkenaz end |
.debug ⇒ Object
34 35 36 |
# File 'lib/hebrew_date.rb', line 34 def debug @debug end |
.israeli ⇒ Boolean
Returns Whether to use Israeli parsha/holiday scheme.
31 32 33 |
# File 'lib/hebrew_date.rb', line 31 def israeli @israeli end |
Instance Attribute Details
#abs_date ⇒ Object (readonly)
57 58 59 |
# File 'lib/hebrew_date.rb', line 57 def abs_date @abs_date end |
#date ⇒ Integer (readonly)
Returns the current Gregorian day of the month (1-31).
45 46 47 |
# File 'lib/hebrew_date.rb', line 45 def date @date end |
#hebrew_date ⇒ Integer (readonly)
Returns the current Hebrew date (1-30).
54 55 56 |
# File 'lib/hebrew_date.rb', line 54 def hebrew_date @hebrew_date end |
#hebrew_month ⇒ Integer (readonly)
Returns the current Hebrew month (1-13).
51 52 53 |
# File 'lib/hebrew_date.rb', line 51 def hebrew_month @hebrew_month end |
#hebrew_year ⇒ Integer (readonly)
Returns the current Hebrew year (e.g. 5775).
48 49 50 |
# File 'lib/hebrew_date.rb', line 48 def hebrew_year @hebrew_year end |
#month ⇒ Integer (readonly)
Returns the current Gregorian month (1-12).
42 43 44 |
# File 'lib/hebrew_date.rb', line 42 def month @month end |
#year ⇒ Integer (readonly)
Returns the current Gregorian year (e.g. 2008).
39 40 41 |
# File 'lib/hebrew_date.rb', line 39 def year @year end |
Class Method Details
.new_from_hebrew(year, month, date) ⇒ Object
Create a HebrewDate with initialized Hebrew date values.
104 105 106 107 108 |
# File 'lib/hebrew_date.rb', line 104 def self.new_from_hebrew(year, month, date) d = self.new d.set_hebrew_date(year, month, date) d end |
Instance Method Details
#__getobj__ ⇒ Object
85 86 87 |
# File 'lib/hebrew_date.rb', line 85 def __getobj__ self.to_date end |
#__setobj__(obj) ⇒ Object
90 91 92 |
# File 'lib/hebrew_date.rb', line 90 def __setobj__(obj) # do nothing end |
#back ⇒ void
This method returns an undefined value.
Move back one day.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/hebrew_date.rb', line 214 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 end |
#clone ⇒ HebrewDate
Get a clone of this object.
96 97 98 |
# File 'lib/hebrew_date.rb', line 96 def clone HebrewDate.new(self.to_date) end |
#day ⇒ Integer
Get the day of the week.
292 293 294 |
# File 'lib/hebrew_date.rb', line 292 def day to_date.strftime('%w').to_i + 1 end |
#forward ⇒ void
This method returns an undefined value.
Move forward one day.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/hebrew_date.rb', line 171 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 end |
#hebrew_leap_year?(year = nil) ⇒ Boolean
Is this a Hebrew leap year?
319 320 321 322 |
# File 'lib/hebrew_date.rb', line 319 def hebrew_leap_year?(year=nil) year ||= @hebrew_year (((7 * year) + 1).remainder(19)) < 7 end |
#hebrew_month_to_s ⇒ String
Get the name of the current Hebrew month.
251 252 253 254 255 256 257 |
# File 'lib/hebrew_date.rb', line 251 def hebrew_month_to_s name = HEBREW_MONTH_NAMES[@hebrew_month - 1] if name == 'Teves' && !@ashkenaz name = 'Tevet' end name end |
#inspect ⇒ Object
80 81 82 |
# File 'lib/hebrew_date.rb', line 80 def inspect strftime('*Y *-m *-d') end |
#last_day_of_hebrew_month(month = nil) ⇒ Integer
Last day of the current Hebrew month.
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/hebrew_date.rb', line 333 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.
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/hebrew_date.rb', line 299 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_year ⇒ Integer
The last month in this Hebrew year (12 or 13).
326 327 328 |
# File 'lib/hebrew_date.rb', line 326 def last_month_of_hebrew_year hebrew_leap_year? ? 13 : 12 end |
#set_date(year, month, date) ⇒ void
This method returns an undefined value.
Set the Gregorian date of this HebrewDate.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/hebrew_date.rb', line 115 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) ⇒ void
This method returns an undefined value.
Set the Hebrew date.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/hebrew_date.rb', line 140 def set_hebrew_date(year, month, date) 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 |
#strftime(format) ⇒ String
Extend the Date strftime method by replacing Hebrew fields. You can denote Hebrew fields by using the * flag. 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
275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/hebrew_date.rb', line 275 def strftime(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, ' ')) end |
#to_date ⇒ Date
Return a Ruby Date corresponding to the Gregorian date.
165 166 167 |
# File 'lib/hebrew_date.rb', line 165 def to_date Date.new(@year, @month, @date) end |