View Source Bonfire.Common.Localise.Cldr.Time (Bonfire v0.9.10-classic-beta.156)

Summary

Functions

Formats a time according to a format string as defined in CLDR and described in TR35

Formats a time according to a format string as defined in CLDR and described in TR35 or raises an exception.

Functions

Link to this function

to_string(time, options \\ [])

View Source
@spec to_string(Cldr.Calendar.any_date_time(), Keyword.t()) ::
  {:ok, String.t()} | {:error, {module(), String.t()}}

Formats a time according to a format string as defined in CLDR and described in TR35

Returns

  • {:ok, formatted_time} or

  • {:error, reason}.

Arguments

  • time is a Time.t/0 struct or any map that contains one or more of the keys :hour, :minute, :second and optionally :microsecond, :time_zone, :zone_abbr, :utc_offset and :std_offset. *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 times (that is, times having:hour,:minuteand:secondfields). The default for partial times is to derive a candidate format from the time and find the best match from the formats returned byCldr.Time.available_formats/2. See [here](README.md#date-time-and-datetime-localization-formats) 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.Time.available_formats/3to see which formats have these variants. *period: :variantwill use a variant for the time period and flexible time period if one is available in the locale. For example, in the:enlocaleperiod: :variant` will return "pm" instead of "PM". ## Examples iex> Bonfire.Common.Localise.Cldr.Time.to_string(~T[07:35:13.215217]) {:ok, "7:35:13 AM"} iex> Bonfire.Common.Localise.Cldr.Time.to_string(~T[07:35:13.215217], format: :short) {:ok, "7:35 AM"} iex> Bonfire.Common.Localise.Cldr.Time.to_string(~T[07:35:13.215217], format: :short, period: :variant) {:ok, "7:35 am"} iex> Bonfire.Common.Localise.Cldr.Time.to_string(~T[07:35:13.215217], format: :medium, locale: :fr) {:ok, "07:35:13"} iex> Bonfire.Common.Localise.Cldr.Time.to_string(~T[07:35:13.215217], format: :medium) {:ok, "7:35:13 AM"} iex> {:ok, date_time} = DateTime.from_naive(~N[2000-01-01 23:59:59.0], "Etc/UTC") iex> Bonfire.Common.Localise.Cldr.Time.to_string(date_time, format: :long) {:ok, "11:59:59 PM UTC"} # A partial time with a best match CLDR-defined format iex> Bonfire.Common.Localise.Cldr.Time.to_string(%{hour: 23, minute: 11}) {:ok, "11:11 PM"} # Sometimes the available time fields can't be mapped to an available # CLDR defined format. iex> Bonfire.Common.Localise.Cldr.Time.to_string(%{minute: 11}) {:error, {Cldr.DateTime.UnresolvedFormat, "No available format resolved for :m"}}
Link to this function

to_string!(time, options \\ [])

View Source
@spec to_string!(Cldr.Calendar.any_date_time(), Keyword.t()) ::
  String.t() | no_return()

Formats a time according to a format string as defined in CLDR and described in TR35 or raises an exception.

Arguments

  • time is a Time.t/0 struct or any map that contains one or more of the keys :hour, :minute, :second and optionally :microsecond, :time_zone, :zone_abbr, :utc_offset and :std_offset. *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 times (that is, times having:hour,:minuteand:secondfields). The default for partial times is to derive a candidate format from the time and find the best match from the formats returned byCldr.Time.available_formats/2. See [here](README.md#date-time-and-datetime-localization-formats) 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.Time.available_formats/3to see which formats have these variants. *period: :variantwill use a variant for the time period and flexible time period if one is available in the locale. For example, in the:enlocaleperiod: :variantwill return "pm" instead of "PM". ## Returns *formatted_time_string` or * raises an exception. ## Examples iex> Bonfire.Common.Localise.Cldr.Time.to_string!(~T[07:35:13.215217]) "7:35:13 AM" iex> Bonfire.Common.Localise.Cldr.Time.to_string!(~T[07:35:13.215217], format: :short) "7:35 AM" iex> Bonfire.Common.Localise.Cldr.Time.to_string!(~T[07:35:13.215217], format: :short, period: :variant) "7:35 am" iex> Bonfire.Common.Localise.Cldr.Time.to_string!(~T[07:35:13.215217], format: :medium, locale: :fr) "07:35:13" iex> Bonfire.Common.Localise.Cldr.Time.to_string!(~T[07:35:13.215217], format: :medium) "7:35:13 AM" iex> {:ok, datetime} = DateTime.from_naive(~N[2000-01-01 23:59:59.0], "Etc/UTC") iex> Bonfire.Common.Localise.Cldr.Time.to_string! datetime, format: :long "11:59:59 PM UTC" # A partial time with a best match CLDR-defined format iex> Bonfire.Common.Localise.Cldr.Time.to_string!(%{hour: 23, minute: 11}) "11:11 PM"