Ingest a date-holidays
days map into Tempo.Holidays.Holiday.t/0.
A country's days is a map of rule string to metadata, exactly as the
upstream holidays.json (and holidays.yaml) hold it — the format the
specification defines:
%{
"12-25" => %{"_name" => "12-25"},
"4th thursday in November" => %{"name" => %{"en" => "Thanksgiving Day"}},
"07-04 and if sunday then next monday if saturday then previous friday" =>
%{"substitute" => true, "name" => %{"en" => "Independence Day"}}
}compile_days/2 turns that into holidays, keeping each rule string with
its dataset dates where Tempo.DateRule.parse/2 reads it, and dropping the
entries it does not (the Bengali calendar's). It is the source-agnostic
core the mix tempo.holidays.update task feeds a downloaded, parsed bundle
into.
Summary
Functions
Compile a single {rule, metadata} entry into a holiday.
Compile a country's days map into a list of holidays.
Functions
@spec compile_day(term(), term(), String.t(), map()) :: [Tempo.Holidays.Holiday.t()]
Compile a single {rule, metadata} entry into a holiday.
Arguments
ruleis the date-holidays rule string (the map key).metais its metadata map ("name","_name","type", …).languageis the ISO 639-1 code for name selection.namesis the bundle's shared_nametranslation table — the top-level"names"map — used to resolve a_namereference. Defaults to%{}(references then stand in for their own names).
Returns
[t:Tempo.Holidays.Holiday.t/0]— a one-element list on a supported rule, so it splices cleanly intoEnum.flat_map/2.[]whenTempo.DateRuledoes not read the rule, when the entry is disabled (metaisfalse, date-holidays' way of removing an inherited holiday), or when the entry is otherwise malformed.
Examples
iex> meta = %{"name" => %{"en" => "Independence Day"}, "substitute" => true}
iex> [holiday] = Tempo.Holidays.DateHolidays.compile_day("07-04", meta, "en")
iex> {holiday.name, holiday.type, holiday.rule}
{"Independence Day", :public, "07-04"}
iex> names = %{"01-01" => %{"name" => %{"en" => "New Year's Day", "fr" => "Nouvel An"}}}
iex> [holiday] = Tempo.Holidays.DateHolidays.compile_day("01-01", %{"_name" => "01-01"}, "fr", names)
iex> holiday.name
"Nouvel An"
iex> Tempo.Holidays.DateHolidays.compile_day("1st monday in May", false, "en")
[]
@spec compile_days(term(), keyword()) :: [Tempo.Holidays.Holiday.t()]
Compile a country's days map into a list of holidays.
Arguments
daysis the date-holidaysdaysmap — rule string to metadata.
Options
:languageis the ISO 639-1 code used to pick a localized name, falling back to English then the raw_name. Defaults to"en".
Returns
- A list of
Tempo.Holidays.Holiday.t/0, one per entry whose ruleTempo.DateRulereads. The others are omitted.
Examples
iex> days = %{
...> "12-25" => %{"name" => %{"en" => "Christmas Day"}},
...> "bengali-revised 1-1" => %{"name" => %{"en" => "Bengali New Year"}}
...> }
iex> holidays = Tempo.Holidays.DateHolidays.compile_days(days)
iex> Enum.map(holidays, & &1.name)
["Christmas Day"]