Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use aware UTC datetimes internally #1009

Merged
merged 1 commit into from Jun 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions babel/dates.py
Expand Up @@ -112,7 +112,7 @@
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 @@
: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 @@
:rtype: time
"""
if time is None:
time = datetime.datetime.utcnow()
time = datetime.datetime.now(UTC)

Check warning on line 204 in babel/dates.py

View check run for this annotation

Codecov / codecov/patch

babel/dates.py#L204

Added line #L204 was not covered by tests
elif isinstance(time, (int, float)):
time = datetime.datetime.utcfromtimestamp(time)
time = datetime.datetime.fromtimestamp(time, UTC)

Check warning on line 206 in babel/dates.py

View check run for this annotation

Codecov / codecov/patch

babel/dates.py#L206

Added line #L206 was not covered by tests

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