Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions babel/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,10 @@ def get_timezone_location(
territory = get_global('zone_territories').get(zone)
if territory not in locale.territories:
territory = 'ZZ' # invalid/unknown
territory_name = locale.territories[territory]
territory_name = locale.territories.get(territory)
if (
not return_city
and territory
and territory_name
and len(get_global('territory_zones').get(territory, [])) == 1
):
return region_format % territory_name
Expand All @@ -552,6 +552,10 @@ def get_timezone_location(

if return_city:
return city_name
if territory_name is None:
# The locale has no name for the zone's region, not even for the
# "unknown region" fallback, so the region is left out.
return region_format % city_name
return region_format % (
fallback_format
% {
Expand Down
11 changes: 11 additions & 0 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,17 @@ def test_get_timezone_location(timezone_getter):
'Deutschland (Berlin) (Ortszeit)')


def test_get_timezone_location_without_a_region_name(timezone_getter):
# Locales without a name for the "unknown region" (CLDR "ZZ") used to
# raise a KeyError instead of falling back to the location without region.
tz = timezone_getter('America/St_Johns')
assert dates.get_timezone_location(tz, locale='aa') == 'St. John’s'
assert dates.get_timezone_location(tz, locale='aa', return_city=True) == 'St. John’s'
assert dates.get_timezone_name(tz, locale='aa') == 'St. John’s'
# Locales that do have a name for it are unchanged.
assert dates.get_timezone_location(tz, locale='en') == 'Canada (St. John’s) Time'


@pytest.mark.parametrize(
('tzname', 'params', 'expected'),
[
Expand Down
Loading