Class: Date
Overview
Extending Ruby’s Date class with the Holidays gem
The Holidays gem automatically extends Ruby’s Date class and gives you access to three new methods: holiday?, #holidays and #calculate_mday.
Examples
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'...}]
Constant Summary
Constants included from Holidays
Holidays::DAY_SYMBOLS, Holidays::DEFINITION_PATH, Holidays::MONTH_LENGTHS, Holidays::VERSION, Holidays::WEEKS
Class Method Summary collapse
-
.calculate_mday(year, month, week, wday) ⇒ Object
Calculate day of the month based on the week number and the day of the week.
Instance Method Summary collapse
-
#holiday?(*options) ⇒ Boolean
Check if the current date is a holiday.
-
#holidays(*options) ⇒ Object
Get holidays on the current date.
Methods included from Holidays
available, between, ca_victoria_day, ch_ge_jeune_genevois, ch_gl_naefelser_fahrt, ch_vd_lundi_du_jeune_federal, closest_monday, de_buss_und_bettag, easter, fi_juhannusaatto, fi_juhannuspaiva, fi_pyhainpaiva, full_week?, g20_day_2014_only, hobart_show_day, ie_st_stephens_day, is_sumardagurinn_fyrsti, jp_citizons_holiday, jp_national_culture_day, jp_substitute_holiday, jp_vernal_equinox_day, load_all, load_custom, merge_defs, next_week, on, orthodox_easter, parse_definition_files_and_return_source, pl_trzech_kroli, pl_trzech_kroli_informal, previous_friday, qld_labour_day_may, qld_labour_day_october, qld_queens_bday_october, regions, se_alla_helgons_dag, se_midsommardagen, to_monday_if_sunday, to_monday_if_weekend, to_weekday_if_boxing_weekend, to_weekday_if_weekend, us_inauguration_day
Class Method Details
.calculate_mday(year, month, week, wday) ⇒ Object
Calculate day of the month based on the week number and the day of the week.
Parameters
year-
Integer.
month-
Integer from 1-12.
week-
One of
:first,:second,:third,:fourth,:fifthor:last. wday-
Day of the week as an integer from 0 (Sunday) to 6 (Saturday) or as a symbol (e.g.
:monday).
Returns an integer.
Examples
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
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
# File 'lib/holidays.rb', line 717 def self.calculate_mday(year, month, week, wday) raise ArgumentError, "Week parameter must be one of Holidays::WEEKS (provided #{week})." unless WEEKS.include?(week) or WEEKS.has_value?(week) unless wday.kind_of?(Numeric) and wday.between?(0,6) or DAY_SYMBOLS.index(wday) raise ArgumentError, "Wday parameter must be an integer between 0 and 6 or one of Date::DAY_SYMBOLS." end week = WEEKS[week] if week.kind_of?(Symbol) wday = DAY_SYMBOLS.index(wday) if wday.kind_of?(Symbol) # :first, :second, :third, :fourth or :fifth if week > 0 return ((week - 1) * 7) + 1 + ((wday - Date.civil(year, month,(week-1)*7 + 1).wday) % 7) end days = MONTH_LENGTHS[month-1] days = 29 if month == 2 and Date.leap?(year) return days - ((Date.civil(year, month, days).wday - wday + 7) % 7) - (7 * (week.abs - 1)) end |
Instance Method Details
#holiday?(*options) ⇒ Boolean
Check if the current date is a holiday.
Returns true or false.
Date.civil('2008-01-01').holiday?(:ca)
=> true
685 686 687 688 |
# File 'lib/holidays.rb', line 685 def holiday?(*) holidays = self.holidays() holidays && !holidays.empty? end |
#holidays(*options) ⇒ Object
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.
675 676 677 |
# File 'lib/holidays.rb', line 675 def holidays(*) Holidays.on(self, ) end |