Cldr.Time (Cldr Dates & Times v2.24.0)
View SourceProvides 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
Returns a map of the available date formats for a given locale and calendar.
Returns a map of the standard time formats for a given locale and calendar.
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
@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
localeis any locale returned byCldr.known_locale_names/0or aCldr.LanguageTag.t/0. The default isCldr.get_locale/0.calendaris any calendar returned byCldr.DateTime.Format.calendars_for/1The default is:gregorian.backendis any module that includesuse Cldrand therefore is aCldrbackend module. The default isCldr.default_backend/0.
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"}
}}
@spec formats( Cldr.Locale.locale_reference(), Cldr.Calendar.calendar(), Cldr.backend() ) :: {:ok, Cldr.DateTime.Format.standard_formats()} | {:error, {atom(), String.t()}}
Returns a map of the standard time formats for a given locale and calendar.
Arguments
localeis any locale returned byCldr.known_locale_names/0or aCldr.LanguageTag.t/0. The default isCldr.get_locale/0.calendaris any calendar returned byCldr.DateTime.Format.calendars_for/1The default is:gregorian.backendis any module that includesuse Cldrand therefore is aCldrbackend module. The default isCldr.default_backend/0.
Examples:
iex> Cldr.Time.formats(:en, :gregorian, MyApp.Cldr)
{
:ok,
%Cldr.Time.Formats{
full: %{unicode: "h:mm:ss a zzzz", ascii: "h:mm:ss a zzzz"},
long: %{unicode: "h:mm:ss a z", ascii: "h:mm:ss a z"},
medium: %{unicode: "h:mm:ss a", ascii: "h:mm:ss a"},
short: %{unicode: "h:mm a", ascii: "h:mm a"}
}
}
iex> Cldr.Time.formats(:en, :buddhist, MyApp.Cldr)
{
:ok,
%Cldr.Time.Formats{
full: %{unicode: "h:mm:ss a zzzz", ascii: "h:mm:ss a zzzz"},
long: %{unicode: "h:mm:ss a z", ascii: "h:mm:ss a z"},
medium: %{unicode: "h:mm:ss a", ascii: "h:mm:ss a"},
short: %{unicode: "h:mm a", ascii: "h:mm a"}
}
}
Return the preferred time format for a locale.
Arguments
language_tagis any language tag returned byCldr.Locale.new/2or anylocale_namereturned byCldr.known_locale_names/1
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
hckey of theuextension 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.Formatterin the following way:
| Symbol | Midn. | Morning | Noon | Afternoon | Midn. |
|---|---|---|---|---|---|
| h | 12 | 1...11 | 12 | 1...11 | 12 |
| K | 0 | 1...11 | 0 | 1...11 | 0 |
| H | 0 | 1...11 | 12 | 13...23 | 0 |
| k | 24 | 1...11 | 12 | 13...23 | 24 |
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
@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
timeis aTime.t/0struct or any map that contains one or more of the keys:hour,:minute,:secondand optionally:microsecond,:time_zone,:zone_abbr,:utc_offsetand:std_offset.backendis any module that includesuse Cldrand therefore is aCldrbackend module. The default isCldr.default_backend!/0.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/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 datetime digits should be transliterated. SeeCldr.known_number_systems/0. The default is the number system associated with the:locale.:separatorsselects which of the available symbol sets should be used when formatting fractional seconds (format characterS). The default is:standard. Some limited locales have an alternative:usvariant that can be used. SeeCldr.Number.Symbol.number_symbols_for/3for the symbols supported for a given locale and number system.:preferexpresses the preference for one of the possible alternative sub-formats. See the variant preference notes below.period: :variantwill use a variant for the time period and flexible time period if one is available in the locale. For example, in the:enlocale,period: :variantwill 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
:preferoption toCldr.Date.to_string/3. The preference is expressed as an atom, or a list of one or two atoms with one atom being either:unicodeor:asciiand one atom being either:defaultor:variant.Some formats (at the time of publishng only time formats but that may change in the future) have
:unicodeand:asciiversions of the format. The difference is the use of ascii space (0x20) as a separateor in the:asciiverison whereas the:unicodeversion may use non-breaking or other space characters. The default is:unicodeand this is the strongly preferred option. The:asciiformat is primarily to support legacy use cases and is not recommended. SeeCldr.Time.available_formats/3to see which formats have these variants.Some formats (at the time of publishing, only date and datetime formats) have
:defaultand:variantversions of the format. These variant formats are only included in a small number of locales. For example, the:"en-CA"locale, which has a:defaultformat respecting typical Canadian formatting and a:variantthat 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"}}
@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
timeis aTime.t/0struct or any map that contains one or more of the keys:hour,:minute,:secondand optionally:microsecond,:time_zone,:zone_abbr,:utc_offsetand:std_offset.backendis any module that includesuse Cldrand therefore is aCldrbackend module. The default isCldr.default_backend!/0.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/3. See here for more information about specifying formats.localeis any valid locale name returned byCldr.known_locale_names/0or aCldr.LanguageTag.t/0struct. The default isCldr.get_locale/0.:number_systema number system into which the formatted time digits should be transliterated.:preferexpresses the preference for one of the possible alternative sub-formats. See the variant preference notes below.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".
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
:preferoption toCldr.Date.to_string/3. The preference is expressed as an atom, or a list of one or two atoms with one atom being either:unicodeor:asciiand one atom being either:defaultor:variant.Some formats (at the time of publishng only time formats but that may change in the future) have
:unicodeand:asciiversions of the format. The difference is the use of ascii space (0x20) as a separateor in the:asciiverison whereas the:unicodeversion may use non-breaking or other space characters. The default is:unicodeand this is the strongly preferred option. The:asciiformat is primarily to support legacy use cases and is not recommended. SeeCldr.Time.available_formats/3to see which formats have these variants.Some formats (at the time of publishing, only date and datetime formats) have
:defaultand:variantversions of the format. These variant formats are only included in a small number of locales. For example, the:"en-CA"locale, which has a:defaultformat respecting typical Canadian formatting and a:variantthat is more closely aligned to US formatting. The default is:default.
Returns
formatted_time_stringorraises 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"