Class: Holidays::DateCalculator::WeekendModifier

Inherits:
Object
  • Object
show all
Defined in:
lib/holidays/date_calculator/weekend_modifier.rb

Instance Method Summary collapse

Instance Method Details

#to_monday_if_sunday(date) ⇒ Object

Move date to Monday if it occurs on a Sunday. Does not modify the date if it is not a Sunday. Used as a callback function.



15
16
17
18
# File 'lib/holidays/date_calculator/weekend_modifier.rb', line 15

def to_monday_if_sunday(date)
  return date unless date.wday == 0
  to_next_weekday(date)
end

#to_monday_if_weekend(date) ⇒ Object

Move date to Monday if it occurs on a Saturday on Sunday. Does not modify date if it is not a weekend. Used as a callback function.



7
8
9
10
# File 'lib/holidays/date_calculator/weekend_modifier.rb', line 7

def to_monday_if_weekend(date)
  return date unless date.wday == 6 || date.wday == 0
  to_next_weekday(date)
end

#to_next_weekday(date) ⇒ Object

Finds the next weekday. For example, if a ‘Friday’ date is received it will return the following Monday. If Sunday then return Monday, if Saturday return Monday, if Tuesday return Wednesday, etc.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/holidays/date_calculator/weekend_modifier.rb', line 74

def to_next_weekday(date)
  case date.wday
  when 6
    date += 2
  when 5
    date += 3
  else
    date += 1
  end

  date
end

#to_previous_day_if_leap_year(date) ⇒ Object



87
88
89
90
# File 'lib/holidays/date_calculator/weekend_modifier.rb', line 87

def to_previous_day_if_leap_year(date)
  date -= 1 if ::Date.gregorian_leap?(date.year)
  date
end

#to_the_second_weekday_after(date) ⇒ Object



24
25
26
# File 'lib/holidays/date_calculator/weekend_modifier.rb', line 24

def to_the_second_weekday_after(date)
  to_monday_if_sunday(to_the_weekday_after(date) + 1)
end

#to_the_weekday_after(date) ⇒ Object



20
21
22
# File 'lib/holidays/date_calculator/weekend_modifier.rb', line 20

def to_the_weekday_after(date)
  to_monday_if_sunday(to_monday_if_sunday(date) + 1)
end

#to_tuesday_if_sunday_or_monday_if_saturday(date) ⇒ Object

if Christmas falls on a Saturday, move it to the next Monday (Boxing Day will be Sunday and potentially Tuesday) if Christmas falls on a Sunday, move it to the next Tuesday (Boxing Day will go on Monday)

if Boxing Day falls on a Saturday, move it to the next Monday (Christmas will go on Friday) if Boxing Day falls on a Sunday, move it to the next Tuesday (Christmas will go on Saturday & Monday)



45
46
47
48
# File 'lib/holidays/date_calculator/weekend_modifier.rb', line 45

def to_tuesday_if_sunday_or_monday_if_saturday(date)
  date += 2 if [0, 6].include?(date.wday)
  date
end

#to_weekday_if_boxing_weekend(date) ⇒ Object

Move Boxing Day if it falls on a weekend, leaving room for Christmas. Used as a callback function.



30
31
32
33
34
35
36
37
38
# File 'lib/holidays/date_calculator/weekend_modifier.rb', line 30

def to_weekday_if_boxing_weekend(date)
  if date.wday == 6 || date.wday == 0
    date += 2
  elsif date.wday == 1 # https://github.com/holidays/holidays/issues/27
    date += 1
  end

  date
end

#to_weekday_if_boxing_weekend_from_year(year) ⇒ Object

Call to_weekday_if_boxing_weekend but first get date based on year Used as a callback function.



58
59
60
# File 'lib/holidays/date_calculator/weekend_modifier.rb', line 58

def to_weekday_if_boxing_weekend_from_year(year)
  to_tuesday_if_sunday_or_monday_if_saturday(Date.civil(year, 12, 26))
end

#to_weekday_if_boxing_weekend_from_year_or_to_tuesday_if_monday(year) ⇒ Object

Call to_weekday_if_boxing_weekend but first get date based on year Used as a callback function.



52
53
54
# File 'lib/holidays/date_calculator/weekend_modifier.rb', line 52

def to_weekday_if_boxing_weekend_from_year_or_to_tuesday_if_monday(year)
  to_weekday_if_boxing_weekend(Date.civil(year, 12, 26))
end

#to_weekday_if_weekend(date) ⇒ Object

Move date to Monday if it occurs on a Sunday or to Friday if it occurs on a Saturday. Used as a callback function.



65
66
67
68
69
# File 'lib/holidays/date_calculator/weekend_modifier.rb', line 65

def to_weekday_if_weekend(date)
  date += 1 if date.wday == 0
  date -= 1 if date.wday == 6
  date
end