]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Use standard library `timezone` instead of `FixedOffsetTimezone` (#1203)
authorAarni Koskela <akx@iki.fi>
Fri, 21 Mar 2025 06:18:16 +0000 (08:18 +0200)
committerGitHub <noreply@github.com>
Fri, 21 Mar 2025 06:18:16 +0000 (08:18 +0200)
babel/messages/catalog.py
babel/util.py
pyproject.toml
tests/messages/test_catalog.py

index a35647f0f62e579159bb2a57119597e3b08b9abf..de96ea576a7a560db65cd069b4aa01f8863912c7 100644 (file)
@@ -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)
index d113982eed4fb2555b4f7ecc20e6a51e71a241c1..c91c12b0ff1404d3ac83f2d196e8b8310ea25a83 100644 (file)
@@ -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
index 98ab326ac7d81ffe36e3410e99d22a63314a604e..814f0b10f321b7bfaf70f0db4ee39ef33e908d55 100644 (file)
@@ -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",
 ]
index 692931ea2ba269d3e06d515e8365de629846cd5c..7fdc0ba5e413568821e35efc8122065af97b21bd 100644 (file)
@@ -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"
+    """))))