View Source Cldr.Calendar behaviour (Cldr Calendars v1.22.0)
Calendar functions for calendars compatible with
Elixir's Calendar behaviour.
Cldr.Calendar supports the creation of calendars
that are variations on the proleptic Cldr.Calendar.Gregorian
calendar. It also adds additional functions, defined
by the Cldr.Calendar behaviour, to support these
derived calendars.
The common purpose of these derived calendars is to support the creation and use of financial year calendars that are commonly used in business.
There are two general types of calendars supported:
monthcalendars that mirror the monthly structure of the proleptic Cldr.Calendar.Gregorian calendar but which are deemed to start the year in a month other than January.weekcalendars that are defined to have a 52 week structure (53 weeks in a long year). These calendars can be configured to start or end on the first, last or nearest day to the beginning or end of a Cldr.Calendar.Gregorian month. The main intent behind this structure is to have each year start and end on the same day of the week with a consistent 13-week quarterly structure than enables a more straight forware comparison with same-period-last-year financial performance.
Link to this section Summary
Types
Specifies the type of a calendar.
Specifies the type of a calendar
Specifies the days of the week as integers.
The types of relationship between two Date.Range intervals
Represents the number of days since the calendar epoch.
Boolean indicating is this is a leap month
The precision for date intervals
Specifies the quarter of year for a calendar date.
Specifies the week of year for a calendar date.
Callbacks
Returns the calendar basis.
Returns a the year in a calendar year.
Returns the CLDR calendar type.
Returns a the cyclic year in a calendar year.
Returns the number of days in a month (withoout a year)
Returns the number of days in a year
Returns a the extended year in a calendar year.
Returns a tuple of {year, week_in_year} for a given year, month or week, and day
for a a calendar.
Returns a date range representing the days in a given month for a calendar year.
Returns the month for a given year, month or week, and day
for a a calendar.
Returns the number of periods (which are months in a month calendar and weeks in a week calendar) in a year
Increments a Date.t or Date.Range.t by a specified positive
or negative integer number of periods (year, quarter, month,
week or day).
Returns a date range representing the days in a given quarter for a calendar year.
Returns a the related year in a calendar year.
Returns a date range representing the days in a given week for a calendar year.
Returns a tuple of {month, week_in_month} for a given year, month or week, and day
for a a calendar.
Returns a tuple of {year, week_in_year} for a given year, month or week, and day
for a a calendar.
Returns the number of weeks in a year
Returns a date range representing the days in a calendar year.
Functions
Returns a calendar configured according to the preferences defined for a locale.
Returns a calendar configured according to the preferences defined for a territory.
Return the calendar module for a locale.
Returns the calendar module preferred for a territory.
Returns a boolean indicating if a module
is a Cldr.Calendar module
Returns the year number for
a date that is the representation
used for a calendar.
Returns the current date or date range for a date period (year, quarter, month, week or day).
Returns the cycle year
number for a date.
Returns a Date.t from a year, day_of_year
and a calendar.
Returns a date represented by a number of days since the start of the epoch.
Returns a Date.t from a keyword list
and a calendar.
Returns a Date.t from a date tuple of
{year, month, day} and a calendar.
Returns the number of days since the start of the epoch.
Formats a date into a string representation
Returns the DateTime (defaulting to UTC timezone) for the given Modified Julian Day.
Returns the {day_of_era, era} for
a date.
Returns the day of the year
for a date.
Returns the default calendar.
Returns the extended year number for
a date.
Returns the first day of a week for a given locale.
Returns the first date of a year
for a Date.t.
Returns the first date of a year
in a calendar.
Returns the gregorian date of the first day of of a year
for a calendar.
Returns the ordinal day number representing Friday.
An inspect_fun/2 that can be configured in
Inspect.Opts supporting inspection of user-defined
calendars.
Returns an Enumerable list of dates of a given precision
of either :years, :quarters, :months, :weeks or
:days
Returns an a Stream function than can be lazily
enumerated.
Returns the day of the week for a given
iso_day_number
Returns the ISO week number for
a date.
Returns the last date of a year
for a Date.t.
Returns the last date of a year
for a calendar.
Returns the gregorian date of the first day of a year
for a calendar.
Localize a date by converting it to calendar introspected from the provided or default locale.
Returns a localized string for a part of
a Date.t.
Returns the minimum days in the first week of a year for a given locale.
Decrements a date or date range by an integer amount of a date period (year, quarter, month, week or day).
Returns the Modified Julian Day of
a Date.t.
Returns the ordinal day number representing Monday
Returns the month number for
a date.
Creates a new calendar based upon the provided configuration.
Returns the next date or date range for a date period (year, quarter, month, week or day).
Adds a duration to a date
Increments a date or date range by an integer amount of a date period (year, quarter, month, week or day).
Returns the previous date or date range for a date period (year, quarter, month, week or day).
Returns the quarter number for
a date.
Returns the related gregorian year
number for a date.
Returns the ordinal day number representing Saturday.
Returns the ordinal day number representing Sunday.
Returns the ordinal day number representing Thursday.
Returns the ordinal day number representing Tuesday.
Validates if the argument is a Cldr.Calendar calendar module.
Returns the ordinal day number representing Wednesday.
Returns the {month, week_number}
for a date.
Returns the {year, week_number}
for a date.
Returns whether a given date is a weekday.
Returns a list of the days of the week that are considered a weekend for a given territory (country)
Returns a list of the days of the week that are considered a weekend for a given territory (country)
Returns whether a given date is a weekend day.
Returns the number of weeks in a year.
Returns the number of days in n weeks
Returns the {year_of_era, era} for
a date.
Link to this section Types
@type calendar() :: module()
Specifies the type of a calendar.
A calendar is a module that implements
the Calendar and Cldr.Calendar
behaviours.
@type calendar_type() :: :month | :week
Specifies the type of a calendar
@type day_of_week() :: 1..7
Specifies the days of the week as integers.
Days of the week are encoded as the integers 1 through
7 with 1 representig Monday and 7 representing Sunday.
Note that a calendar can be configured to start
on any day of the week. day_of_week is only a
way of encoding the days as an integer.
@type interval_relation() ::
:precedes
| :preceded_by
| :meets
| :met_by
| :overlaps
| :overlapped_by
| :finished_by
| :finishes
| :contains
| :during
| :starts
| :started_by
| :equals
The types of relationship between two Date.Range intervals
@type iso_day_number() :: integer()
Represents the number of days since the calendar epoch.
The Calendar epoch is 0000-01-01
in the proleptic gregorian calendar.
@type leap_month?() :: boolean()
Boolean indicating is this is a leap month
@type precision() :: :years | :quarters | :months | :weeks | :days
The precision for date intervals
@type quarter() :: 1..4
Specifies the quarter of year for a calendar date.
@type week() :: pos_integer()
Specifies the week of year for a calendar date.
Link to this section Callbacks
@callback calendar_base() :: :week | :month
Returns the calendar basis.
Returns either :week or :month
@callback calendar_year(Calendar.year(), week(), Calendar.day()) :: Calendar.year()
Returns a the year in a calendar year.
@callback cldr_calendar_type() ::
:gregorian | :persian | :coptic | :ethiopic | :chinese | :japanese | :dangi
Returns the CLDR calendar type.
Only algorithmic calendars are considered in this implementation
@callback cyclic_year(Calendar.year(), Calendar.month(), Calendar.day()) :: Calendar.year()
Returns a the cyclic year in a calendar year.
@callback days_in_month(month :: Calendar.month()) :: Calendar.day() | {:ambiguous, Range.t() | [pos_integer()]} | {:error, :undefined}
Returns the number of days in a month (withoout a year)
@callback days_in_year(year :: Calendar.year()) :: Calendar.day()
Returns the number of days in a year
@callback extended_year(Calendar.year(), Calendar.month(), Calendar.day()) :: Calendar.year()
Returns a the extended year in a calendar year.
@callback iso_week_of_year( year :: Calendar.year(), month :: Calendar.month(), day :: Calendar.day() ) :: {Calendar.year(), Calendar.week()} | {:error, :not_defined}
Returns a tuple of {year, week_in_year} for a given year, month or week, and day
for a a calendar.
The iso_week_of_year is calculated based on the ISO calendar.
@callback month(year :: Calendar.year(), month :: Calendar.month()) :: Date.Range.t() | {:error, :not_defined}
Returns a date range representing the days in a given month for a calendar year.
@callback month_of_year( year :: Calendar.year(), month :: Calendar.month() | week(), day :: Calendar.day() ) :: Calendar.month() | {Calendar.month(), leap_month?()}
Returns the month for a given year, month or week, and day
for a a calendar.
The month_of_year is calculated based upon the calendar configuration.
@callback periods_in_year(year :: Calendar.year()) :: week() | Calendar.month()
Returns the number of periods (which are months in a month calendar and weeks in a week calendar) in a year
@callback plus( year :: Calendar.year(), month :: Calendar.month() | week(), day :: Calendar.day(), months_or_quarters :: :months | :quarters, increment :: integer(), options :: Keyword.t() ) :: {Calendar.year(), Calendar.month(), Calendar.day()}
Increments a Date.t or Date.Range.t by a specified positive
or negative integer number of periods (year, quarter, month,
week or day).
Calendars need only implement this callback for :months and :quarters
since all other date periods can be derived.
@callback quarter(year :: Calendar.year(), quarter :: quarter()) :: Date.Range.t() | {:error, :not_defined}
Returns a date range representing the days in a given quarter for a calendar year.
@callback week(year :: Calendar.year(), week :: week()) :: Date.Range.t() | {:error, :not_defined}
Returns a date range representing the days in a given week for a calendar year.
@callback week_of_month(Calendar.year(), week(), Calendar.day()) :: {Calendar.month(), week()} | {:error, :not_defined}
Returns a tuple of {month, week_in_month} for a given year, month or week, and day
for a a calendar.
The week_in_month is calculated based upon the calendar configuration.
@callback week_of_year( year :: Calendar.year(), month :: Calendar.month() | week(), day :: Calendar.day() ) :: {Calendar.year(), Calendar.week()} | {:error, :not_defined}
Returns a tuple of {year, week_in_year} for a given year, month or week, and day
for a a calendar.
The week_in_year is calculated based upon the calendar configuration.
@callback weeks_in_year(year :: Calendar.year()) :: week() | {:error, :not_defined}
Returns the number of weeks in a year
@callback year(year :: Calendar.year()) :: Date.Range.t() | {:error, :not_defined}
Returns a date range representing the days in a calendar year.
Link to this section Functions
Returns a calendar configured according to the preferences defined for a locale.
Returns a calendar configured according to the preferences defined for a territory.
Return the calendar module for a locale.
arguments
Arguments
:localeis any locale or locale name validated byCldr.validate_locale/2. The default isCldr.get_locale()which returns the locale set for the current process
returns
Returns
{:ok, calendar_module}or{:error, {exception, reason}}
examples
Examples
iex> Cldr.Calendar.calendar_from_locale "en-US"
{:ok, Cldr.Calendar.US}
iex> Cldr.Calendar.calendar_from_locale "en-GB-u-ca-gregory"
{:ok, Cldr.Calendar.GB}
iex> Cldr.Calendar.calendar_from_locale "fa-IR"
{:ok, Cldr.Calendar.Persian}
iex> Cldr.Calendar.calendar_from_locale "fa-IR-u-ca-gregory"
{:ok, Cldr.Calendar.IR}
Returns the calendar module preferred for a territory.
arguments
Arguments
territoryis any valid ISO3166-2 code as anString.tor upcasedatom()
returns
Returns
{:ok, calendar_module}or{:error, {exception, reason}}
examples
Examples
iex> Cldr.Calendar.calendar_from_territory :US
{:ok, Cldr.Calendar.US}
iex> Cldr.Calendar.calendar_from_territory :YY
{:error, {Cldr.UnknownTerritoryError, "The territory :YY is unknown"}}
notes
Notes
The overwhelming majority of territories have
:gregorian as their first preferred calendar
and therefore Cldr.Calendar.Gregorian or
a derivation of it will be returned for most
territories.
Returning any other calendar module would require:
That another calendar is preferred over
:gregorianfor a territoryThat a calendar module is available to support that calendar.
As an example, Iran (territory :IR) prefers the
:persian calendar. If the optional library
ex_cldr_calendars_persian
is installed, the calendar module Cldr.Calendar.Persian will
be returned. If it is not installed, Cldr.Calendar.Gregorian
will be returned as :gregorian is the second preference
for :IR.
Returns a boolean indicating if a module
is a Cldr.Calendar module
@spec calendar_year(Date.t()) :: Calendar.year()
Returns the year number for
a date that is the representation
used for a calendar.
The calendar year may be different the the year in the struct. The struct year is designed for convertability and for date/time arithmetic.
The representation in rendered calendar may be different. For example, in the Chinese calendar the cardinal year since epoch is stored in the struct but the calendar year used for representation is the sexigesimal year (a number between 1 and 60).
arguments
Arguments
dateis anyDate.t()
returns
Returns
- the calendar year as an integer.
examples
Examples
iex> Cldr.Calendar.calendar_year ~D[2019-01-01]
2019
iex> Cldr.Calendar.calendar_year Cldr.Calendar.first_day_of_year(2019, Cldr.Calendar.NRF)
2019
iex> Cldr.Calendar.calendar_year Cldr.Calendar.last_day_of_year(2019, Cldr.Calendar.NRF)
2019
Returns the current date or date range for a date period (year, quarter, month, week or day).
arguments
Arguments
date_or_date_rangeis anyDate.torDate.Range.tperiodis:year,:quarter,:month,:weekor:day
returns
Returns
When a Date.t is passed, a Date.t is
returned. When a Date.Range.t is passed
a Date.Range.t is returned.
examples
Examples
iex> Cldr.Calendar.current ~D[2019-01-01], :day
~D[2019-01-01]
@spec cyclic_year(Date.t()) :: Calendar.year()
Returns the cycle year
number for a date.
A related gregorian year is the gregorian year that is most closely associated with a date that is in another calendar.
arguments
Arguments
dateis anyDate.t()
returns
Returns
- the cyclic year as an integer.
examples
Examples
iex> Cldr.Calendar.cyclic_year ~D[2019-01-01]
2019
iex> Cldr.Calendar.cyclic_year Cldr.Calendar.first_day_of_year(2019, Cldr.Calendar.NRF)
2019
iex> Cldr.Calendar.cyclic_year Cldr.Calendar.last_day_of_year(2019, Cldr.Calendar.NRF)
2019
date_from_day_of_year(year, day_of_year, calendar \\ Cldr.Calendar.Gregorian)
View SourceReturns a Date.t from a year, day_of_year
and a calendar.
arguments
Arguments
yearis any integer year that is valid incalendarday_of_yearis any valid ordinal day ofyearcalendaris any module implementing theCalendarandCldr.Calendarbehaviours. The default isCldr.Calendar.Gregorian.
returns
Returns
a
Date.tor{:error, :invalid_date}
@spec date_from_iso_days(Calendar.iso_days() | iso_day_number(), calendar()) :: Date.t()
Returns a date represented by a number of days since the start of the epoch.
The start of the epoch is the date
0000-01-01.
argumenets
Argumenets
iso_daysis an integer representing the number of days since the start of the epoch.calendaris any module that implements theCalendarandCldr.Calendarbehaviours
returns
Returns
- a
Date.t()
example
Example
iex> Cldr.Calendar.date_from_iso_days 737425, Calendar.ISO
~D[2019-01-01]
iex> Cldr.Calendar.date_from_iso_days 366, Calendar.ISO
~D[0001-01-01]
iex> Cldr.Calendar.date_from_iso_days 0, Calendar.ISO
~D[0000-01-01]
Returns a Date.t from a keyword list
and a calendar.
arguments
Arguments
[year: year, month: month, day: day}is a keyword list representing a datecalendaris any module implementing theCalendarandCldr.Calendarbehaviours. The default isCldr.Calendar.Gregorian.
returns
Returns
a
Date.tor{:error, :invalid_date}
examples
Examples
iex> Cldr.Calendar.date_from_list [year: 2019, month: 3, day: 25], Cldr.Calendar.Gregorian
%Date{calendar: Cldr.Calendar.Gregorian, day: 25, month: 3, year: 2019}
iex> Cldr.Calendar.date_from_list [year: 2019, month: 2, day: 29], Cldr.Calendar.Gregorian
{:error, :invalid_date}
Returns a Date.t from a date tuple of
{year, month, day} and a calendar.
arguments
Arguments
{year, month, day}is a tuple representing a datecalendaris any module implementing theCalendarandCldr.Calendarbehaviours. The default isCldr.Calendar.Gregorian.
returns
Returns
- a
Date.t
examples
Examples
iex> Cldr.Calendar.date_from_tuple {2019, 3, 25}, Cldr.Calendar.Gregorian
%Date{calendar: Cldr.Calendar.Gregorian, day: 25, month: 3, year: 2019}
iex> Cldr.Calendar.date_from_tuple {2019, 2, 29}, Cldr.Calendar.Gregorian
{:error, :invalid_date}
@spec date_to_iso_days(Date.t()) :: iso_day_number()
Returns the number of days since the start of the epoch.
The start of the epoch is the date 0000-01-01.
argumenets
Argumenets
dateis anyDate.t()
returns
Returns
- The integer number of days since the epoch
for the given
date.
example
Example
iex> Cldr.Calendar.date_to_iso_days ~D[2019-01-01]
737425
iex> Cldr.Calendar.date_to_iso_days ~D[0001-01-01]
366
iex> Cldr.Calendar.date_to_iso_days ~D[0000-01-01]
0
Formats a date into a string representation
Note that the output is not decorated with the calendar module name.
example
Example
iex> Cldr.Calendar.date_to_string ~D[2019-12-04]
"2019-12-04"
iex> Cldr.Calendar.date_to_string ~D[2019-23-04 Cldr.Calendar.NRF]
"2019-W23-4"
Returns the DateTime (defaulting to UTC timezone) for the given Modified Julian Day.
arguments
Arguments
mjdis a number representing days passed since November 17, 1858 (Julian Calendar)
returns
Returns
- a
DateTime.t()at UTC timezone
examples
Examples
iex> Cldr.Calendar.datetime_from_modified_julian_date(59848)
~U[2022-09-26 00:00:00.000Z]
iex> Cldr.Calendar.datetime_from_modified_julian_date(59848.75)
~U[2022-09-26 18:00:00.000Z]
@spec day_of_era(Date.t()) :: {Calendar.day(), Calendar.era()}
Returns the {day_of_era, era} for
a date.
arguments
Arguments
dateis anyDate.t()
returns
Returns
- a the days since the start of the era and the era of the year as a tuple
examples
Examples
iex> Cldr.Calendar.day_of_era ~D[2019-01-01]
{737060, 1}
iex> Cldr.Calendar.day_of_era Cldr.Calendar.first_day_of_year(2019, Cldr.Calendar.NRF)
{737093, 1}
iex> Cldr.Calendar.day_of_era Cldr.Calendar.last_day_of_year(2019, Cldr.Calendar.NRF)
{737456, 1}
See Date.day_of_week/1.
@spec day_of_year(Date.t()) :: Calendar.day()
Returns the day of the year
for a date.
arguments
Arguments
dateis anyDate.t()
returns
Returns
- a the day of the year as an integer
examples
Examples
iex> Cldr.Calendar.day_of_year ~D[2019-01-01]
1
iex> Cldr.Calendar.day_of_year ~D[2016-12-31]
366
iex> Cldr.Calendar.day_of_year ~D[2019-12-31]
365
iex> Cldr.Calendar.day_of_year ~D[2019-52-07 Cldr.Calendar.NRF]
365
iex> Cldr.Calendar.day_of_year ~D[2012-53-07 Cldr.Calendar.NRF]
372
See Date.days_in_month/1.
Returns the default calendar.
@spec extended_year(Date.t()) :: Calendar.year()
Returns the extended year number for
a date.
arguments
Arguments
dateis anyDate.t()
returns
Returns
- the extended calendar year as an integer.
examples
Examples
iex> Cldr.Calendar.extended_year ~D[2019-01-01]
2019
iex> Cldr.Calendar.extended_year Cldr.Calendar.first_day_of_year(2019, Cldr.Calendar.NRF)
2019
iex> Cldr.Calendar.extended_year Cldr.Calendar.last_day_of_year(2019, Cldr.Calendar.NRF)
2019
Returns the first day of a week for a given locale.
Note that the first of the first week is commonly not aligned with the first day of the year.
Returns the first date of a year
for a Date.t.
arguments
Arguments
dateis anyDate.t()
returns
Returns
a
Date.t()or{:error, :invalid_date}
examples
Examples
iex> Cldr.Calendar.first_day_of_year ~D[2019-12-01]
~D[2019-01-01]
@spec first_day_of_year(year :: Calendar.year(), calendar :: calendar()) :: Date.t()
Returns the first date of a year
in a calendar.
arguments
Arguments
yearis any yearcalendaris any module that implements theCalendarandCldr.Calendarbehaviours
returns
Returns
a
Date.t()or{:error, :invalid_date}
examples
Examples
iex> Cldr.Calendar.first_day_of_year 2019, Cldr.Calendar.Gregorian
%Date{calendar: Cldr.Calendar.Gregorian, day: 1, month: 1, year: 2019}
iex> Cldr.Calendar.first_day_of_year 2019, Cldr.Calendar.NRF
%Date{calendar: Cldr.Calendar.NRF, day: 1, month: 1, year: 2019}
@spec first_gregorian_day_of_year(Calendar.year(), calendar()) :: Date.t() | {:error, :invalid_date}
Returns the gregorian date of the first day of of a year
for a calendar.
arguments
Arguments
yearis any integer year numbercalendaris any module that implements theCalendarandCldr.Calendarbehaviours orCalendar.ISO
examples
Examples
iex> Cldr.Calendar.first_gregorian_day_of_year 2019, Cldr.Calendar.Gregorian
%Date{calendar: Cldr.Calendar.Gregorian, day: 1, month: 1, year: 2019}
iex> Cldr.Calendar.first_gregorian_day_of_year 2019, Cldr.Calendar.NRF
%Date{calendar: Cldr.Calendar.Gregorian, day: 3, month: 2, year: 2019}
iex> Cldr.Calendar.first_gregorian_day_of_year ~D[2019-12-01]
~D[2019-01-01]
@spec friday() :: 5
Returns the ordinal day number representing Friday.
@spec inspect(term(), list()) :: Inspect.Algebra.t()
An inspect_fun/2 that can be configured in
Inspect.Opts supporting inspection of user-defined
calendars.
This function can be configured in IEx for Elixir version 1.9
and later by:
IEx.configure(inspect: [inspect_fun: &Cldr.Calendar.inspect/2])
:ok
@spec interval( date_from :: Date.t(), date_to_or_count :: Date.t() | non_neg_integer(), precision() ) :: [Date.t()]
Returns an Enumerable list of dates of a given precision
of either :years, :quarters, :months, :weeks or
:days
arguments
Arguments
date_fromis a anyDate.tthat is the start of the sequencedate_to_or_countis upper bound of the sequence as aDate.tor the number of dates in the sequence to be generatedprecisionis one of:years,:quarters,:months,:weeksor:days
The sequence is generated starting with date_from until the next date
in the sequence would be after date_to.
notes
Notes
The sequence can be in ascending or descending date order
based upon whether date_from is greater than date_to.
returns
Returns
- A list of dates
examples
Examples
iex> d = ~D[2019-01-31]
~D[2019-01-31]
iex> d2 = ~D[2019-05-31]
~D[2019-05-31]
iex> Cldr.Calendar.interval d, 3, :months
[~D[2019-01-31], ~D[2019-02-28], ~D[2019-03-31]]
iex> Cldr.Calendar.interval d, d2, :months
[~D[2019-01-31], ~D[2019-02-28], ~D[2019-03-31],
~D[2019-04-30], ~D[2019-05-31]]
@spec interval_stream( date_from :: Date.t(), date_to_or_count :: Date.t() | non_neg_integer(), precision() ) :: (... -> any())
Returns an a Stream function than can be lazily
enumerated.
This function has the same arguments and provides
the same functionality as interval/3 except that
it is lazily evaluated.
arguments
Arguments
date_fromis a anyDate.tthat is the start of the sequencedate_to_or_countis upper bound of the sequence as aDate.tor the number of dates in the sequence to be generatedprecisionis one of:years,:quarters,:months,:weeksor:days
The sequence is generated starting with date_from until the next date
in the sequence would be after date_to.
notes
Notes
The sequence can be in ascending or descending date order
based upon whether date_from is greater than date_to.
returns
Returns
- A list of dates
examples
Examples
iex> d = ~D[2019-01-31]
~D[2019-01-31]
iex> d2 = ~D[2019-05-31]
~D[2019-05-31]
iex> Cldr.Calendar.interval_stream(d, 3, :months) |> Enum.to_list
[~D[2019-01-31], ~D[2019-02-28], ~D[2019-03-31]]
iex> Cldr.Calendar.interval_stream(d, d2, :months) |> Enum.to_list
[~D[2019-01-31], ~D[2019-02-28], ~D[2019-03-31],
~D[2019-04-30], ~D[2019-05-31]]
@spec iso_days_to_day_of_week(Calendar.iso_days() | Calendar.day()) :: day_of_week()
Returns the day of the week for a given
iso_day_number
arguments
Arguments
iso_day_numberis the number of days since the start of the epoch. SeeCldr.Calendar.date_to_iso_days/1
returns
Returns
- An integer representing a day of the week where Monday
is represented by
1and Sunday is represented by7
examples
Examples
iex> days = Cldr.Calendar.date_to_iso_days ~D[2019-01-01]
iex> Cldr.Calendar.iso_days_to_day_of_week(days) == Cldr.Calendar.tuesday
true
@spec iso_week_of_year(Date.t()) :: {Calendar.year(), week()}
Returns the ISO week number for
a date.
arguments
Arguments
dateis anyDate.t()
returns
Returns
a the ISO week of the year as an integer or
{:error, :not_defined}is the calendar does not support the concept of weeks.
examples
Examples
iex> Cldr.Calendar.iso_week_of_year ~D[2019-01-01]
{2019, 1}
iex> Cldr.Calendar.iso_week_of_year ~D[2019-02-01]
{2019, 5}
iex> Cldr.Calendar.iso_week_of_year ~D[2019-52-01 Cldr.Calendar.NRF]
{2020, 4}
iex> Cldr.Calendar.iso_week_of_year ~D[2019-26-01 Cldr.Calendar.NRF]
{2019, 30}
iex> Cldr.Calendar.iso_week_of_year ~D[2019-12-01 Cldr.Calendar.Julian]
{:error, :not_defined}
Returns the last date of a year
for a Date.t.
arguments
Arguments
dateis anyDate.t()
returns
Returns
a
Date.t()or{:error, :invalid_date}
examples
Examples
iex> Cldr.Calendar.last_day_of_year ~D[2019-01-01]
~D[2019-12-31]
@spec last_day_of_year(year :: Calendar.year(), calendar :: calendar()) :: Date.t()
Returns the last date of a year
for a calendar.
arguments
Arguments
yearis any yearcalendaris any module that implements theCalendarandCldr.Calendarbehaviours
returns
Returns
a
Date.t()or{:error, :invalid_date}
examples
Examples
iex> Cldr.Calendar.last_day_of_year(2019, Cldr.Calendar.Gregorian)
%Date{calendar: Cldr.Calendar.Gregorian, day: 31, month: 12, year: 2019}
iex> Cldr.Calendar.last_day_of_year(2019, Cldr.Calendar.NRF)
%Date{calendar: Cldr.Calendar.NRF, day: 7, month: 52, year: 2019}
@spec last_gregorian_day_of_year(Calendar.year(), calendar()) :: Date.t() | {:error, :invalid_date}
Returns the gregorian date of the first day of a year
for a calendar.
arguments
Arguments
yearis any integer year numbercalendaris any module that implements theCalendarandCldr.Calendarbehaviours orCalendar.ISO
examples
Examples
iex> Cldr.Calendar.last_gregorian_day_of_year 2019, Cldr.Calendar.Gregorian
%Date{calendar: Cldr.Calendar.Gregorian, day: 31, month: 12, year: 2019}
iex> Cldr.Calendar.last_gregorian_day_of_year 2019, Cldr.Calendar.NRF
%Date{calendar: Cldr.Calendar.Gregorian, day: 1, month: 2, year: 2020}
iex> Cldr.Calendar.last_gregorian_day_of_year ~D[2019-12-01]
~D[2019-12-31]
@spec localize(Date.t()) :: {:ok, Date.t()} | {:error, :incompatible_calendars} | {:error, {module(), String.t()}}
Localize a date by converting it to calendar introspected from the provided or default locale.
arguments
Arguments
dateis anyDate.toptionsis a Keyword list of options. The default is[]
options
Options
:localeis any valid locale name in the list returned byCldr.known_locale_names/1or aCldr.LanguageTagstruct returned byCldr.Locale.new!/2. The default isCldr.get_locale().:backendis any module that includesuse Cldrand therefore is aCldrbackend module. The default isdefault_backend/0.
returns
Returns
{:ok, date}wheredateis converted into the calendar associated with the current or provided locale.
examples
Examples
iex> Cldr.Calendar.localize ~D[2022-06-09], locale: "fr"
{:ok, %Date{year: 2022, month: 6, day: 9, calendar: Cldr.Calendar.FR}}
Returns a localized string for a part of
a Date.t.
arguments
Arguments
dateis anyDate.tpartis one of:era,:quarter,:month,:day_of_weekor:days_of_weekoptionsis a Keyword list of options
options
Options
:localeis any valid locale name in the list returned byCldr.known_locale_names/1or aCldr.LanguageTagstruct returned byCldr.Locale.new!/2. The default isCldr.get_locale().:backendis any module that includesuse Cldrand therefore is aCldrbackend module. The default isdefault_backend/0.:formatis one of:wide,:abbreviatedor:narrow. The default is:abbreviated.
returns
Returns
A string representing the localized date part, or
A list of strings representing the days of the week for the part
:days_of_week. The days are in week order for the given date's calendar{error, {exception_module, message}}if an error is detected
examples
Examples
iex> Cldr.Calendar.localize ~D[2019-01-01], :era
"AD"
iex> Cldr.Calendar.localize ~D[2019-01-01], :day_of_week
"Tue"
iex> Cldr.Calendar.localize ~D[0001-01-01], :day_of_week
"Mon"
iex> Cldr.Calendar.localize ~D[2019-01-01], :days_of_week
[{1, "Mon"}, {2, "Tue"}, {3, "Wed"}, {4, "Thu"}, {5, "Fri"}, {6, "Sat"}, {7, "Sun"}]
iex> Cldr.Calendar.localize ~D[2019-06-01], :era
"AD"
iex> Cldr.Calendar.localize ~D[2019-06-01], :quarter
"Q2"
iex> Cldr.Calendar.localize ~D[2019-06-01], :month
"Jun"
iex> Cldr.Calendar.localize ~D[2019-06-01], :day_of_week
"Sat"
iex> Cldr.Calendar.localize ~D[2019-06-01], :day_of_week, format: :wide
"Saturday"
iex> Cldr.Calendar.localize ~D[2019-06-01], :day_of_week, format: :narrow
"S"
iex> Cldr.Calendar.localize ~D[2019-06-01], :day_of_week, locale: "ar"
"السبت"
Returns the minimum days in the first week of a year for a given locale.
Decrements a date or date range by an integer amount of a date period (year, quarter, month, week or day).
arguments
Arguments
date_or_date_rangeis anyDate.torDate.Range.tperiodis:years,:quarters,:months,:weeksor:daysoptionsis a Keyword list of options
options
Options
:coerceis a boolean which, when set totruewill coerce the month and/or day to be a valid date. This affects,for example, moving to the previous month from~D[2019-03-31]. Since there is no date~D[2019-02-31]this would normally return{:error, :invalid_date}. Settingcoerce: trueit will return~D[2019-02-28].
returns
Returns
When a Date.t is passed, a Date.t is
returned. When a Date.Range.t is passed
a Date.Range.t is returned.
examples
Examples
iex> Cldr.Calendar.minus ~D[2016-03-01], :days, 1
~D[2016-02-29]
iex> Cldr.Calendar.minus ~D[2019-03-01], :months, 1
~D[2019-02-01]
iex> Cldr.Calendar.minus ~D[2016-03-01], :days, 1
~D[2016-02-29]
iex> Cldr.Calendar.minus ~D[2019-03-01], :days, 1
~D[2019-02-28]
iex> Cldr.Calendar.minus ~D[2019-03-01], :months, 1
~D[2019-02-01]
iex> Cldr.Calendar.minus ~D[2019-03-01], :quarters, 1
~D[2018-12-01]
iex> Cldr.Calendar.minus ~D[2019-03-01], :years, 1
~D[2018-03-01]
Returns the Modified Julian Day of
a Date.t.
arguments
Arguments
date_or_datetimeis anyDate.t()or aDateTime.t()if aDateTime.t()is given, the result will be given at the current timezone.
returns
Returns
- an number representing the
Modified Julian Day of the
date
notes
Notes
The Modified Julian Day is the number of days since November 17, 1858. Therefore this function only returns valid values for dates after this date.
examples
Examples
iex> Cldr.Calendar.modified_julian_day ~D[2019-01-01]
58484.0
iex> Cldr.Calendar.modified_julian_day ~U[2019-01-01 12:00:00Z]
58484.5
iex> Cldr.Calendar.modified_julian_day(~U[2022-09-26 18:00:00.000Z])
59848.75If the given DateTime is not UTC, the result is given on the local timezone
iex> dt = DateTime.shift_zone!(~U[2019-01-01 14:00:00Z], "America/Sao_Paulo")
#DateTime<2019-01-01 12:00:00-02:00 -02 America/Sao_Paulo>
iex> Cldr.Calendar.modified_julian_day(dt)
58484.5
@spec monday() :: 1
Returns the ordinal day number representing Monday
@spec month_of_year(Date.t()) :: Calendar.month() | {Calendar.month(), leap_month :: :leap}
Returns the month number for
a date.
arguments
Arguments
dateis anyDate.t()
returns
Returns
- a the month of the year as an integer
examples
Examples
iex> Cldr.Calendar.month_of_year ~D[2019-01-01]
1
iex> Cldr.Calendar.month_of_year ~D[2019-12-01]
12
iex> Cldr.Calendar.month_of_year ~D[2019-52-01 Cldr.Calendar.NRF]
12
iex> Cldr.Calendar.month_of_year ~D[2019-26-01 Cldr.Calendar.NRF]
6
@spec new(module(), calendar_type(), Keyword.t()) :: {:ok, calendar()} | {:module_already_exists, module()}
Creates a new calendar based upon the provided configuration.
If a module exists with the calendar_module name then it
is returned, not recreated.
arguments
Arguments
calendar_moduleis am atom representing the module name of the created calendar.calendar_typeis an atom of either:monthor:weekindicating which type of calendar is to be createdconfigis a Keyword list defining the configuration of the calendar.
returns
Returns
{:ok, module}wheremoduleis the new calendar module that conforms to theCalendarandCldr.Calendarbehaviours or{:module_already_exists, module}if a module of the given calendar name already exists. It is not guaranteed that the module is in fact a calendar module in this case.
configuration-options
Configuration options
The following options can be provided to create a new calendar.
:cldr_backenddefines a default backend module to be used for this calendar. The default isnil.:weeks_in_monthdefines the layout of weeks in a quarter for a week- or month- based calendar. The value must be one of[4, 4, 5],[4,5,4]or[5,4,4]. The default is[4,4,5]. This option is ignored for:monthbased calendars that have the parameterday_of_year: :first.:begins_or_endsdetermines whether the calendar year begins or ends on the given:day_of_weekand:month_of_year. The default is:begins.:first_or_lastdetermines whether the calendar year starts (or ends) on the first, last or nearest:day-of_weekand:month_of_year. The default is:first:day_of_weekdetermines the day of the week on which this calendar begins or ends. It may be a number in the range1..7representing Monday to Sunday. It may also be:firstindicating the the weeks are calculated from the first day of the calendar day irrespective of the day of the week. In this case the last week of the year may be less than 7 days in length. The default is1.:month_of_yeardetermines the Cldr.Calendar.Gregorian month of year in which this calendar begins or ends. The default is1.:yearis used to determine which calendar Greogian year is applicable for a given calendar date. The valid options are:first,:lastandmajority. The default is:majority.:min_days_in_first_weekis used to determine how many days of the Cldr.Calendar.Gregorian year must be in the first week of a calendar year. This is used when determining when the year starts for week-based years. The default is4which is consistent with the ISO Week calendar
examples
Examples
Each calendar has a function __config__/0 generated within
it and therefore the configuration of the included calendars
in ex_cldr_calendars provide insight into the behaviour
of the configuration parameters.
As an example here we define the ISO Week calendar calendar in full:
defmodule ISOWeek do
use Cldr.Calendar.Base.Week,
day_of_week: 1, # Weeks begin or end on Monday
month_of_year: 1, # Years begin or end in January
min_days_in_first_week, 4, # 4 Cldr.Calendar.Gregorian days of the year must be in the first week
begins_or_ends: :begins, # The year *begins* on the `day_of_week` and `month_of_year`
first_or_last: :first, # They year *begins* on the *first* `day_of_week` and `month_of_year`
weeks_in_month: [4, 4, 5], # The weeks are laid out as *months* in a `[4,4,5]` pattern
year: :majority, # Any given year is that in which the majority of Cldr.Calendar.Gregorian months fall
cldr_backend: nil, # No default `cldr_backend` is configured.
locale: nil # No `locale` is used to aid configuration
endThis can be generated at runtime by:
iex> Cldr.Calendar.new ISOWeek, :week,
...> day_of_week: 1,
...> month_of_year: 1,
...> min_days_in_first_week: 4,
...> begins_or_ends: :begins,
...> first_or_last: :first,
...> weeks_in_month: [4, 4, 5],
...> year: :majority,
...> cldr_backend: nil
{:ok, ISOWeek}Note that Cldr.Calendar.ISOWeek is included as part of this
library.
Returns the next date or date range for a date period (year, quarter, month, week or day).
arguments
Arguments
date_or_date_rangeis anyDate.torDate.Range.tperiodis:year,:quarter,:month,:weekor:day
returns
Returns
When a Date.t is passed, a Date.t is
returned. When a Date.Range.t is passed
a Date.Range.t is returned.
examples
Examples
iex> Cldr.Calendar.next ~D[2019-01-01], :day
~D[2019-01-02]
iex> Cldr.Calendar.next ~D[2019-01-01], :month
~D[2019-02-01]
iex> Cldr.Calendar.next ~D[2019-01-01], :quarter
~D[2019-04-01]
iex> Cldr.Calendar.next ~D[2019-01-01], :year
~D[2020-01-01]
@spec plus(integer(), integer()) :: integer()
@spec plus(Calendar.date(), Cldr.Calendar.Duration.t()) :: Calendar.date()
Adds a duration to a date
arguments
Arguments
dateis any map that conforms toCalendar.date()durationis any duration returned byCldr.Calendar.Duration.new!/2optionsis a Keyword list of options
options
Options
- Options are those applicable to
Cldr.Calendar.plus/4
returns
Returns
- A
dateadvanced by the duration
examples
Examples
iex> Cldr.Calendar.plus ~D[2020-01-01],
...> Cldr.Calendar.Duration.new!(~D[2020-01-01], ~D[2020-02-01])
~D[2020-02-01]
iex> Cldr.Calendar.plus ~D[2020-01-01],
...> Cldr.Calendar.Duration.new!(~D[2020-01-01], ~D[2020-01-02])
~D[2020-01-02]
iex> Cldr.Calendar.plus ~D[2020-01-01],
...> Cldr.Calendar.Duration.new!(~D[2020-01-01], ~D[2020-02-01])
~D[2020-02-01]
iex> Cldr.Calendar.plus ~D[2020-01-01],
...> Cldr.Calendar.Duration.new!(~D[2020-01-01], ~D[2021-02-01])
~D[2021-02-01]
@spec plus(Calendar.date() | Date.Range.t(), atom(), integer(), Keyword.t()) :: Calendar.date()
Increments a date or date range by an integer amount of a date period (year, quarter, month, week or day).
arguments
Arguments
date_or_date_rangeis anyDate.torDate.Range.tperiodis:years,:quarters,:months,:weeksor:daysoptionsis a Keyword list of options
options
Options
:coerceis a boolean which, when set totruewill coerce the month and/or day to be a valid date. This affects,for example, moving to the previous month from~D[2019-03-31]. Since there is no date~D[2019-02-31]this would normally return{:error, :invalid_date}. Settingcoerce: trueit will return~D[2019-02-28].
returns
Returns
When a Date.t is passed, a Date.t is
returned. When a Date.Range.t is passed
a Date.Range.t is returned.
examples
Examples
iex> Cldr.Calendar.plus ~D[2016-02-29], :days, 1
~D[2016-03-01]
iex> Cldr.Calendar.plus ~D[2019-03-01], :months, 1
~D[2019-04-01]
iex> Cldr.Calendar.plus ~D[2016-02-29], :days, 1
~D[2016-03-01]
iex> Cldr.Calendar.plus ~D[2019-02-28], :days, 1
~D[2019-03-01]
iex> Cldr.Calendar.plus ~D[2019-03-01], :months, 1
~D[2019-04-01]
iex> Cldr.Calendar.plus ~D[2019-03-01], :quarters, 1
~D[2019-06-01]
iex> Cldr.Calendar.plus ~D[2019-03-01], :years, 1
~D[2020-03-01]
Returns the previous date or date range for a date period (year, quarter, month, week or day).
arguments
Arguments
date_or_date_rangeis anyDate.torDate.Range.tperiodis:year,:quarter,:month,:weekor:dayoptionsis a Keyword list of options that is passed toplus/4orminus/4
returns
Returns
When a Date.t is passed, a Date.t is
returned. When a Date.Range.t is passed
a Date.Range.t is returned.
examples
Examples
iex> Cldr.Calendar.previous ~D[2019-01-01], :day
~D[2018-12-31]
iex> Cldr.Calendar.previous ~D[2019-01-01], :quarter
~D[2018-10-01]
iex> Cldr.Calendar.previous ~D[2019-01-01], :month
~D[2018-12-01]
iex> Cldr.Calendar.previous ~D[2019-01-01], :year
~D[2018-01-01]
Returns the quarter number for
a date.
arguments
Arguments
dateis anyDate.t()
returns
Returns
- a the quarter of the year as an integer
examples
Examples
iex> Cldr.Calendar.quarter_of_year ~D[2019-01-01]
1
iex> Cldr.Calendar.quarter_of_year Cldr.Calendar.first_day_of_year(2019, Cldr.Calendar.NRF)
1
iex> Cldr.Calendar.quarter_of_year Cldr.Calendar.last_day_of_year(2019, Cldr.Calendar.NRF)
4
@spec saturday() :: 6
Returns the ordinal day number representing Saturday.
@spec sunday() :: 7
Returns the ordinal day number representing Sunday.
@spec thursday() :: 4
Returns the ordinal day number representing Thursday.
@spec tuesday() :: 2
Returns the ordinal day number representing Tuesday.
Validates if the argument is a Cldr.Calendar calendar module.
arguments
Arguments
calendar_moduleis a module that implements theCldr.Calendarbehaviour
returns
Returns
{:ok, calendar_module}or{:error, {exception, reason}}
examples
Examples
iex> Cldr.Calendar.validate_calendar Cldr.Calendar.Gregorian
{:ok, Cldr.Calendar.Gregorian}
iex> Cldr.Calendar.validate_calendar :not_a_calendar
{:error,
{Cldr.InvalidCalendarModule, ":not_a_calendar is not a calendar module."}}
@spec wednesday() :: 3
Returns the ordinal day number representing Wednesday.
@spec week_of_month(Date.t()) :: {Calendar.month(), week()}
Returns the {month, week_number}
for a date.
The nature of a week depends on the calendar configuration and therefore some results may be surprising. For example the date of December 31st 2018 is actually in month one of the ISO Week calendar of 2019.
arguments
Arguments
dateis anyDate.t()
returns
Returns
a tuple of the form
{month, week}or{:error, :not_defined}if the calendar does not support the concept of weeks.
examples
Examples
iex> Cldr.Calendar.week_of_month(~D[2019-01-01])
{1, 1}
iex> Cldr.Calendar.week_of_month(~D[2018-12-31])
{1, 1}
iex> Cldr.Calendar.week_of_month(~D[2019-01-01 Cldr.Calendar.BasicWeek])
{1, 1}
iex> Cldr.Calendar.week_of_month(~D[2018-12-31 Cldr.Calendar.BasicWeek])
{12, 5}
iex> Cldr.Calendar.week_of_month(~D[2018-12-31 Cldr.Calendar.Julian])
{:error, :not_defined}
@spec week_of_year(Date.t()) :: {Calendar.year(), week()}
Returns the {year, week_number}
for a date.
arguments
Arguments
dateis anyDate.t()
returns
Returns
a the week of the year as an integer or
{:error, :not_defined}if the calendar does not support the concept of weeks.
examples
Examples
iex> Cldr.Calendar.week_of_year ~D[2019-01-01]
{2019, 1}
iex> Cldr.Calendar.week_of_year ~D[2019-12-01]
{2019, 48}
iex> Cldr.Calendar.week_of_year ~D[2019-52-01 Cldr.Calendar.NRF]
{2019, 52}
iex> Cldr.Calendar.week_of_year ~D[2019-26-01 Cldr.Calendar.NRF]
{2019, 26}
iex> Cldr.Calendar.week_of_year ~D[2019-12-01 Cldr.Calendar.Julian]
{:error, :not_defined}
Returns whether a given date is a weekday.
Weekdays are locale-specific and depend on the policies of a given territory (country).
arguments
Arguments
dateis anyDate.t()optionsis a Keyword list of options
options
Options
:localeis any locale or locale name validated byCldr.validate_locale/2. The default isCldr.get_locale()which returns the locale set for the current process:territoryis any valid ISO-3166-2 territory that is validated byCldr.validate_territory/1:backendis anyCldrbackend module. See the backend configuration documentation for further information. The default isCldr.Calendar.Backend.Defaultwhich configures only theenlocale.
notes
Notes
When identifying which territory context within which to determine whether a given day is a weekday or not the following order applies:
A territory specified by the
:territoryoptionThe territory defined as part of the
:localeoptionThe territory defined as part of the current processes default locale.
examples
Examples
# The default locale for `Cldr` is `en-001` for which
# the territory is `001` (the world). The weekdays
# for `001` are Monday to Friday
iex> Cldr.Calendar.weekday? ~D[2019-03-23], locale: :en
false
iex> Cldr.Calendar.weekday? ~D[2019-03-23], territory: "IS"
false
# Saturday is a weekday in India
iex> Cldr.Calendar.weekday? ~D[2019-03-23], locale: :"en-IN", backend: MyApp.Cldr
true
# Friday is not a weekday in Saudi Arabia
iex> Cldr.Calendar.weekday? ~D[2019-03-22], locale: :"ar-SA", backend: MyApp.Cldr
false
# Friday is not a weekday in Israel
iex> Cldr.Calendar.weekday? ~D[2019-03-22], locale: :he, backend: MyApp.Cldr
false
Returns a list of the days of the week that are considered a weekend for a given territory (country)
arguments
Arguments
territoryis any valid ISO3166-2 code
returns
Returns
- A list of integers representing the days of the week that are week days
notes
Notes
The list of days may not my monotonic. See the example for Saudi Arabia below.
examples
Examples
iex> Cldr.Calendar.weekdays("US")
[1, 2, 3, 4, 5]
iex> Cldr.Calendar.weekdays("IN")
[1, 2, 3, 4, 5, 6]
iex> Cldr.Calendar.weekdays("SA")
[1, 2, 3, 4, 7]
iex> Cldr.Calendar.weekdays("yy")
{:error, {Cldr.UnknownTerritoryError, "The territory \"yy\" is unknown"}}
Returns a list of the days of the week that are considered a weekend for a given territory (country)
arguments
Arguments
territoryis any valid ISO3166-2 code
returns
Returns
- A list of integers representing the days of the week that are weekend days
examples
Examples
iex> Cldr.Calendar.weekend("US")
[6, 7]
iex> Cldr.Calendar.weekend("IN")
[7]
iex> Cldr.Calendar.weekend("SA")
[5, 6]
iex> Cldr.Calendar.weekend("yy")
{:error, {Cldr.UnknownTerritoryError, "The territory \"yy\" is unknown"}}
Returns whether a given date is a weekend day.
Weekend days are locale-specific and depend on the policies of a given territory (country).
arguments
Arguments
dateis anyDate.t()optionsis a Keyword list of options
options
Options
:localeis any locale or locale name validated byCldr.validate_locale/2. The default isCldr.get_locale()which returns the locale set for the current process:territoryis any valid ISO-3166-2 territory that is validated byCldr.validate_territory/1:backendis anyCldrbackend module. See the backend configuration documentation for further information. The default isCldr.Calendar.Backend.Defaultwhich configures only theenlocale.
notes
Notes
When identifying which territory context within which to determine whether a given day is a weekend or not the following order applies:
A territory specified by the
:territoryoptionThe territory defined as part of the
:localeoptionThe territory defined as part of the current processes default locale.
examples
Examples
# The default locale for `Cldr` is `en-001` for which
# the territory is `001` (the world). The weekend
# for `001` is Saturday and Sunday
iex> Cldr.Calendar.weekend? ~D[2019-03-23]
true
iex> Cldr.Calendar.weekend? ~D[2019-03-23], locale: :en
true
iex> Cldr.Calendar.weekend? ~D[2019-03-23], territory: "IS"
true
# In India the official weekend is only Sunday
iex> Cldr.Calendar.weekend? ~D[2019-03-23], locale: "en-IN", backend: MyApp.Cldr
false
# In Israel the weekend starts on Friday
iex> Cldr.Calendar.weekend? ~D[2019-03-22], locale: :he, backend: MyApp.Cldr
true
# As it also does in Saudia Arabia
iex> Cldr.Calendar.weekend? ~D[2019-03-22], locale: :"ar-SA", backend: MyApp.Cldr
true
# Sunday is not a weekend day in Saudi Arabia
iex> Cldr.Calendar.weekend? ~D[2019-03-24], locale: :"ar-SA", backend: MyApp.Cldr
false
Returns the number of weeks in a year.
arguments
Arguments
- Either a
Date.t()or an integer year a calendar name
returns
Returns
- In integer number of weeks in a year
examples
Examples
iex> Cldr.Calendar.weeks_in_year ~D[2026-W01-1 Cldr.Calendar.ISOWeek]
53
iex> Cldr.Calendar.weeks_in_year ~D[2019-01-01]
52
iex> Cldr.Calendar.weeks_in_year ~D[2020-01-01]
52
iex> Cldr.Calendar.weeks_in_year 2020, Cldr.Calendar.ISOWeek
53
@spec weeks_in_year(Calendar.year(), calendar()) :: week()
Returns the number of days in n weeks
example
Example
iex> Cldr.Calendar.weeks_to_days(2)
14
@spec year_of_era(Date.t()) :: {Calendar.day(), Calendar.era()}
Returns the {year_of_era, era} for
a date.
This function differs slightly
from Date.year_of_era/1. See the notes
below
arguments
Arguments
dateis anyDate.t()
returns
Returns
- a the year since the start of the era and the era of the year as a tuple
notes
Notes
Unlike
Date.year_of_era/1, this function supports eras that change part way through the calendar year. This is common in the Japanese calendar where the eras change when a new emperor is ordained which can happen at any time of year. Therefore this function is consistent withDate.year_of_era/1for the Gregorian and related calendars, but returns a different (and more accurate) result for the Japanese calendar.This is also true for fiscal year calendars that start on a day other than January 1st. The year of era will depend on whether the calendar was configured with
year: :beginning,year: :endingoryear: :majority
examples
Examples
iex> Cldr.Calendar.year_of_era ~D[2019-01-01]
{2019, 1}
iex> Cldr.Calendar.year_of_era Cldr.Calendar.first_day_of_year(2019, Cldr.Calendar.NRF)
{2019, 1}
iex> Cldr.Calendar.year_of_era Cldr.Calendar.last_day_of_year(2019, Cldr.Calendar.NRF)
{2019, 1}