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
6 changes: 3 additions & 3 deletions babel/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def _get_dt_and_tzinfo(
:rtype: tuple[datetime, tzinfo]
"""
if dt_or_tzinfo is None:
dt = datetime.datetime.now()
tzinfo = LOCALTZ
dt = datetime.datetime.now(tzinfo)
elif isinstance(dt_or_tzinfo, str):
dt = None
tzinfo = get_timezone(dt_or_tzinfo)
Expand Down Expand Up @@ -626,7 +626,7 @@ def get_timezone_name(
the timezone; if a ``tzinfo`` object is used, the
resulting display name will be generic, i.e.
independent of daylight savings time; if `None`, the
current date in UTC is assumed
current date and time in the local timezone is assumed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a behavioral change, isn't it?

:param width: either "long" or "short"
:param uncommon: deprecated and ignored
:param zone_variant: defines the zone variation to return. By default the
Expand All @@ -648,7 +648,7 @@ def get_timezone_name(
if dt is None:
zone_variant = 'generic'
else:
dst = tzinfo.dst(dt)
dst = dt.dst()
zone_variant = "daylight" if dst else "standard"
else:
if zone_variant not in ('generic', 'standard', 'daylight'):
Expand Down
39 changes: 38 additions & 1 deletion tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# history and logs, available at https://github.com/python-babel/babel/commits/master/.

import calendar
from datetime import date, datetime, time, timedelta
from datetime import date, datetime, time, timedelta, timezone

import freezegun
import pytest
Expand Down Expand Up @@ -361,6 +361,43 @@ def test_get_timezone_name_misc(timezone_getter):
assert (dates.get_timezone_name(time(16, 20), locale='en_US', width='short') == "UTC")


@pytest.mark.parametrize(
('offset', 'expected'),
[
(timedelta(hours=5, minutes=30), 'GMT+05:30'),
(-timedelta(hours=3, minutes=30), 'GMT-03:30'),
],
)
def test_get_timezone_name_none_uses_local_offset(monkeypatch, offset, expected):
monkeypatch.setattr(dates, 'LOCALTZ', timezone(offset))

with freezegun.freeze_time('2023-07-01 12:00:00'):
assert dates.get_timezone_name(None, locale='en') == expected


@pytest.mark.parametrize(
('frozen_time', 'expected_offset'),
[
('2023-01-15 12:00:00', timedelta(hours=-5)),
('2023-07-15 12:00:00', timedelta(hours=-4)),
],
)
def test_get_dt_and_tzinfo_none_preserves_local_dst(
timezone_getter,
monkeypatch,
frozen_time,
expected_offset,
):
local_tz = timezone_getter('America/New_York')
monkeypatch.setattr(dates, 'LOCALTZ', local_tz)

with freezegun.freeze_time(frozen_time):
dt, tzinfo = dates._get_dt_and_tzinfo(None)

assert tzinfo is local_tz
assert dt.utcoffset() == expected_offset


def test_format_date():
d = date(2007, 4, 1)
assert dates.format_date(d, locale='en_US') == 'Apr 1, 2007'
Expand Down