1 Star 0 Fork 24

ZhouPengcheng / babel

forked from src-openEuler / babel 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
backport-Use-aware-UTC-datetimes-internally.patch 3.82 KB
一键复制 编辑 原始数据 按行查看 历史
From 1747d2230a0294a07c99563cf50fb95a37b6ac0f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ville=20Skytt=C3=A4?= <ville.skytta@iki.fi>
Date: Tue, 27 Jun 2023 23:08:06 +0300
Subject: [PATCH] Use aware UTC datetimes internally (#1009)
Avoids deprecation warnings on Python 3.12.
---
babel/dates.py | 10 +++++-----
docs/dates.rst | 6 +++---
tests/test_dates.py | 6 +++---
3 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/babel/dates.py b/babel/dates.py
index ad82b789b..ddc8e7105 100644
--- a/babel/dates.py
+++ b/babel/dates.py
@@ -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:
@@ -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):
@@ -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)
diff --git a/docs/dates.rst b/docs/dates.rst
index 1827a9a20..0c2c17fc0 100644
--- a/docs/dates.rst
+++ b/docs/dates.rst
@@ -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`.
diff --git a/tests/test_dates.py b/tests/test_dates.py
index 3f1fc3fc8..f4f577397 100644
--- a/tests/test_dates.py
+++ b/tests/test_dates.py
@@ -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
@@ -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'))
@@ -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")
1
https://gitee.com/zhoupengcheng11/babel.git
git@gitee.com:zhoupengcheng11/babel.git
zhoupengcheng11
babel
babel
master

搜索帮助