View Source Cldr.Time (Cldr Dates & Times v2.20.3)

Provides localization and formatting of a time.

A time is any Time.t/0 struct or any map with one or more of the keys :hour, :minute, :second and optionally :time_zone, :zone_abbr, :utc_offset, :std_offset and :microsecond.

Cldr.Time provides support for the built-in calendar Calendar.ISO or any calendars defined with ex_cldr_calendars

CLDR provides standard format strings for Time which are represented by the names :short, :medium, :long and :full. This allows for locale-independent formatting since each locale may define the underlying format string as appropriate.

Summary

Functions

Return the preferred time format for a locale.

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.

Functions

Link to this function

available_formats(locale \\ Cldr.get_locale(), calendar \\ Cldr.Calendar.default_cldr_calendar(), backend \\ Cldr.Date.default_backend())

View Source
@spec available_formats(
  Cldr.Locale.locale_reference(),
  Cldr.Calendar.calendar(),
  Cldr.backend()
) :: {:ok, map()} | {:error, {atom(), String.t()}}

Returns a map of the available date formats for a given locale and calendar.

Arguments

Examples:

iex> Cldr.Time.available_formats(:en)
{:ok,
 %{
   h: %{unicode: "h a", ascii: "h a"},
   hms: %{unicode: "h:mm:ss a", ascii: "h:mm:ss a"},
   ms: "mm:ss",
   H: "HH",
   Hm: "HH:mm",
   Hms: "HH:mm:ss",
   Hmsv: "HH:mm:ss v",
   Hmv: "HH:mm v",
   hm: %{unicode: "h:mm a", ascii: "h:mm a"},
   hmsv: %{unicode: "h:mm:ss a v", ascii: "h:mm:ss a v"},
   hmv: %{unicode: "h:mm a v", ascii: "h:mm a v"}
 }}
Link to this function

formats(locale \\ Cldr.get_locale(), calendar \\ Cldr.Calendar.default_cldr_calendar(), backend \\ Cldr.Date.default_backend())

View Source

Returns a map of the standard time formats for a given locale and calendar.

Arguments

Examples:

iex> Cldr.Time.formats(:en, :gregorian, MyApp.Cldr)
{:ok,
 %Cldr.Time.Formats{
   short: "h:mm a",
   medium: "h:mm:ss a",
   long: "h:mm:ss a z",
   full: "h:mm:ss a zzzz"
 }}

iex> Cldr.Time.formats(:en, :buddhist, MyApp.Cldr)
{:ok,
 %Cldr.Time.Formats{
   short: "h:mm a",
   medium: "h:mm:ss a",
   long: "h:mm:ss a z",
   full: "h:mm:ss a zzzz"
 }}
Link to this function

hour_format_from_locale(locale)

View Source

Return the preferred time format for a locale.

Arguments

Returns

  • The hour format as an atom to be used for localization purposes. The return value is used as a function name in Cldr.DateTime.Formatter

Notes

  • The hc key of the u extension is honoured and will override the default preferences for a locale or territory. See the last example below.

  • Different locales and territories present the hour of day in different ways. These are represented in Cldr.DateTime.Formatter in the following way:

SymbolMidn.MorningNoonAfternoonMidn.
h121...11121...1112
K01...1101...110
H01...111213...230
k241...111213...2324

Examples

iex> Cldr.Time.hour_format_from_locale("en-AU")
:h12

iex> Cldr.Time.hour_format_from_locale("fr")
:h23

iex> Cldr.Time.hour_format_from_locale("fr-u-hc-h12")
:h12
Link to this function

hour_format_from_locale(locale_name, backend \\ Cldr.Date.default_backend())

View Source
Link to this macro

is_full_time(time)

View Source (macro)
Link to this function

to_string(time, backend \\ Cldr.Date.default_backend(), options \\ [])

View Source
@spec to_string(Cldr.Calendar.any_date_time(), Cldr.backend(), options()) ::
  {:ok, String.t()} | {:error, {module(), String.t()}}
@spec to_string(Cldr.Calendar.any_date_time(), options(), []) ::
  {: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.

  • backend is any module that includes use Cldr and therefore is a Cldr backend module. The default is Cldr.default_backend!/0.

  • options is a keyword list of options for formatting.

Options

  • :format is one of :short, :medium, :long, :full, or a format id or a format string. The default is :medium for full times (that is, times having :hour, :minute and :second fields). The default for partial times is to derive a candidate format from the time and find the best match from the formats returned by Cldr.Time.available_formats/3. See here for more information about specifying formats.

  • :locale any locale returned by Cldr.known_locale_names/1. The default is Cldr.get_locale/0.

  • :number_system a number system into which the formatted date digits should be transliterated.

  • :prefer expresses the preference for one of the possible alternative sub-formats. See the variant preference notes below.

  • period: :variant will use a variant for the time period and flexible time period if one is available in the locale. For example, in the :en locale, period: :variant will return "pm" instead of "PM".

Variant Preference

  • A small number of formats have one of two different alternatives, each with their own preference specifier. The preferences are specified with the :prefer option to Cldr.Date.to_string/3. The preference is expressed as an atom, or a list of one or two atoms with one atom being either :unicode or :ascii and one atom being either :default or :variant.
    • Some formats (at the time of publishng only time formats but that may change in the future) have :unicode and :ascii versions of the format. The difference is the use of ascii space (0x20) as a separateor in the :ascii verison whereas the :unicode version may use non-breaking or other space characters. The default is :unicode and this is the strongly preferred option. The :ascii format is primarily to support legacy use cases and is not recommended. See Cldr.Time.available_formats/3 to see which formats have these variants.

    • Some formats (at the time of publishing, only date and datetime formats) have :default and :variant versions of the format. These variant formats are only included in a small number of locales. For example, the :"en-CA" locale, which has a :default format respecting typical Canadian formatting and a :variant that is more closely aligned to US formatting. The default is :default.

Examples

iex> Cldr.Time.to_string(~T[07:35:13.215217], MyApp.Cldr)
{:ok, "7:35:13 AM"}

iex> Cldr.Time.to_string(~T[07:35:13.215217], MyApp.Cldr, format: :short)
{:ok, "7:35 AM"}

iex> Cldr.Time.to_string(~T[07:35:13.215217], MyApp.Cldr, format: :short, period: :variant)
{:ok, "7:35 am"}

iex> Cldr.Time.to_string(~T[07:35:13.215217], MyApp.Cldr, format: :medium, locale: "fr")
{:ok, "07:35:13"}

iex> Cldr.Time.to_string(~T[07:35:13.215217], MyApp.Cldr, format: :medium)
{:ok, "7:35:13 AM"}

iex> {:ok, datetime} = DateTime.from_naive(~N[2000-01-01 23:59:59.0], "Etc/UTC")
iex> Cldr.Time.to_string datetime, MyApp.Cldr, format: :long
{:ok, "11:59:59 PM UTC"}

# A partial time with a best match CLDR-defined format
iex> 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> Cldr.Time.to_string(%{minute: 11})
{:error,
 {Cldr.DateTime.UnresolvedFormat, "No available format resolved for :m"}}
Link to this function

to_string!(time, backend \\ Cldr.Date.default_backend(), options \\ [])

View Source
@spec to_string!(Cldr.Calendar.any_date_time(), Cldr.backend(), options()) ::
  String.t() | no_return()
@spec to_string!(Cldr.Calendar.any_date_time(), options(), []) ::
  String.t() | no_return()

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

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.

  • backend is any module that includes use Cldr and therefore is a Cldr backend module. The default is Cldr.default_backend!/0.

  • options is a keyword list of options for formatting.

Options

  • :format is one of :short, :medium, :long, :full, or a format id or a format string. The default is :medium for full times (that is, times having :hour, :minute and :second fields). The default for partial times is to derive a candidate format from the time and find the best match from the formats returned by Cldr.Time.available_formats/3. See here for more information about specifying formats.

  • locale is any valid locale name returned by Cldr.known_locale_names/0 or a Cldr.LanguageTag.t/0 struct. The default is Cldr.get_locale/0.

  • :number_system a number system into which the formatted time digits should be transliterated.

  • :prefer expresses the preference for one of the possible alternative sub-formats. See the variant preference notes below.

  • period: :variant will use a variant for the time period and flexible time period if one is available in the locale. For example, in the :en locale period: :variant will return "pm" instead of "PM".

Variant Preference

  • A small number of formats have one of two different alternatives, each with their own preference specifier. The preferences are specified with the :prefer option to Cldr.Date.to_string/3. The preference is expressed as an atom, or a list of one or two atoms with one atom being either :unicode or :ascii and one atom being either :default or :variant.
    • Some formats (at the time of publishng only time formats but that may change in the future) have :unicode and :ascii versions of the format. The difference is the use of ascii space (0x20) as a separateor in the :ascii verison whereas the :unicode version may use non-breaking or other space characters. The default is :unicode and this is the strongly preferred option. The :ascii format is primarily to support legacy use cases and is not recommended. See Cldr.Time.available_formats/3 to see which formats have these variants.

    • Some formats (at the time of publishing, only date and datetime formats) have :default and :variant versions of the format. These variant formats are only included in a small number of locales. For example, the :"en-CA" locale, which has a :default format respecting typical Canadian formatting and a :variant that is more closely aligned to US formatting. The default is :default.

Returns

  • formatted_time_string or

  • raises an exception.

Examples

iex> Cldr.Time.to_string!(~T[07:35:13.215217], MyApp.Cldr)
"7:35:13 AM"

iex> Cldr.Time.to_string!(~T[07:35:13.215217], MyApp.Cldr, format: :short)
"7:35 AM"

iex> Cldr.Time.to_string!(~T[07:35:13.215217], MyApp.Cldr, format: :short, period: :variant)
"7:35 am"

iex> Cldr.Time.to_string!(~T[07:35:13.215217], MyApp.Cldr, format: :medium, locale: "fr")
"07:35:13"

iex> Cldr.Time.to_string!(~T[07:35:13.215217], MyApp.Cldr, format: :medium)
"7:35:13 AM"

iex> {:ok, datetime} = DateTime.from_naive(~N[2000-01-01 23:59:59.0], "Etc/UTC")
iex> Cldr.Time.to_string!(datetime, MyApp.Cldr, format: :long)
"11:59:59 PM UTC"

# A partial time with a best match CLDR-defined format
iex> Cldr.Time.to_string!(%{hour: 23, minute: 11})
"11:11 PM"