Skip to content

Commit

Permalink
Use aware UTC datetimes internally (#1009)
Browse files Browse the repository at this point in the history
Avoids deprecation warnings on Python 3.12.
  • Loading branch information
scop committed Jun 27, 2023
1 parent 95a0ea8 commit 1747d22
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions babel/dates.py
Expand Up @@ -112,7 +112,7 @@ def _get_tz_name(dt_or_tzinfo: _DtOrTzinfo) -> str:
elif hasattr(tzinfo, 'key') and tzinfo.key is not None: # ZoneInfo object
return tzinfo.key
else:
return tzinfo.tzname(dt or datetime.datetime.utcnow())
return tzinfo.tzname(dt or datetime.datetime.now(UTC))


def _get_datetime(instant: _Instant) -> datetime.datetime:
Expand Down Expand Up @@ -147,9 +147,9 @@ def _get_datetime(instant: _Instant) -> datetime.datetime:
:rtype: datetime
"""
if instant is None:
return datetime.datetime.utcnow()
return datetime.datetime.now(UTC).replace(tzinfo=None)
elif isinstance(instant, (int, float)):
return datetime.datetime.utcfromtimestamp(instant)
return datetime.datetime.fromtimestamp(instant, UTC).replace(tzinfo=None)
elif isinstance(instant, datetime.time):
return datetime.datetime.combine(datetime.date.today(), instant)
elif isinstance(instant, datetime.date) and not isinstance(instant, datetime.datetime):
Expand Down Expand Up @@ -201,9 +201,9 @@ def _get_time(
:rtype: time
"""
if time is None:
time = datetime.datetime.utcnow()
time = datetime.datetime.now(UTC)
elif isinstance(time, (int, float)):
time = datetime.datetime.utcfromtimestamp(time)
time = datetime.datetime.fromtimestamp(time, UTC)

if time.tzinfo is None:
time = time.replace(tzinfo=UTC)
Expand Down
6 changes: 3 additions & 3 deletions docs/dates.rst
Expand Up @@ -67,9 +67,9 @@ local time when returning dates to users. At that point the timezone the
user has selected can usually be established and Babel can automatically
rebase the time for you.

To get the current time use the :meth:`~datetime.datetime.utcnow` method
of the :class:`~datetime.datetime` object. It will return a naive
:class:`~datetime.datetime` object in UTC.
To get the current time use the :meth:`~datetime.datetime.now` method
of the :class:`~datetime.datetime` object,
passing :attr:`~datetime.timezone.utc` to it as the timezone.

For more information about timezones see :ref:`timezone-support`.

Expand Down
6 changes: 3 additions & 3 deletions tests/test_dates.py
Expand Up @@ -17,7 +17,7 @@
import pytest

from babel import Locale, dates
from babel.dates import NO_INHERITANCE_MARKER, _localize
from babel.dates import NO_INHERITANCE_MARKER, UTC, _localize
from babel.util import FixedOffsetTimezone


Expand Down Expand Up @@ -542,7 +542,7 @@ def test_get_timezone_name_time_pytz(timezone_getter, tzname, params, expected):


def test_get_timezone_name_misc(timezone_getter):
localnow = datetime.utcnow().replace(tzinfo=timezone_getter('UTC')).astimezone(dates.LOCALTZ)
localnow = datetime.now(timezone_getter('UTC')).astimezone(dates.LOCALTZ)
assert (dates.get_timezone_name(None, locale='en_US') ==
dates.get_timezone_name(localnow, locale='en_US'))

Expand Down Expand Up @@ -703,7 +703,7 @@ def test_zh_TW_format():


def test_format_current_moment():
frozen_instant = datetime.utcnow()
frozen_instant = datetime.now(UTC)
with freezegun.freeze_time(time_to_freeze=frozen_instant):
assert dates.format_datetime(locale="en_US") == dates.format_datetime(frozen_instant, locale="en_US")

Expand Down

0 comments on commit 1747d22

Please sign in to comment.