Skip to content

Commit ad28a4a

Browse files
committed
Fix parsing language tags with a -u-tz short code than needs canonicalising
1 parent 015bd80 commit ad28a4a

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
**Note that `ex_cldr` version 2.39.0 and later are supported on Elixir 1.12 and later only.**
44

5-
## Cldr v2.43.1
5+
## Cldr v2.43.2
66

7-
This is the changelog for Cldr v2.43.1 released on August 26th, 2025. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr/tags)
7+
This is the changelog for Cldr v2.43.2 released on ______, 2025. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr/tags)
88

99
### Upgrading
1010

@@ -15,6 +15,14 @@ This is the changelog for Cldr v2.43.1 released on August 26th, 2025. For older
1515

1616
### Bug Fixes
1717

18+
* Fix Parsing a language tag when the U extension has a timezone short code that needs to be canonicalised. For example, "en-u-tz-est5edt" needs to become "en-u-tz-usnyc".
19+
20+
## Cldr v2.43.1
21+
22+
This is the changelog for Cldr v2.43.1 released on August 26th, 2025. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr/tags)
23+
24+
### Bug Fixes
25+
1826
* Fix `Cldr.Locale.timezone_from_locale/1` when the territory has one zone and the zone short code is not instantiated as part of the language tag.
1927

2028
## Cldr v2.43.0

lib/cldr/locale.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,11 +1369,11 @@ defmodule Cldr.Locale do
13691369
zone_id
13701370

13711371
%{prefer: short_zone} when not is_nil(short_zone)->
1372-
%{aliases: [zone_id]} =
1372+
%{aliases: zone_ids} =
13731373
Cldr.Timezone.timezones()
13741374
|> Map.fetch!(short_zone)
13751375

1376-
zone_id
1376+
hd(zone_ids)
13771377

13781378
%{aliases: zones} ->
13791379
ambiguous_timezone_error(territory, zones)

lib/cldr/validity/u.ex

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ defmodule Cldr.Validity.U do
173173
# valid function check that the value provided
174174
# is acceptable for the given key.
175175

176-
for {key, values} <- @validity_data, key in @process_keys do
176+
for {key, values} <- @validity_data, key in @process_keys -- ["tz"] do
177177
defp valid(unquote(key), value) when value in unquote(Map.keys(values)) do
178178
unquote(Macro.escape(values))
179179
|> get_value(unquote(key), value)
@@ -182,6 +182,22 @@ defmodule Cldr.Validity.U do
182182
end
183183
end
184184

185+
# For timezones a nil value means its an acceptable
186+
# time zone name but it needs to be resolved to its
187+
# canonical version. For example, est5edt becomes usnyc
188+
@tz_values @validity_data["tz"]
189+
defp valid("tz", value) do
190+
case Map.fetch(@tz_values, value) do
191+
{:ok, nil} ->
192+
{:ok, preferred} = preferred_timezone(value)
193+
{:ok, %{aliases: aliases}} = Map.fetch(Cldr.Timezone.timezones(), preferred)
194+
{:ok, hd(aliases)}
195+
196+
{:ok, values} when is_list(values) ->
197+
{:ok, hd(values)}
198+
end
199+
end
200+
185201
# Calendar names may be compound like
186202
# islamic-rgsa
187203
defp valid("ca", values) when is_list(values) do
@@ -266,6 +282,12 @@ defmodule Cldr.Validity.U do
266282
{:error, invalid_key_error(key)}
267283
end
268284

285+
defp preferred_timezone(timezone) do
286+
Cldr.Timezone.timezones()
287+
|> Map.fetch!(timezone)
288+
|> Map.fetch(:preferred)
289+
end
290+
269291
defp get_value(map, "cu", value) do
270292
(Map.get(map, value) || value)
271293
|> String.upcase()

test/language_tag_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,12 @@ defmodule CldrLanguageTagTest do
159159
{:error,
160160
{Cldr.LanguageTag.ParseError, "The value \"bogus\" is not valid for the key \"mu\""}}
161161
end
162+
163+
test "with u extensions and time zone that requires canonicalisation" do
164+
import Cldr.LanguageTag.Sigil
165+
166+
assert {:ok, tag} = Cldr.validate_locale("en-u-tz-est5edt")
167+
assert "TestBackend.Cldr.Locale.new!(\"en-US-u-tz-usnyc\")" == inspect(tag)
168+
assert "America/New_York" = tag.locale.timezone
169+
end
162170
end

0 commit comments

Comments
 (0)