# `Calendrical.Buddhist`

Implementation of the Thai Buddhist (Buddhist Era) calendar.

The Buddhist calendar shares its month and day structure with the
proleptic Gregorian calendar exactly. The only difference is the
*year numbering*: years are counted from the death (*parinirvana*) of
Gautama Buddha, traditionally placed at **543 BCE** in the Gregorian
calendar. The relation is:

    buddhist_year = gregorian_year + 543

So 1 BE corresponds to proleptic Gregorian year **−542** (543 BCE in
the historical "no year zero" convention) and modern Gregorian
2026 CE corresponds to **2569 BE**.

This calendar is the official solar calendar of Thailand, where it
is used alongside the Gregorian calendar in everyday life.

## Month and day structure

Months and days are identical to `Calendrical.Gregorian`. Leap years
follow the proleptic Gregorian rule:

> A year is a leap year if it is divisible by 4, except for centurial
> years that are not divisible by 400.

Day boundaries are at midnight, matching the Thai civil convention.

## Reference

- CLDR `:buddhist` calendar type. The CLDR era data places 1 BE
  (`:be`) at proleptic Gregorian `−542-01-01`.
- This module follows Reingold & Dershowitz's *Calendrical
  Calculations* (4th ed.) practice of treating year-shifted Gregorian
  variants as a thin wrapper around the Gregorian implementation.

# `day`

```elixir
@type day() :: 1..31
```

# `month`

```elixir
@type month() :: 1..12
```

# `year`

```elixir
@type year() :: integer()
```

# `buddhist_year`

```elixir
@spec buddhist_year(integer()) :: year()
```

Returns the Buddhist year corresponding to the given Gregorian year.

### Arguments

* `gregorian_year` is any proleptic Gregorian year as an integer.

### Returns

* An integer Buddhist year (543 more than the input).

### Examples

    iex> Calendrical.Buddhist.buddhist_year(2026)
    2569

# `calendar_base`

Identifies whether this calendar is month
or week based.

# `calendar_year`

```elixir
@spec calendar_year(Calendar.year(), Calendar.month(), Calendar.day()) ::
  Calendar.year()
```

Returns the calendar year as displayed
on rendered calendars.

# `cldr_calendar_type`

Defines the CLDR calendar type for this calendar.

This type is used in support of `Calendrical.
localize/3`.

# `cyclic_year`

```elixir
@spec cyclic_year(Calendar.year(), Calendar.month(), Calendar.day()) ::
  Calendar.year()
```

Returns the cyclic year as displayed
on rendered calendars.

# `date_from_iso_days`

```elixir
@spec date_from_iso_days(integer()) :: {year(), month(), day()}
```

Returns a Buddhist `{year, month, day}` tuple for the given ISO day
number.

### Arguments

* `iso_days` is an integer count of days since the proleptic
  ISO epoch.

### Returns

* A three-tuple `{year, month, day}` in Buddhist Era numbering.

### Examples

    iex> Calendrical.Buddhist.date_from_iso_days(739_252)
    {2567, 1, 2}

# `date_to_iso_days`

```elixir
@spec date_to_iso_days(year(), month(), day()) :: integer()
```

Returns the number of ISO days for the given Buddhist `year`,
`month`, and `day`.

### Arguments

* `year` is any Buddhist year as an integer.

* `month` is a month in the range `1..12`.

* `day` is a day-of-month.

### Returns

* An integer count of days since the proleptic ISO epoch.

### Examples

    iex> Calendrical.Buddhist.date_to_iso_days(2569, 1, 1)
    739982

# `day_of_era`

```elixir
@spec day_of_era(Calendar.year(), Calendar.month(), Calendar.day()) ::
  {day :: Calendar.day(), era :: Calendar.era()}
```

Calculates the day and era from the given
`year`, `month`, and `day`.

By default we consider on two eras: before the epoch
and on-or-after the epoch.

# `day_of_year`

```elixir
@spec day_of_year(Calendar.year(), Calendar.month(), Calendar.day()) :: Calendar.day()
```

Calculates the day of the year from the given
`year`, `month`, and `day`.

# `days_in_month`

```elixir
@spec days_in_month(Calendar.month()) ::
  Calendar.month()
  | {:ambiguous, Range.t() | [pos_integer()]}
  | {:error, :undefined}
```

Returns how many days there are in the given month.

Must be implemented in derived calendars because
we cannot know what the calendar format is.

# `days_in_month`

```elixir
@spec days_in_month(Calendar.year(), Calendar.month()) :: Calendar.month()
@spec days_in_month(year(), month()) :: 28..31
```

Returns the number of days in the given Buddhist `year` and `month`.

### Arguments

* `year` is any Buddhist year as an integer.

* `month` is a month in the range `1..12`.

### Returns

* The number of days in the month (`28..31`).

### Examples

    iex> Calendrical.Buddhist.days_in_month(2569, 2)
    28

# `days_in_week`

Returns the number days in a a week.

# `days_in_year`

```elixir
@spec days_in_year(year()) :: 365..366
```

Returns the number of days in the given Buddhist `year`.

### Arguments

* `year` is any Buddhist year as an integer.

### Returns

* `365` for an ordinary year or `366` for a leap year.

### Examples

    iex> Calendrical.Buddhist.days_in_year(2569)
    365

    iex> Calendrical.Buddhist.days_in_year(2567)
    366

# `epoch`

# `epoch_day_of_week`

# `extended_year`

```elixir
@spec extended_year(Calendar.year(), Calendar.month(), Calendar.day()) ::
  Calendar.year()
```

Returns the extended year as displayed
on rendered calendars.

# `first_day_of_week`

# `gregorian_offset`

```elixir
@spec gregorian_offset() :: 543
```

Returns the offset (in years) between the Buddhist Era and the
proleptic Gregorian calendar.

Subtracting this offset from a Buddhist year yields the corresponding
Gregorian year.

### Returns

* The integer `543`.

### Examples

    iex> Calendrical.Buddhist.gregorian_offset()
    543

# `gregorian_year`

```elixir
@spec gregorian_year(year()) :: integer()
```

Returns the Gregorian year corresponding to the given Buddhist year.

### Arguments

* `buddhist_year` is any Buddhist Era year as an integer.

### Returns

* An integer Gregorian year (543 less than the input).

### Examples

    iex> Calendrical.Buddhist.gregorian_year(2569)
    2026

# `iso_week_of_year`

```elixir
@spec iso_week_of_year(Calendar.year(), Calendar.month(), Calendar.day()) ::
  {:error, :not_defined}
```

Calculates the ISO week of the year from the
given `year`, `month`, and `day`.

By default this function always returns
`{:error, :not_defined}`.

# `last_day_of_week`

# `leap_year?`

```elixir
@spec leap_year?(year()) :: boolean()
```

Returns whether the given Buddhist `year` is a leap year.

The underlying calendar is proleptic Gregorian, so the leap-year rule
applies to the corresponding Gregorian year.

### Arguments

* `year` is any Buddhist year as an integer.

### Returns

* `true` if the year contains 366 days; otherwise `false`.

### Examples

    iex> Calendrical.Buddhist.leap_year?(2567)
    true

    iex> Calendrical.Buddhist.leap_year?(2569)
    false

# `month`

Returns a `t:Date.Range.t/0` representing
a given month of a year.

# `month_of_year`

```elixir
@spec month_of_year(Calendar.year(), Calendar.month(), Calendar.day()) ::
  Calendar.month() | {Calendar.month(), Calendrical.leap_month?()}
```

Returns the month of the year from the given
`year`, `month`, and `day`.

# `months_in_leap_year`

Returns the number of months in a leap year.

# `months_in_ordinary_year`

Returns the number of months in a normal year.

# `months_in_year`

Returns the number of months in a year, without a year.

Returns an integer when every year has the same number of
months, or `{:ambiguous, first..last}` for lunisolar calendars
whose year length varies (e.g. the Hebrew calendar's 12 or 13
months).

# `months_in_year`

Returns the number of months in a
given `year`.

# `naive_datetime_from_iso_days`

```elixir
@spec naive_datetime_from_iso_days(Calendar.iso_days()) ::
  {Calendar.year(), Calendar.month(), Calendar.day(), Calendar.hour(),
   Calendar.minute(), Calendar.second(), Calendar.microsecond()}
```

Converts the `t:Calendar.iso_days` format to the
datetime format specified by this calendar.

# `naive_datetime_to_iso_days`

```elixir
@spec naive_datetime_to_iso_days(
  Calendar.year(),
  Calendar.month(),
  Calendar.day(),
  Calendar.hour(),
  Calendar.minute(),
  Calendar.second(),
  Calendar.microsecond()
) :: Calendar.iso_days()
```

Returns the `t:Calendar.iso_days` format of
the specified date.

# `periods_in_year`

Returns the number of periods in a given
`year`. A period corresponds to a month
in month-based calendars and a week in
week-based calendars.

# `plus`

Adds an `increment` number of `date_part`s
to a `year-month-day`.

`date_part` can be `:years`, `:months`, `:weeks` or `:days`.

# `quarter`

Returns a `t:Date.Range.t/0` representing
a given quarter of a year.

# `quarter_of_year`

```elixir
@spec quarter_of_year(Calendar.year(), Calendar.month(), Calendar.day()) ::
  Calendrical.quarter()
```

Returns the quarter of the year from the given
`year`, `month`, and `day`.

# `related_gregorian_year`

```elixir
@spec related_gregorian_year(Calendar.year(), Calendar.month(), Calendar.day()) ::
  Calendar.year()
```

Returns the related Gregorian year as displayed
on rendered calendars.

Per TR35 this is the Gregorian year in which this
calendar's `year` begins, so it is constant for every
date of the calendar year.

# `valid_date?`

```elixir
@spec valid_date?(year(), month(), day()) :: boolean()
```

Returns whether the given `year`, `month`, and `day` form a valid
Buddhist date.

### Arguments

* `year` is any Buddhist year as an integer.

* `month` is a month in the range `1..12`.

* `day` is a day-of-month.

### Returns

* `true` if the date is valid; otherwise `false`.

### Examples

    iex> Calendrical.Buddhist.valid_date?(2569, 1, 31)
    true

    iex> Calendrical.Buddhist.valid_date?(2569, 2, 29)
    false

# `week`

Returns a `t:Date.Range.t/0` representing
a given week of a year.

# `week_of_month`

```elixir
@spec week_of_month(Calendar.year(), Calendar.month(), Calendar.day()) ::
  {pos_integer(), pos_integer()} | {:error, :not_defined}
```

Calculates the week of the year from the given
`year`, `month`, and `day`.

By default this function always returns
`{:error, :not_defined}`.

# `week_of_year`

```elixir
@spec week_of_year(Calendar.year(), Calendar.month(), Calendar.day()) ::
  {:error, :not_defined}
```

Calculates the week of the year from the given
`year`, `month`, and `day`.

By default this function always returns
`{:error, :not_defined}`.

# `weeks_in_year`

Returns the number of weeks in a
given `year`.

# `year`

Returns a `t:Date.Range.t/0` representing
a given year.

# `year_of_era`

```elixir
@spec year_of_era(Calendar.year()) :: {year :: Calendar.year(), era :: Calendar.era()}
```

Calculates the year and era from the given `year`.

# `year_of_era`

```elixir
@spec year_of_era(Calendar.year(), Calendar.month(), Calendar.day()) ::
  {year :: Calendar.year(), era :: Calendar.era()}
```

Calculates the year and era from the given `date`.

---

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