Provides timezone data access and timezone formatting for CLDR date/time format symbols.
This module combines CLDR short zone code lookups (mapping between
BCP 47 timezone identifiers and IANA timezone names) with the format
symbol handlers for z, Z, O, v, V, X, and x.
Summary
Functions
Returns {:ok, map} for a given CLDR short zone code,
or :error if no such short code exists.
Returns a timezone map for a given CLDR short zone code, or a default value.
Returns the localized GMT offset format.
Returns the ISO 8601 timezone offset format.
Returns the CLDR metazone for an IANA timezone name.
Returns the specific or generic non-location timezone name.
Returns a mapping of IANA time zone names to their known territory.
Returns the count of timezones for a given territory.
Returns a mapping of CLDR short zone codes to IANA timezone names.
Returns a mapping of territories to their known IANA timezone names.
Returns a list of timezone maps for a given territory.
Validates a CLDR short zone code and returns the canonical IANA timezone name.
Returns the IANA timezone that represents a CLDR metazone.
Functions
@spec fetch_short_zone(String.t()) :: {:ok, map()} | {:error, Exception.t()}
Returns {:ok, map} for a given CLDR short zone code,
or :error if no such short code exists.
Arguments
short_zoneis a CLDR short timezone code string.
Returns
{:ok, map}where map has:aliases,:preferred, and:territorykeys.{:error, exception}if the short zone code is not found.
Examples
iex> Localize.DateTime.Timezone.fetch_short_zone("ausyd")
{
:ok,
%{
preferred: nil,
aliases: ["Australia/Sydney", "Australia/ACT", "Australia/Canberra", "Australia/NSW"],
territory: :AU
}
}
iex> match?({:error, _}, Localize.DateTime.Timezone.fetch_short_zone("nope"))
true
Returns a timezone map for a given CLDR short zone code, or a default value.
Arguments
short_zoneis a CLDR short timezone code string.defaultis the value to return if the short zone is not found. Defaults tonil.
Returns
- A map with
:aliases,:preferred, and:territorykeys, or the default value.
Examples
iex> Localize.DateTime.Timezone.get_short_zone("ausyd")
%{
preferred: nil,
aliases: ["Australia/Sydney", "Australia/ACT", "Australia/Canberra", "Australia/NSW"],
territory: :AU
}
iex> Localize.DateTime.Timezone.get_short_zone("nope")
nil
@spec gmt_format(map(), atom(), Keyword.t()) :: {:ok, String.t()} | {:error, Exception.t()}
Returns the localized GMT offset format.
Uses the locale's gmt_format pattern (e.g., ["GMT", 0]) and
hour_format pattern to render the datetime's total UTC offset.
Arguments
datetimeis a map with an integer:utc_offsetin seconds and optionally an integer:std_offsetin seconds (aDateTime.t/0satisfies this shape).locale_idis a resolved locale identifier atom (e.g.,:en), such as thecldr_locale_idof a validatedLocalize.LanguageTag.t/0. It is passed directly toLocalize.Locale.get/2and is not validated or canonicalized by this function.optionsis a keyword list of options.
Options
:formatis:long(e.g.,"GMT+01:00") or:short(e.g.,"GMT+1"; minutes are dropped when zero). The default is:long.:zero_formatcontrols rendering of a zero offset. The default,:gmt_zero, uses the locale's zero pattern (e.g.,"GMT"); any other value formats the zero offset through the hour pattern (e.g.,"GMT+00:00").
Returns
{:ok, formatted_string}(e.g.,"GMT+01:00"or"GMT").{:error, exception}if the locale's timezone data cannot be loaded.
Examples
iex> Localize.DateTime.Timezone.gmt_format(%{utc_offset: 3600, std_offset: 0}, :en)
{:ok, "GMT+01:00"}
iex> Localize.DateTime.Timezone.gmt_format(%{utc_offset: -28800, std_offset: 0}, :en, format: :short)
{:ok, "GMT-8"}
Returns the ISO 8601 timezone offset format.
This function is locale-independent — ISO 8601 offsets are the same in every locale.
Arguments
datetimeis a map with an integer:utc_offsetin seconds and optionally an integer:std_offsetin seconds (aDateTime.t/0satisfies this shape).optionsis a keyword list of options.
Options
:formatis:short(minutes omitted when zero),:long(hours and minutes), or:full(like:long, with seconds appended when non-zero). The default is:long.:typeis:basic(no separator, e.g.,"+0500") or:extended(colon separator, e.g.,"+05:00"). The default is:basic.:z_for_zerois a boolean controlling whether a zero offset renders as"Z". The default istrue.
Returns
{:ok, formatted_string}(e.g.,"+0500","Z","+05:00").
Examples
iex> Localize.DateTime.Timezone.iso_format(%{utc_offset: 18000, std_offset: 0})
{:ok, "+0500"}
iex> Localize.DateTime.Timezone.iso_format(%{utc_offset: 19800, std_offset: 0}, type: :extended)
{:ok, "+05:30"}
iex> Localize.DateTime.Timezone.iso_format(%{utc_offset: 0, std_offset: 0})
{:ok, "Z"}
Returns the CLDR metazone for an IANA timezone name.
Zones move between metazones over time (for example
America/Indiana/Knox has alternated between the central and
eastern metazones), so the datetime selects the applicable usage
period.
Arguments
time_zoneis an IANA timezone name (e.g.,"America/New_York") or any of its CLDR aliases (e.g.,"Asia/Calcutta").datetimeis a map that may carry:year..:secondfields selecting the metazone in effect at that instant. When the fields are absent (ordatetimeisnil), the currently effective metazone is returned. The default isnil.
Returns
The metazone as an atom (e.g.,
:america_eastern), matching the keys of the localetime_zone_names.metazonedata.nilwhen the zone has no metazone mapping for the instant.
Examples
iex> Localize.DateTime.Timezone.metazone_for("America/New_York")
:america_eastern
iex> Localize.DateTime.Timezone.metazone_for("Asia/Calcutta")
:india
iex> Localize.DateTime.Timezone.metazone_for("America/Indiana/Knox", ~N[2000-06-01 00:00:00])
:america_eastern
iex> Localize.DateTime.Timezone.metazone_for("America/Indiana/Knox", ~N[2020-06-01 00:00:00])
:america_central
@spec non_location_format(map(), atom(), Keyword.t()) :: {:ok, String.t()} | {:error, Exception.t()}
Returns the specific or generic non-location timezone name.
Looks up the metazone name for the datetime's timezone in the
locale's timezone data (e.g., "Eastern Standard Time"). When the
timezone has no metazone mapping, or the locale carries no name
for it, falls back to gmt_format/3.
Arguments
datetimeis a map with:time_zone,:utc_offset, and:std_offsetkeys (aDateTime.t/0satisfies this shape).locale_idis a resolved locale identifier atom (e.g.,:en), such as thecldr_locale_idof a validatedLocalize.LanguageTag.t/0. It is passed directly toLocalize.Locale.get/2and is not validated or canonicalized by this function.optionsis a keyword list of options.
Options
:formatis:short(e.g.,"EST") or:long(e.g.,"Eastern Standard Time"). The default is:long.:typeis:specific(standard or daylight name chosen from the datetime's:std_offset),:generic,:standard, or:daylight. The default is:specific.
Returns
{:ok, timezone_name}with the localized non-location name, or{:ok, gmt_offset_string}when falling back to the GMT format.{:error, exception}if the locale's timezone data cannot be loaded.
Examples
iex> datetime = %{time_zone: "America/New_York", utc_offset: -18000, std_offset: 0}
iex> Localize.DateTime.Timezone.non_location_format(datetime, :en, format: :long)
{:ok, "Eastern Standard Time"}
iex> datetime = %{time_zone: "America/New_York", utc_offset: -18000, std_offset: 0}
iex> Localize.DateTime.Timezone.non_location_format(datetime, :en, format: :short)
{:ok, "EST"}
Returns a mapping of IANA time zone names to their known territory.
A time zone can only belong to one territory in CLDR.
Returns
- A map where each key is an IANA timezone string and each value is a territory atom.
Examples
iex> territories = Localize.DateTime.Timezone.territories_by_timezone()
iex> Map.get(territories, "Australia/Sydney")
:AU
@spec timezone_count_for_territory(atom()) :: {:ok, non_neg_integer()} | {:error, Exception.t()}
Returns the count of timezones for a given territory.
Arguments
territoryis a territory atom like:USor:AU.
Returns
{:ok, count}where count is the number of timezones.{:error, exception}if the territory has no known timezones.
Examples
iex> {:ok, count} = Localize.DateTime.Timezone.timezone_count_for_territory(:AU)
iex> count > 0
true
Returns a mapping of CLDR short zone codes to IANA timezone names.
Each key is a BCP 47 short timezone identifier string and each
value is a map with :aliases, :preferred, and :territory
keys.
Returns
- A map of
%{String.t() => map()}.
Examples
iex> timezones = Localize.DateTime.Timezone.timezones()
iex> Map.get(timezones, "ausyd")
%{preferred: nil, aliases: ["Australia/Sydney", "Australia/ACT", "Australia/Canberra", "Australia/NSW"], territory: :AU}
@spec timezones_by_territory() :: %{ required(atom()) => [ %{ short_zone: String.t(), territory: atom(), aliases: [term(), ...], preferred: nil | String.t() }, ... ] }
Returns a mapping of territories to their known IANA timezone names.
Returns
- A map where each key is a territory atom and each value is a
list of timezone maps including
:short_zone,:aliases,:preferred, and:territorykeys.
Examples
iex> {:ok, zones} = Localize.DateTime.Timezone.timezones_for_territory(:AU)
iex> Enum.any?(zones, & &1.short_zone == "ausyd")
true
@spec timezones_for_territory(atom()) :: {:ok, [map()]} | {:error, Exception.t()}
Returns a list of timezone maps for a given territory.
Arguments
territoryis a territory atom like:USor:AU.
Returns
{:ok, list}where list is timezone maps for the territory.{:error, exception}if the territory has no known timezones.
Examples
iex> {:ok, zones} = Localize.DateTime.Timezone.timezones_for_territory(:US)
iex> is_list(zones)
true
@spec validate_short_zone(String.t()) :: {:ok, String.t()} | {:error, Exception.t()}
Validates a CLDR short zone code and returns the canonical IANA timezone name.
Arguments
short_zoneis a CLDR short timezone code string.
Returns
{:ok, iana_name}whereiana_nameis the canonical IANA timezone name string.{:error, exception}if the short zone code is not valid.
Examples
iex> Localize.DateTime.Timezone.validate_short_zone("ausyd")
{:ok, "Australia/Sydney"}
iex> Localize.DateTime.Timezone.validate_short_zone("nope")
{:error, %Localize.UnknownTimezoneError{timezone: "nope"}}
Returns the IANA timezone that represents a CLDR metazone.
Arguments
metazoneis a metazone atom as returned bymetazone_for/2(e.g.,:america_pacific).territoryis a territory atom used to select a territory-specific representative zone (e.g.,:CAselects"America/Vancouver"for:america_pacific). The default is:"001", the metazone's golden zone.
Returns
The IANA timezone name for the territory, falling back to the metazone's golden zone when the territory has no specific mapping.
nilwhen the metazone is unknown.
Examples
iex> Localize.DateTime.Timezone.zone_for_metazone(:america_pacific)
"America/Los_Angeles"
iex> Localize.DateTime.Timezone.zone_for_metazone(:america_pacific, :CA)
"America/Vancouver"
iex> Localize.DateTime.Timezone.zone_for_metazone(:no_such_metazone)
nil