Bonfire.Common.Localise.Cldr.Date (Bonfire v1.0.1-social-alpha.9)
View SourceSummary
Functions
@spec to_string(Cldr.Calendar.any_date_time(), Keyword.t()) :: {:ok, String.t()} | {:error, {module(), String.t()}}
Formats a date according to a format string as defined in CLDR and described in TR35.
Arguments
dateis aDate.t/0struct or any map that contains one or more of the keys:year,:month,:dayand optionally:calendar.optionsis a keyword list of options for formatting.
Options
:formatis one of:short,:medium,:long,:full, or a format id or a format string. The default is:mediumfor full dates (that is, dates having:year,:month,:dayand:calendarfields). The default for partial dates is to derive a candidate format id from the date and find the best match from the formats returned byCldr.Date.available_formats/3. See here for more information about specifying formats.:localeany locale returned byCldr.known_locale_names/1. The default isCldr.get_locale/0.:number_systema number system into which the formatted date digits should be transliterated.:preferis either:unicode(the default) or:ascii. A small number of formats have two variants - one using Unicode spaces (typically non-breaking space) and another using only ASCII whitespace. The:asciiformat is primarily to support legacy use cases and is not recommended. SeeCldr.Date.available_formats/3to see which formats have these variants. Currently no date-specific formats have such variants but they may in the future.:erawhich, if set to:variant, will use a variant for the era if one is available in the requested locale. In the:enlocale, for example,era: :variantwill returnCEinstead ofADandBCEinstead ofBC.
Returns
{:ok, formatted_string}or{:error, reason}
Examples
# Full dates have the default format `:medium`
iex> Bonfire.Common.Localise.Cldr.Date.to_string(~D[2017-07-10], locale: :en)
{:ok, "Jul 10, 2017"}
iex> Bonfire.Common.Localise.Cldr.Date.to_string(~D[2017-07-10], format: :medium, locale: :en)
{:ok, "Jul 10, 2017"}
iex> Bonfire.Common.Localise.Cldr.Date.to_string(~D[2017-07-10], format: :full, locale: :en)
{:ok, "Monday, July 10, 2017"}
iex> Bonfire.Common.Localise.Cldr.Date.to_string(~D[2017-07-10], format: :short, locale: :en)
{:ok, "7/10/17"}
iex> Bonfire.Common.Localise.Cldr.Date.to_string(~D[2017-07-10], format: :short, locale: "fr")
{:ok, "10/07/2017"}
# A partial date with a derived "best match" format
iex> Bonfire.Common.Localise.Cldr.Date.to_string(%{year: 2024, month: 6}, locale: "fr")
{:ok, "6/2024"}
# A partial date with a best match CLDR-defined format
iex> Bonfire.Common.Localise.Cldr.Date.to_string(%{year: 2024, month: 6}, format: :yMMM, locale: "fr")
{:ok, "juin 2024"}
# Sometimes the available date fields can't be mapped to an available
# CLDR defined format.
iex> Bonfire.Common.Localise.Cldr.Date.to_string(%{year: 2024, day: 3}, locale: "fr")
{:error,
{Cldr.DateTime.UnresolvedFormat, "No available format resolved for :dy"}}
@spec to_string!(Cldr.Calendar.any_date_time(), Keyword.t()) :: String.t() | no_return()
Formats a date according to a format string as defined in CLDR and described in TR35 or raises an exception.
Arguments
dateis aDate.t/0struct or any map that contains one or more of the keys:year,:month,:dayand optionally:calendar.optionsis a keyword list of options for formatting.
Options
:formatis one of:short,:medium,:long,:full, or a format id or a format string. The default is:mediumfor full dates (that is, dates having:year,:month,:dayand:calendarfields). The default for partial dates is to derive a candidate format id from the date and find the best match from the formats returned byCldr.Date.available_formats/3. See here for more information about specifying formats.:localeany locale returned byCldr.known_locale_names/1. The default isCldr.get_locale/0.:number_systema number system into which the formatted date digits should be transliterated.:preferis either:unicode(the default) or:ascii. A small number of formats have two variants - one using Unicode spaces (typically non-breaking space) and another using only ASCII whitespace. The:asciiformat is primarily to support legacy use cases and is not recommended. SeeCldr.Date.available_formats/3to see which formats have these variants. Currently no date-specific formats have such variants but they may in the future.:erawhich, if set to:variant, will use a variant for the era if one is available in the requested locale. In the:enlocale, for example,era: :variantwill returnCEinstead ofADandBCEinstead ofBC.
Returns
formatted_dateorraises an exception.
Examples
iex> Bonfire.Common.Localise.Cldr.Date.to_string!(~D[2017-07-10], locale: :en)
"Jul 10, 2017"
iex> Bonfire.Common.Localise.Cldr.Date.to_string!(~D[2017-07-10], format: :medium, locale: :en)
"Jul 10, 2017"
iex> Bonfire.Common.Localise.Cldr.Date.to_string!(~D[2017-07-10], format: :full, locale: :en)
"Monday, July 10, 2017"
iex> Bonfire.Common.Localise.Cldr.Date.to_string!(~D[2017-07-10], format: :short, locale: :en)
"7/10/17"
iex> Bonfire.Common.Localise.Cldr.Date.to_string!(~D[2017-07-10], format: :short, locale: "fr")
"10/07/2017"
# A partial date with a derived "best match" format
iex> Bonfire.Common.Localise.Cldr.Date.to_string!(%{year: 2024, month: 6}, locale: "fr")
"6/2024"
# A partial date with a best match CLDR-defined format
iex> Bonfire.Common.Localise.Cldr.Date.to_string!(%{year: 2024, month: 6}, format: :yMMM, locale: "fr")
"juin 2024"