# `Tempo.Holidays.DateHolidays`
[🔗](https://github.com/elixir-tempo/tempo_holidays/blob/v0.1.0/lib/tempo/holidays/date_holidays.ex#L1)

Ingest a [date-holidays](https://github.com/commenthol/date-holidays)
`days` map into `t: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.

# `compile_day`

```elixir
@spec compile_day(term(), term(), String.t(), map()) :: [Tempo.Holidays.Holiday.t()]
```

Compile a single `{rule, metadata}` entry into a holiday.

### Arguments

* `rule` is the date-holidays rule string (the map key).

* `meta` is its metadata map (`"name"`, `"_name"`, `"type"`, …).

* `language` is the ISO 639-1 code for name selection.

* `names` is the bundle's shared `_name` translation table — the
  top-level `"names"` map — used to resolve a `_name` reference. 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 into `Enum.flat_map/2`.

* `[]` when `Tempo.DateRule` does not read the rule, when the entry is
  disabled (`meta` is `false`, 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")
    []

# `compile_days`

```elixir
@spec compile_days(term(), keyword()) :: [Tempo.Holidays.Holiday.t()]
```

Compile a country's `days` map into a list of holidays.

### Arguments

* `days` is the date-holidays `days` map — rule string to metadata.

### Options

* `:language` is 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 `t:Tempo.Holidays.Holiday.t/0`, one per entry whose rule
  `Tempo.DateRule` reads. 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"]

---

*Consult [api-reference.md](api-reference.md) for complete listing*
