Koyomi.Holidays.Rules (Koyomi v0.2.1)

Copy Markdown View Source

Date rules for holidays: Easter, the nth weekday of a month, and the days that replace a holiday on a weekend. The built-in countries use them, and a Koyomi.Holidays.Provider can too.

Weekdays are numbered as in Date.day_of_week/1: 1 is Monday and 7 is Sunday.

Summary

Functions

Returns Easter Sunday of year in the Gregorian calendar (the Western churches' date), by the anonymous Gregorian computus.

Returns the nth weekday of a month, or the last one when n is :last.

Returns the first weekday on or after date.

Returns the last weekday on or before date.

Returns the days that replace holidays on a weekend, as {day, holiday} pairs.

Returns true when date is a Saturday or a Sunday.

Types

weekday()

@type weekday() :: 1..7

Functions

easter(year)

@spec easter(integer()) :: Date.t()

Returns Easter Sunday of year in the Gregorian calendar (the Western churches' date), by the anonymous Gregorian computus.

Examples

iex> Koyomi.Holidays.Rules.easter(2025)
~D[2025-04-20]

nth_weekday(year, month, weekday, n)

@spec nth_weekday(integer(), 1..12, weekday(), pos_integer() | :last) :: Date.t()

Returns the nth weekday of a month, or the last one when n is :last.

Examples

iex> Koyomi.Holidays.Rules.nth_weekday(2027, 11, 4, 4)
~D[2027-11-25]

iex> Koyomi.Holidays.Rules.nth_weekday(2027, 5, 1, :last)
~D[2027-05-31]

on_or_after(date, weekday)

@spec on_or_after(Date.t(), weekday()) :: Date.t()

Returns the first weekday on or after date.

Examples

iex> Koyomi.Holidays.Rules.on_or_after(~D[2027-06-20], 6)
~D[2027-06-26]

on_or_before(date, weekday)

@spec on_or_before(Date.t(), weekday()) :: Date.t()

Returns the last weekday on or before date.

Examples

iex> Koyomi.Holidays.Rules.on_or_before(~D[2027-05-24], 1)
~D[2027-05-24]

substitute_days(holidays, eligible)

Returns the days that replace holidays on a weekend, as {day, holiday} pairs.

Each holiday of eligible on a Saturday or a Sunday, in date order, is replaced by the next weekday that is neither a holiday of holidays nor a day already given to another holiday. This is the British rule for substitute days: with Christmas Day on a Saturday and Boxing Day on the Sunday, the days off are Monday 27 and Tuesday 28 December.

Examples

iex> christmas = {~D[2027-12-25], "Christmas Day", "Christmas Day"}
iex> boxing = {~D[2027-12-26], "Boxing Day", "Boxing Day"}
iex> Koyomi.Holidays.Rules.substitute_days([christmas, boxing], [christmas, boxing])
[{~D[2027-12-27], christmas}, {~D[2027-12-28], boxing}]

weekend?(date)

@spec weekend?(Date.t()) :: boolean()

Returns true when date is a Saturday or a Sunday.