From a5e8d8578804b65213f0fc38d384c37561250068 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 20 Mar 2025 09:49:33 +0200 Subject: [PATCH] Use standard library `timezone` instead of `FixedOffsetTimezone` --- babel/messages/catalog.py | 7 +++++-- babel/util.py | 14 ++++++++++++-- pyproject.toml | 4 +++- tests/messages/test_catalog.py | 22 ++++++++++++++++++---- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index a35647f0..de96ea57 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -23,7 +23,7 @@ from babel import __version__ as VERSION from babel.core import Locale, UnknownLocaleError from babel.dates import format_datetime from babel.messages.plurals import get_plural -from babel.util import LOCALTZ, FixedOffsetTimezone, _cmp, distinct +from babel.util import LOCALTZ, _cmp, distinct if TYPE_CHECKING: from typing_extensions import TypeAlias @@ -118,7 +118,10 @@ def _parse_datetime_header(value: str) -> datetime.datetime: net_mins_offset *= plus_minus # Create an offset object - tzoffset = FixedOffsetTimezone(net_mins_offset) + tzoffset = datetime.timezone( + offset=datetime.timedelta(minutes=net_mins_offset), + name=f'Etc/GMT{net_mins_offset:+d}', + ) # Store the offset in a datetime object dt = dt.replace(tzinfo=tzoffset) diff --git a/babel/util.py b/babel/util.py index d113982e..c91c12b0 100644 --- a/babel/util.py +++ b/babel/util.py @@ -255,10 +255,20 @@ odict = dict class FixedOffsetTimezone(datetime.tzinfo): - """Fixed offset in minutes east from UTC.""" + """ + Fixed offset in minutes east from UTC. - def __init__(self, offset: float, name: str | None = None) -> None: + DEPRECATED: Use the standard library `datetime.timezone` instead. + """ + # TODO (Babel 3.x): Remove this class + def __init__(self, offset: float, name: str | None = None) -> None: + warnings.warn( + "`FixedOffsetTimezone` is deprecated and will be removed in a future version of Babel. " + "Use the standard library `datetime.timezone` class.", + DeprecationWarning, + stacklevel=2, + ) self._offset = datetime.timedelta(minutes=offset) if name is None: name = 'Etc/GMT%+d' % offset diff --git a/pyproject.toml b/pyproject.toml index 98ab326a..814f0b10 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,5 +50,7 @@ markers = [ ] filterwarnings = [ # The doctest for format_number would raise this, but we don't really want to see it. - "ignore:babel.numbers.format_decimal:DeprecationWarning" + "ignore:babel.numbers.format_decimal:DeprecationWarning", + # FixedOffsetTimezone is still being tested, but we don't want to see the deprecation warning. + "ignore:.*FixedOffsetTimezone:DeprecationWarning", ] diff --git a/tests/messages/test_catalog.py b/tests/messages/test_catalog.py index 692931ea..7fdc0ba5 100644 --- a/tests/messages/test_catalog.py +++ b/tests/messages/test_catalog.py @@ -12,6 +12,7 @@ import copy import datetime +import pickle import unittest from io import StringIO @@ -524,10 +525,10 @@ def test_catalog_update(): def test_datetime_parsing(): val1 = catalog._parse_datetime_header('2006-06-28 23:24+0200') - assert val1.year == 2006 - assert val1.month == 6 - assert val1.day == 28 - assert val1.tzinfo.zone == 'Etc/GMT+120' + assert val1.timetuple()[:5] == (2006, 6, 28, 23, 24) + assert val1.utctimetuple()[:5] == (2006, 6, 28, 21, 24) + assert val1.tzinfo.tzname(None) == 'Etc/GMT+120' + assert val1 == datetime.datetime(2006, 6, 28, 21, 24, tzinfo=UTC) val2 = catalog._parse_datetime_header('2006-06-28 23:24') assert val2.year == 2006 @@ -562,3 +563,16 @@ def test_update_catalog_comments(): # Auto comments will be obliterated here assert all(message.user_comments for message in catalog if message.id) + + +def test_catalog_tz_pickleable(): + """ + Test that catalogs with timezoned times are pickleable. + This would previously fail with `FixedOffsetTimezone.__init__() missing 1 required positional argument: 'offset'` + when trying to load the pickled data. + """ + pickle.loads(pickle.dumps(pofile.read_po(StringIO(r""" +msgid "" +msgstr "" +"POT-Creation-Date: 2007-04-01 15:30+0200\n" + """)))) -- 2.47.2