Skip to content
Merged
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: 4 additions & 4 deletions babel/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def get_timezone(zone: str | datetime.tzinfo | None = None) -> datetime.tzinfo:

try:
return zoneinfo.ZoneInfo(zone)
except zoneinfo.ZoneInfoNotFoundError as e:
except (zoneinfo.ZoneInfoNotFoundError, ValueError) as e:
# ValueError is raised for invalid zone names (e.g. absolute paths)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this in the other PR, I added a test for it.

raise LookupError(f"Unknown timezone {zone}") from e


Expand Down Expand Up @@ -467,7 +468,7 @@ def get_timezone_gmt(
>>> get_timezone_gmt(dt, locale='en', width='iso8601_short')
'+00'
>>> tz = get_timezone('America/Los_Angeles')
>>> dt = _localize(tz, datetime(2007, 4, 1, 15, 30))
>>> dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
>>> get_timezone_gmt(dt, locale='en')
'GMT-07:00'
>>> get_timezone_gmt(dt, 'short', locale='en')
Expand Down Expand Up @@ -893,9 +894,8 @@ def format_time(
For any pattern requiring the display of the time-zone a
timezone has to be specified explicitly:

>>> t = datetime(2007, 4, 1, 15, 30)
>>> tzinfo = get_timezone('Europe/Paris')
>>> t = _localize(tzinfo, t)
>>> t = datetime(2007, 4, 1, 15, 30, tzinfo=tzinfo)
>>> format_time(t, format='full', tzinfo=tzinfo, locale='fr_FR')
'15:30:00 heure d’été d’Europe centrale'
>>> format_time(t, "hh 'o''clock' a, zzzz", tzinfo=get_timezone('US/Eastern'),
Expand Down
3 changes: 1 addition & 2 deletions docs/dates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,8 @@ display a list of time-zones to the user.
.. code-block:: pycon

>>> from datetime import datetime
>>> from babel.dates import _localize

>>> dt = _localize(tz, datetime(2007, 8, 15))
>>> dt = datetime(2007, 8, 15, tzinfo=tz)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_localize is now not used anywhere besides tests. Is it safe to move it there or do you think there's a chance someone is relying on it (despite being private)?

>>> get_timezone_name(dt, locale=Locale.parse('de_DE'))
'Mitteleuropäische Sommerzeit'
>>> get_timezone_name(tz, locale=Locale.parse('de_DE'))
Expand Down
6 changes: 6 additions & 0 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ def test_get_timezone_unknown():
dates.get_timezone("DoesNot/Exist")


@pytest.mark.parametrize('zone', ['/UTC', 'Europe/Berlin/', ''])
def test_get_timezone_invalid(zone):
with pytest.raises(LookupError):
dates.get_timezone(zone)


def test_get_timezone_gmt(timezone_getter):
dt = datetime(2007, 4, 1, 15, 30)
assert dates.get_timezone_gmt(dt, locale='en') == 'GMT+00:00'
Expand Down
1 change: 0 additions & 1 deletion tests/test_localtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
reason="Issue 1092 is not applicable on Windows",
)
def test_issue_1092(monkeypatch):
pytest.importorskip("pytz", reason="pytz is not installed")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not need pytz

monkeypatch.setenv("TZ", "/UTC") # Malformed timezone name.
with pytest.raises(LookupError):
get_localzone()
Expand Down
Loading