Skip to content

Commit 5bc0fc1

Browse files
committed
Improve handling of no-inheritance-marker in timezone data
Fixes #1192 (but uncovers another bug)
1 parent b6dd7c7 commit 5bc0fc1

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

babel/dates.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,9 @@ def get_timezone_name(
649649
info = locale.time_zones.get(zone, {})
650650
# Try explicitly translated zone names first
651651
if width in info and zone_variant in info[width]:
652-
return info[width][zone_variant]
652+
value = info[width][zone_variant]
653+
if value != NO_INHERITANCE_MARKER:
654+
return value
653655

654656
metazone = get_global('meta_zones').get(zone)
655657
if metazone:
@@ -660,7 +662,7 @@ def get_timezone_name(
660662
# If the short form is marked no-inheritance,
661663
# try to fall back to the long name instead.
662664
name = metazone_info.get('long', {}).get(zone_variant)
663-
if name:
665+
if name and name != NO_INHERITANCE_MARKER:
664666
return name
665667

666668
# If we have a concrete datetime, we assume that the result can't be

tests/test_dates.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,3 +1187,33 @@ def test_issue_1089():
11871187
def test_issue_1162(locale, format, negative, expected):
11881188
delta = timedelta(seconds=10800) * (-1 if negative else +1)
11891189
assert dates.format_timedelta(delta, add_direction=True, format=format, locale=locale) == expected
1190+
1191+
1192+
def test_issue_1192():
1193+
# The actual returned value here is not actually strictly specified ("get_timezone_name"
1194+
# is not an operation specified as such). Issue #1192 concerned this invocation returning
1195+
# the invalid "no inheritance marker" value; _that_ should never be returned here.
1196+
# IOW, if the below "Hawaii-Aleutian Time" changes with e.g. CLDR updates, that's fine.
1197+
assert dates.get_timezone_name('Pacific/Honolulu', 'short', locale='en_GB') == "Hawaii-Aleutian Time"
1198+
1199+
1200+
@pytest.mark.xfail
1201+
def test_issue_1192_fmt(timezone_getter):
1202+
"""
1203+
There is an issue in how we format the fallback for z/zz in the absence of data
1204+
(esp. with the no inheritance marker present).
1205+
This test is marked xfail until that's fixed.
1206+
"""
1207+
# env TEST_TIMEZONES=Pacific/Honolulu TEST_LOCALES=en_US,en_GB TEST_TIME_FORMAT="YYYY-MM-dd H:mm z" bin/icu4c_date_format
1208+
# Defaulting TEST_TIME to 2025-03-04T13:53:00Z
1209+
# Pacific/Honolulu en_US 2025-03-04 3:53 HST
1210+
# Pacific/Honolulu en_GB 2025-03-04 3:53 GMT-10
1211+
# env TEST_TIMEZONES=Pacific/Honolulu TEST_LOCALES=en_US,en_GB TEST_TIME_FORMAT="YYYY-MM-dd H:mm zz" bin/icu4c_date_format
1212+
# Pacific/Honolulu en_US 2025-03-04 3:53 HST
1213+
# Pacific/Honolulu en_GB 2025-03-04 3:53 GMT-10
1214+
tz = timezone_getter("Pacific/Honolulu")
1215+
dt = _localize(tz, datetime(2025, 3, 4, 13, 53, tzinfo=UTC))
1216+
assert dates.format_datetime(dt, "YYYY-MM-dd H:mm z", locale="en_US") == "2025-03-04 3:53 HST"
1217+
assert dates.format_datetime(dt, "YYYY-MM-dd H:mm z", locale="en_GB") == "2025-03-04 3:53 GMT-10"
1218+
assert dates.format_datetime(dt, "YYYY-MM-dd H:mm zz", locale="en_US") == "2025-03-04 3:53 HST"
1219+
assert dates.format_datetime(dt, "YYYY-MM-dd H:mm zz", locale="en_GB") == "2025-03-04 3:53 GMT-10"

0 commit comments

Comments
 (0)