Module: ManuallyInvokedBuild::HolidayCalculator

Defined in:
lib/manually_invoked_build/holiday_calculator.rb

Class Method Summary collapse

Class Method Details

.christmas_for(year) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/manually_invoked_build/holiday_calculator.rb', line 93

def christmas_for(year)
  xmas = Date.new(year,12,25)
  if xmas.saturday?
    Date.new(year,12,24)
  elsif xmas.sunday?
    Date.new(year,12,26)
  else
    xmas
  end
end

.good_friday_list(csv_text) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/manually_invoked_build/holiday_calculator.rb', line 61

def good_friday_list(csv_text)
  CSV
    .parse(csv_text, headers: true, col_sep: "\t")
    .map do |line|
      day, month = line["Good Friday"].split('.')
      Date.new(line['Year'].to_i, month.to_i, day.to_i)
    end
end

.independence_day_for(year) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/manually_invoked_build/holiday_calculator.rb', line 74

def independence_day_for(year)
  id = Date.new(year,7,4)
  if id.saturday?
    Date.new(year,7,3)
  elsif id.sunday?
    Date.new(year,7,5)
  else
    id
  end
end

.labor_day_for(year) ⇒ Object



85
86
87
# File 'lib/manually_invoked_build/holiday_calculator.rb', line 85

def labor_day_for(year)
  (1..6).map { |day| Date.new(year,9,day) }.find { |date| date.monday? }
end

.market_dates(year_range, good_friday_dates = []) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/manually_invoked_build/holiday_calculator.rb', line 9

def market_dates(year_range, good_friday_dates=[])
  holidays = year_range.flat_map do |year|
    [
      new_years_day_for(year),
      mlk_day_for(year),
      presidents_day_for(year),
      memorial_day_for(year),
      independence_day_for(year),
      labor_day_for(year),
      thanksgiving_day_for(year),
      christmas_for(year)
    ]
  end

  date_range = (Date.new(year_range.min, 1, 1)..Date.new(year_range.max, 1, 1))
  date_range.reject do |date|
    date.saturday? ||
    date.sunday? ||
    good_friday_dates.include?(date) ||
    holidays.include?(date)
  end
end

.memorial_day_for(year) ⇒ Object



70
71
72
# File 'lib/manually_invoked_build/holiday_calculator.rb', line 70

def memorial_day_for(year)
  (25..31).map { |day| Date.new(year,5,day) }.find { |date| date.monday? }
end

.mlk_day_for(year) ⇒ Object



53
54
55
# File 'lib/manually_invoked_build/holiday_calculator.rb', line 53

def mlk_day_for(year)
  (15..21).map { |day| Date.new(year,1,day) }.find { |date| date.monday? }
end

.new_years_day_for(year) ⇒ Object

  • New Year’s Day (January 1st, or first Monday of January if on weekend)

    * Martin Luther King Day (third Monday of January)
    * Washington's Birthday (third Monday of Feburary)
    * Good Friday (Determined by http://www.maa.clell.de/StarDate/publ_holidays.html)
    * Memorial Day (last Monday of May)
    * Independence Day (July 4, or July 3rd if on Saturday, July 5th if on Sunday)
    * Labor Day (First Monday of September)
    * Thanksgiving Day (Fourth Thursday of November)
    * Christmas Day (December 25th, or December 24th if on Saturday, December 26th if on Sunday)
    


42
43
44
45
46
47
48
49
50
51
# File 'lib/manually_invoked_build/holiday_calculator.rb', line 42

def new_years_day_for(year)
  nyd = Date.new(year,1,1)
  if nyd.saturday?
    Date.new(year,1,3)
  elsif nyd.sunday?
    Date.new(year,1,2)
  else
    nyd
  end
end

.presidents_day_for(year) ⇒ Object



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

def presidents_day_for(year)
  (15..21).map { |day| Date.new(year,2,day) }.find { |date| date.monday? }
end

.thanksgiving_day_for(year) ⇒ Object



89
90
91
# File 'lib/manually_invoked_build/holiday_calculator.rb', line 89

def thanksgiving_day_for(year)
  (22..29).map { |day| Date.new(year,11,day) }.find { |date| date.thursday? }
end