Module: HolidayCo

Defined in:
lib/holiday_co/holiday_calculator.rb,
lib/holiday_co.rb,
lib/holiday_co/cache.rb,
lib/holiday_co/utils.rb,
lib/holiday_co/errors.rb,
lib/holiday_co/version.rb,
lib/holiday_co/models/day.rb,
lib/holiday_co/models/year.rb,
lib/holiday_co/calculate_holidays/fixed.rb,
lib/holiday_co/calculate_holidays/pascua.rb,
lib/holiday_co/calculate_holidays/movable.rb

Overview

Dates are in YYYY-MM-DD format

Defined Under Namespace

Modules: CalculateHolidays Classes: Cache, Day, HolidayCalculator, Year, YearDataNotAvailableError

Constant Summary collapse

UTC_OFFSET =

Colombian time zone is 5 hours behind UTC.

"-05:00".freeze
AVAILABLE_YEARS =

Colombian Holidays started in 1983

(1983..9999).freeze
VERSION =
"2.0.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cache_enabled=(value) ⇒ Object (writeonly)

Sets the attribute cache_enabled

Parameters:

  • value

    the value to set the attribute cache_enabled to.



44
45
46
# File 'lib/holiday_co/cache.rb', line 44

def cache_enabled=(value)
  @cache_enabled = value
end

.cache_sizeObject

Returns the value of attribute cache_size.



45
46
47
# File 'lib/holiday_co/cache.rb', line 45

def cache_size
  @cache_size
end

Class Method Details

.adjust_day_for_pascua(day, month, march_22_offset, days_to_sunday, lunar_cycle_position) ⇒ Object



78
79
80
81
82
83
# File 'lib/holiday_co/utils.rb', line 78

def self.adjust_day_for_pascua(day, month, march_22_offset, days_to_sunday, lunar_cycle_position)
  return day unless exception_date?(day, month)
  return day unless pascua_correction?(day, march_22_offset, days_to_sunday, lunar_cycle_position)

  day -= 7
end

.cacheObject



60
61
62
# File 'lib/holiday_co/cache.rb', line 60

def cache
  @cache ||= Cache.new(max_size: cache_size)
end

.cache_enabled?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/holiday_co/cache.rb', line 51

def cache_enabled?
  @cache_enabled
end

.calculate_pascua_day_per_year(year) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/holiday_co/utils.rb', line 31

def self.calculate_pascua_day_per_year(year)
  # The "Día de Pascua" (aka Domingo de Resurrección) plays a crucial role in determining
  # 5 of the 18 holidays, but its calculation is unique. For more details, check out:
  # https://blogs.elespectador.com/actualidad/algoritmo-calcular-las-fechas-la-semana-santa-ano
  # This calculation is based on an astronomical algorithm that takes into account the lunar cycle,
  # leap years, and the position of weekdays, which is used to calculate the exact date of
  # pascua each year

  lunar_cycle_position = year % 19 # a: Position in the lunar cycle
  leap_year_indicator = year % 4  # b: Leap year indicator
  week_day_position = year % 7    # c: Day of the week

  century = year / 100            # d: Century of the year
  leap_year_adjustment = (13 + 8 * century) / 25  # e: Adjustment for leap years in centuries
  century_quarters = century / 4  # m: Adjustment for century quarters

  lunar_adjustment = (15 - leap_year_adjustment + century - century_quarters) % 30  # n: Lunar adjustment for Easter date
  weekday_adjustment = (4 + century - century_quarters) % 7  # p: Adjustment for weekday of March 22

  march_22_offset = (19 * lunar_cycle_position + lunar_adjustment) % 30  # q: Lunar cycle position and adjustment

  final_weekday_adjustment = (
    (2 * leap_year_indicator) +
    (4 * week_day_position) +
    (6 * march_22_offset) +
    weekday_adjustment
  ) % 7  # r: Final weekday adjustment

  pascua_offset = march_22_offset + final_weekday_adjustment
  day, month = calculate_pascua_offset(pascua_offset)
  day = adjust_day_for_pascua(day, month, march_22_offset, final_weekday_adjustment, lunar_cycle_position)

  generate_pascua_date(year, month, day)
end

.calculate_pascua_offset(pascua_offset) ⇒ Object



66
67
68
# File 'lib/holiday_co/utils.rb', line 66

def self.calculate_pascua_offset(pascua_offset)
  pascua_offset <= 9 ? [pascua_offset + 22, "03"] : [pascua_offset - 9, "04"]
end

.clear_cache!Object



64
65
66
# File 'lib/holiday_co/cache.rb', line 64

def clear_cache!
  cache.clear!
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (HolidayCo)

    the object that the method was called on



47
48
49
# File 'lib/holiday_co/cache.rb', line 47

def configure
  yield self if block_given?
end

.current_dateObject



19
20
21
# File 'lib/holiday_co/utils.rb', line 19

def self.current_date
  current_time.to_date
end

.current_timeObject



11
12
13
# File 'lib/holiday_co/utils.rb', line 11

def self.current_time
  Time.now.getlocal(UTC_OFFSET)
end

.current_yearObject



15
16
17
# File 'lib/holiday_co/utils.rb', line 15

def self.current_year
  current_time.year
end

.exception_date?(day, month) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/holiday_co/utils.rb', line 70

def self.exception_date?(day, month)
  [26, 25].include?(day) && month == "04"
end

.generate_pascua_date(year, month, day) ⇒ Object



85
86
87
# File 'lib/holiday_co/utils.rb', line 85

def self.generate_pascua_date(year, month, day)
  { "#{year}" => "#{year}-#{month}-#{format('%02d', day)}" }
end

.holidays(year = current_year) ⇒ Object



18
19
20
# File 'lib/holiday_co.rb', line 18

def holidays(year = current_year)
  Year.new(year).holidays
end

.holidays_dates(year = current_year) ⇒ Object



26
27
28
# File 'lib/holiday_co.rb', line 26

def holidays_dates(year = current_year)
  Year.new(year).holiday_dates
end

.holidays_names(year = current_year) ⇒ Object



22
23
24
# File 'lib/holiday_co.rb', line 22

def holidays_names(year = current_year)
  Year.new(year).holiday_names
end

.is_holiday?(date = current_date) ⇒ Boolean Also known as: holiday?

Returns:

  • (Boolean)


12
13
14
# File 'lib/holiday_co.rb', line 12

def is_holiday?(date = current_date)
  Day.new(date).holiday?
end

.pascua_correction?(day, march_22_offset, days_to_sunday, lunar_cycle_position) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/holiday_co/utils.rb', line 74

def self.pascua_correction?(day, march_22_offset, days_to_sunday, lunar_cycle_position)
  day == 26 || march_22_offset == 28 && days_to_sunday == 6 && lunar_cycle_position > 10
end

.pascua_day_per_year(year) ⇒ Object



23
24
25
26
27
# File 'lib/holiday_co/utils.rb', line 23

def self.pascua_day_per_year(year)
  raise YearDataNotAvailableError unless AVAILABLE_YEARS.cover?(year)

  calculate_pascua_day_per_year(year)
end