Skip to content

Commit 7110e62

Browse files
authored
Use canonical IANA zone names in zone_territories (#1220)
Refs #1218
1 parent e91c346 commit 7110e62

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

scripts/import_cldr.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,22 @@ def parse_global(srcdir, sup):
251251
for key_elem in bcp47_timezone.findall('.//keyword/key'):
252252
if key_elem.attrib['name'] == 'tz':
253253
for elem in key_elem.findall('type'):
254-
if 'deprecated' not in elem.attrib:
255-
aliases = str(elem.attrib['alias']).split()
256-
tzid = aliases.pop(0)
257-
territory = _zone_territory_map.get(tzid, '001')
258-
territory_zones.setdefault(territory, []).append(tzid)
259-
zone_territories[tzid] = territory
260-
for alias in aliases:
254+
if 'deprecated' in elem.attrib:
255+
continue
256+
aliases = str(elem.attrib['alias']).split()
257+
iana = elem.attrib.get('iana')
258+
tzid = iana or aliases[0] # Use the IANA ID if available, otherwise the first alias
259+
territory = '001'
260+
# The windowsZones map might use an alias to refer to a timezone,
261+
# so can't just do a simple dict lookup.
262+
for cand in (tzid, *aliases):
263+
if cand in _zone_territory_map:
264+
territory = _zone_territory_map[cand]
265+
break
266+
territory_zones.setdefault(territory, []).append(tzid)
267+
zone_territories[tzid] = territory
268+
for alias in aliases:
269+
if alias != tzid:
261270
zone_aliases[alias] = tzid
262271
break
263272

tests/test_core.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,18 @@ def test_ignore_invalid_locales_in_lc_ctype(monkeypatch):
5555
default_locale('LC_CTYPE')
5656

5757

58-
def test_get_global():
59-
assert core.get_global('zone_aliases')['GMT'] == 'Etc/GMT'
60-
assert core.get_global('zone_aliases')['UTC'] == 'Etc/UTC'
61-
assert core.get_global('zone_territories')['Europe/Berlin'] == 'DE'
58+
def test_zone_aliases_and_territories():
59+
aliases = core.get_global('zone_aliases')
60+
territories = core.get_global('zone_territories')
61+
assert aliases['GMT'] == 'Etc/GMT'
62+
assert aliases['UTC'] == 'Etc/UTC'
63+
assert territories['Europe/Berlin'] == 'DE'
64+
# Check that the canonical (IANA) names are used in `territories`,
65+
# but that aliases are still available.
66+
assert territories['Africa/Asmara'] == 'ER'
67+
assert aliases['Africa/Asmera'] == 'Africa/Asmara'
68+
assert territories['Europe/Kyiv'] == 'UA'
69+
assert aliases['Europe/Kiev'] == 'Europe/Kyiv'
6270

6371

6472
def test_hash():

0 commit comments

Comments
 (0)