|
1 | | -# defmodule Cldr.Timezone do |
2 | | -# @moduledoc """ |
3 | | -# Functions to map between the CLDR short time zone code and the |
4 | | -# IANA timezone names. |
5 | | -# |
6 | | -# The Unicode [locale](https://unicode.org/reports/tr35/#Locale) |
7 | | -# [extension U](https://unicode.org/reports/tr35/#u_Extension) |
8 | | -# allows the specification of the time zone requested for the provided locale. |
9 | | -# |
10 | | -# This short timezone codes never change even if the IANA names change |
11 | | -# over time. Therefore these short codes are always stable between CLDR |
12 | | -# releases. |
13 | | -# |
14 | | -# """ |
15 | | -# alias Cldr.Locale |
16 | | -# |
17 | | -# @type short_zone :: String.t() |
18 | | -# |
19 | | -# @type timezone :: %{ |
20 | | -# aliases: [String.t(), ...], |
21 | | -# preferred: nil | short_zone(), |
22 | | -# territory: Locale.territory_code() |
23 | | -# } |
24 | | -# @unknown_zone "Etc/Unknown" |
25 | | -# |
26 | | -# @timezones Cldr.Config.timezones() |
27 | | -# |
28 | | -# @timezones_by_territory @timezones |
29 | | -# |> Enum.group_by( |
30 | | -# fn {_k, v} -> v.territory end, |
31 | | -# fn {k, v} -> Map.put(v, :short_zone, k) end |
32 | | -# ) |
33 | | -# |> Enum.map(fn |
34 | | -# {nil, _} -> |
35 | | -# nil |
36 | | -# |
37 | | -# {k, v} -> |
38 | | -# case Cldr.validate_territory(k) do |
39 | | -# {:ok, territory} -> {territory, Elixir.List.flatten(v)} |
40 | | -# end |
41 | | -# end) |
42 | | -# |> Enum.reject(&is_nil/1) |
43 | | -# |> Map.new() |
44 | | -# |
45 | | -# @territories_by_timezone @timezones_by_territory |
46 | | -# |> Enum.map(fn {territory, zones} -> |
47 | | -# Enum.map(zones, fn zone -> |
48 | | -# Enum.map(zone.aliases, fn aliass -> {aliass, territory} end) |
49 | | -# end) |
50 | | -# end) |
51 | | -# |> List.flatten() |
52 | | -# |> Enum.reject(fn {_zone, territory} -> territory == :UT end) |
53 | | -# |> Map.new() |
54 | | -# |
55 | | -# @doc """ |
56 | | -# Returns a mapping of CLDR short zone codes to |
57 | | -# IANA timezone names. |
58 | | -# |
59 | | -# """ |
60 | | -# @spec timezones() :: %{(zone_name :: String.t()) => timezone()} |
61 | | -# def timezones do |
62 | | -# @timezones |
63 | | -# end |
64 | | -# |
65 | | -# @doc false |
66 | | -# @deprecated "Use timezones_by_territory/0 instead" |
67 | | -# def timezones_for_territory do |
68 | | -# timezones_by_territory() |
69 | | -# end |
70 | | -# |
71 | | -# @spec timezones_by_territory() :: |
72 | | -# unquote(Cldr.Type.timezones_by_territory(@timezones_by_territory)) |
73 | | -# |
74 | | -# @doc """ |
75 | | -# Returns a mapping of territories to |
76 | | -# their known IANA timezone names. |
77 | | -# |
78 | | -# """ |
79 | | -# @dialyzer {:nowarn_function, timezones_by_territory: 0} |
80 | | -# def timezones_by_territory do |
81 | | -# @timezones_by_territory |
82 | | -# end |
83 | | -# |
84 | | -# @doc """ |
85 | | -# Returns a mapping of time zone IDs to |
86 | | -# their known territory. |
87 | | -# |
88 | | -# A time zone can only belong to one |
89 | | -# territory in CLDR. |
90 | | -# |
91 | | -# """ |
92 | | -# def territories_by_timezone do |
93 | | -# @territories_by_timezone |
94 | | -# end |
95 | | -# |
96 | | -# @doc false |
97 | | -# def timezones_for_territory(territory) do |
98 | | -# timezones_by_territory() |
99 | | -# |> Map.fetch(territory) |
100 | | -# end |
101 | | -# |
102 | | -# @doc """ |
103 | | -# Returns a list of IANA time zone names for |
104 | | -# a given CLDR short zone code, or `nil`. |
105 | | -# |
106 | | -# The first time zone name in the list is |
107 | | -# the canonical time zone name. |
108 | | -# |
109 | | -# ### Examples |
110 | | -# |
111 | | -# iex> Cldr.Timezone.get_short_zone("ausyd") |
112 | | -# %{ |
113 | | -# preferred: nil, |
114 | | -# aliases: ["Australia/Sydney", "Australia/ACT", "Australia/Canberra", "Australia/NSW"], |
115 | | -# territory: :AU |
116 | | -# } |
117 | | -# |
118 | | -# iex> Cldr.Timezone.get_short_zone("nope") |
119 | | -# nil |
120 | | -# |
121 | | -# """ |
122 | | -# @spec get_short_zone(String.t(), String.t() | nil) :: map() | nil |
123 | | -# def get_short_zone(short_zone, default \\ nil) do |
124 | | -# Map.get(timezones(), short_zone, default) |
125 | | -# end |
126 | | -# |
127 | | -# @doc false |
128 | | -# @deprecated "Use get_short_zone/1 instead" |
129 | | -# def get(short_zone) do |
130 | | -# get_short_zone(short_zone) |
131 | | -# end |
132 | | -# |
133 | | -# @doc """ |
134 | | -# Returns a `{:ok, list}` where list is a |
135 | | -# list of IANA timezone names for |
136 | | -# a given CLDR short zone code. If no such |
137 | | -# short code exists then `:error` is returned. |
138 | | -# |
139 | | -# ### Example |
140 | | -# |
141 | | -# iex> Cldr.Timezone.fetch_short_zone("ausyd") |
142 | | -# { |
143 | | -# :ok, |
144 | | -# %{ |
145 | | -# preferred: nil, |
146 | | -# aliases: ["Australia/Sydney", "Australia/ACT", "Australia/Canberra", "Australia/NSW"], |
147 | | -# territory: :AU |
148 | | -# } |
149 | | -# } |
150 | | -# |
151 | | -# iex> Cldr.Timezone.fetch_short_zone("nope") |
152 | | -# :error |
153 | | -# |
154 | | -# """ |
155 | | -# @spec fetch_short_zone(String.t()) :: {:ok, map()} | :error |
156 | | -# def fetch_short_zone(short_zone) do |
157 | | -# Map.fetch(timezones(), short_zone) |
158 | | -# end |
159 | | -# |
160 | | -# @doc false |
161 | | -# @deprecated "Use fetch_short_zone/1 instead" |
162 | | -# def fetch(short_zone) do |
163 | | -# fetch_short_zone(short_zone) |
164 | | -# end |
165 | | -# |
166 | | -# @doc false |
167 | | -# @deprecated "Use validate_short_zone/1 instead" |
168 | | -# def validate_timezone(short_zone) do |
169 | | -# validate_short_zone(short_zone) |
170 | | -# end |
171 | | -# |
172 | | -# @doc false |
173 | | -# @spec validate_short_zone(String.t()) :: {:ok, String.t()} | {:error, String.t()} |
174 | | -# def validate_short_zone(short_zone) do |
175 | | -# case fetch(short_zone) do |
176 | | -# {:ok, %{aliases: [first_zone | _others]}} -> |
177 | | -# {:ok, first_zone} |
178 | | -# |
179 | | -# :error -> |
180 | | -# {:error, @unknown_zone} |
181 | | -# end |
182 | | -# end |
183 | | -# end |
| 1 | +defmodule Cldr.Timezone do |
| 2 | + @moduledoc """ |
| 3 | + Functions to map between the CLDR short time zone code and the |
| 4 | + IANA timezone names. |
| 5 | +
|
| 6 | + The Unicode [locale](https://unicode.org/reports/tr35/#Locale) |
| 7 | + [extension U](https://unicode.org/reports/tr35/#u_Extension) |
| 8 | + allows the specification of the time zone requested for the provided locale. |
| 9 | +
|
| 10 | + This short timezone codes never change even if the IANA names change |
| 11 | + over time. Therefore these short codes are always stable between CLDR |
| 12 | + releases. |
| 13 | +
|
| 14 | + """ |
| 15 | + alias Cldr.Locale |
| 16 | + |
| 17 | + @type short_zone :: String.t() |
| 18 | + |
| 19 | + @type timezone :: %{ |
| 20 | + aliases: [String.t(), ...], |
| 21 | + preferred: nil | short_zone(), |
| 22 | + territory: Locale.territory_code() |
| 23 | + } |
| 24 | + @unknown_zone "Etc/Unknown" |
| 25 | + |
| 26 | + @timezones Cldr.Config.timezones() |
| 27 | + |
| 28 | + @timezones_by_territory @timezones |
| 29 | + |> Enum.group_by( |
| 30 | + fn {_k, v} -> v.territory end, |
| 31 | + fn {k, v} -> Map.put(v, :short_zone, k) end |
| 32 | + ) |
| 33 | + |> Enum.map(fn |
| 34 | + {nil, _} -> |
| 35 | + nil |
| 36 | + |
| 37 | + {k, v} -> |
| 38 | + case Cldr.validate_territory(k) do |
| 39 | + {:ok, territory} -> {territory, Elixir.List.flatten(v)} |
| 40 | + end |
| 41 | + end) |
| 42 | + |> Enum.reject(&is_nil/1) |
| 43 | + |> Map.new() |
| 44 | + |
| 45 | + @territories_by_timezone @timezones_by_territory |
| 46 | + |> Enum.map(fn {territory, zones} -> |
| 47 | + Enum.map(zones, fn zone -> |
| 48 | + Enum.map(zone.aliases, fn aliass -> {aliass, territory} end) |
| 49 | + end) |
| 50 | + end) |
| 51 | + |> List.flatten() |
| 52 | + |> Enum.reject(fn {_zone, territory} -> territory == :UT end) |
| 53 | + |> Map.new() |
| 54 | + |
| 55 | + @doc """ |
| 56 | + Returns a mapping of CLDR short zone codes to |
| 57 | + IANA timezone names. |
| 58 | +
|
| 59 | + """ |
| 60 | + @spec timezones() :: %{(zone_name :: String.t()) => timezone()} |
| 61 | + def timezones do |
| 62 | + @timezones |
| 63 | + end |
| 64 | + |
| 65 | + @doc false |
| 66 | + @deprecated "Use timezones_by_territory/0 instead" |
| 67 | + def timezones_for_territory do |
| 68 | + timezones_by_territory() |
| 69 | + end |
| 70 | + |
| 71 | + @spec timezones_by_territory() :: |
| 72 | + unquote(Cldr.Type.timezones_by_territory(@timezones_by_territory)) |
| 73 | + |
| 74 | + @doc """ |
| 75 | + Returns a mapping of territories to |
| 76 | + their known IANA timezone names. |
| 77 | +
|
| 78 | + """ |
| 79 | + @dialyzer {:nowarn_function, timezones_by_territory: 0} |
| 80 | + def timezones_by_territory do |
| 81 | + @timezones_by_territory |
| 82 | + end |
| 83 | + |
| 84 | + @doc """ |
| 85 | + Returns a mapping of time zone IDs to |
| 86 | + their known territory. |
| 87 | +
|
| 88 | + A time zone can only belong to one |
| 89 | + territory in CLDR. |
| 90 | +
|
| 91 | + """ |
| 92 | + def territories_by_timezone do |
| 93 | + @territories_by_timezone |
| 94 | + end |
| 95 | + |
| 96 | + @doc false |
| 97 | + def timezones_for_territory(territory) do |
| 98 | + timezones_by_territory() |
| 99 | + |> Map.fetch(territory) |
| 100 | + end |
| 101 | + |
| 102 | + @doc """ |
| 103 | + Returns a list of IANA time zone names for |
| 104 | + a given CLDR short zone code, or `nil`. |
| 105 | +
|
| 106 | + The first time zone name in the list is |
| 107 | + the canonical time zone name. |
| 108 | +
|
| 109 | + ### Examples |
| 110 | +
|
| 111 | + iex> Cldr.Timezone.get_short_zone("ausyd") |
| 112 | + %{ |
| 113 | + preferred: nil, |
| 114 | + aliases: ["Australia/Sydney", "Australia/ACT", "Australia/Canberra", "Australia/NSW"], |
| 115 | + territory: :AU |
| 116 | + } |
| 117 | +
|
| 118 | + iex> Cldr.Timezone.get_short_zone("nope") |
| 119 | + nil |
| 120 | +
|
| 121 | + """ |
| 122 | + @spec get_short_zone(String.t(), String.t() | nil) :: map() | nil |
| 123 | + def get_short_zone(short_zone, default \\ nil) do |
| 124 | + Map.get(timezones(), short_zone, default) |
| 125 | + end |
| 126 | + |
| 127 | + @doc false |
| 128 | + @deprecated "Use get_short_zone/1 instead" |
| 129 | + def get(short_zone) do |
| 130 | + get_short_zone(short_zone) |
| 131 | + end |
| 132 | + |
| 133 | + @doc """ |
| 134 | + Returns a `{:ok, list}` where list is a |
| 135 | + list of IANA timezone names for |
| 136 | + a given CLDR short zone code. If no such |
| 137 | + short code exists then `:error` is returned. |
| 138 | +
|
| 139 | + ### Example |
| 140 | +
|
| 141 | + iex> Cldr.Timezone.fetch_short_zone("ausyd") |
| 142 | + { |
| 143 | + :ok, |
| 144 | + %{ |
| 145 | + preferred: nil, |
| 146 | + aliases: ["Australia/Sydney", "Australia/ACT", "Australia/Canberra", "Australia/NSW"], |
| 147 | + territory: :AU |
| 148 | + } |
| 149 | + } |
| 150 | +
|
| 151 | + iex> Cldr.Timezone.fetch_short_zone("nope") |
| 152 | + :error |
| 153 | +
|
| 154 | + """ |
| 155 | + @spec fetch_short_zone(String.t()) :: {:ok, map()} | :error |
| 156 | + def fetch_short_zone(short_zone) do |
| 157 | + Map.fetch(timezones(), short_zone) |
| 158 | + end |
| 159 | + |
| 160 | + @doc false |
| 161 | + @deprecated "Use fetch_short_zone/1 instead" |
| 162 | + def fetch(short_zone) do |
| 163 | + fetch_short_zone(short_zone) |
| 164 | + end |
| 165 | + |
| 166 | + @doc false |
| 167 | + @deprecated "Use validate_short_zone/1 instead" |
| 168 | + def validate_timezone(short_zone) do |
| 169 | + validate_short_zone(short_zone) |
| 170 | + end |
| 171 | + |
| 172 | + @doc false |
| 173 | + @spec validate_short_zone(String.t()) :: {:ok, String.t()} | {:error, String.t()} |
| 174 | + def validate_short_zone(short_zone) do |
| 175 | + case fetch(short_zone) do |
| 176 | + {:ok, %{aliases: [first_zone | _others]}} -> |
| 177 | + {:ok, first_zone} |
| 178 | + |
| 179 | + :error -> |
| 180 | + {:error, @unknown_zone} |
| 181 | + end |
| 182 | + end |
| 183 | +end |
0 commit comments