| Class | Date |
| In: |
lib/holidays.rb
|
| Parent: | Object |
The Holidays gem automatically extends Ruby‘s Date class and gives you access to three new methods: holiday?, holidays and calculate_mday.
Lookup Canada Day in the :ca region
Date.civil(2008,7,1).holiday?(:ca) => true
Lookup Canada Day in the :fr region
Date.civil(2008,7,1).holiday?(:fr) => false
Lookup holidays on North America in January 1.
Date.civil(2008,1,1).holidays(:ca, :mx, :us, :informal, :observed)
=> [{:name => 'New Year\'s Day'...}]
Calculate day of the month based on the week number and the day of the week.
Returns an integer.
First Monday of January, 2008:
Date.calculate_mday(2008, 1, :first, :monday) => 7
Third Thursday of December, 2008:
Date.calculate_mday(2008, 12, :third, :thursday) => 18
Last Monday of January, 2008:
Date.calculate_mday(2008, 1, :last, 1) => 28
# File lib/holidays.rb, line 383
383: def self.calculate_mday(year, month, week, wday)
384: raise ArgumentError, "Week parameter must be one of Holidays::WEEKS (provided #{week})." unless WEEKS.include?(week) or WEEKS.has_value?(week)
385:
386: unless wday.kind_of?(Numeric) and wday.between?(0,6) or DAY_SYMBOLS.index(wday)
387: raise ArgumentError, "Wday parameter must be an integer between 0 and 6 or one of Date::DAY_SYMBOLS."
388: end
389:
390: week = WEEKS[week] if week.kind_of?(Symbol)
391: wday = DAY_SYMBOLS.index(wday) if wday.kind_of?(Symbol)
392:
393: # :first, :second, :third, :fourth or :fifth
394: if week > 0
395: return ((week - 1) * 7) + 1 + ((7 + wday - Date.civil(year, month,(week-1)*7 + 1).wday) % 7)
396: end
397:
398: days = MONTH_LENGTHS[month-1]
399:
400: days = 29 if month == 1 and Date.civil(year,1,1).leap?
401:
402: return days - ((Date.civil(year, month, days).wday - wday + 7) % 7)
403: end
Check if the current date is a holiday.
Returns true or false.
Date.civil('2008-01-01').holiday?(:ca)
=> true
# File lib/holidays.rb, line 351
351: def holiday?(*options)
352: holidays = self.holidays(options)
353: holidays && !holidays.empty?
354: end
Get holidays on the current date.
Returns an array of hashes or nil. See Holidays#between for options and the output format.
Date.civil('2008-01-01').holidays(:ca_)
=> [{:name => 'New Year\'s Day',...}]
Also available via Holidays#on.
# File lib/holidays.rb, line 341
341: def holidays(*options)
342: Holidays.on(self, options)
343: end