From 9d674ca6aa4c77345fce62156a522e500fc17951 Mon Sep 17 00:00:00 2001 From: saranti Date: Sun, 7 Jul 2024 19:09:17 +1000 Subject: [PATCH] use zoneinfo instead of pytz --- pyproject.toml | 1 + requirements/requirements-tests.txt | 1 + tests/test_arrow.py | 15 +++++++++++++++ tests/test_formatter.py | 8 ++++++-- tests/utils.py | 9 ++++++--- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5ccc497a1..5ebb1b920 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,6 +41,7 @@ dynamic = ["version"] [project.optional-dependencies] test = [ + "backports.zoneinfo==0.2.1;python_version<'3.9'", "dateparser==1.*", "pre-commit", "pytest", diff --git a/requirements/requirements-tests.txt b/requirements/requirements-tests.txt index 77005e0b5..c2b7dcfd3 100644 --- a/requirements/requirements-tests.txt +++ b/requirements/requirements-tests.txt @@ -1,4 +1,5 @@ -r requirements.txt +backports.zoneinfo==0.2.1;python_version<'3.9' dateparser==1.* pre-commit pytest diff --git a/tests/test_arrow.py b/tests/test_arrow.py index 507c1ab0f..0b64008a5 100644 --- a/tests/test_arrow.py +++ b/tests/test_arrow.py @@ -1,3 +1,8 @@ +try: + import zoneinfo +except ImportError: + from backports import zoneinfo + import pickle import sys import time @@ -67,6 +72,16 @@ def test_init_pytz_timezone(self): assert result._datetime == self.expected assert_datetime_equality(result._datetime, self.expected, 1) + def test_init_zoneinfo_timezone(self): + result = arrow.Arrow( + 2024, 7, 10, 18, 55, 45, 999999, tzinfo=zoneinfo.ZoneInfo("Europe/Paris") + ) + self.expected = datetime( + 2024, 7, 10, 18, 55, 45, 999999, tzinfo=zoneinfo.ZoneInfo("Europe/Paris") + ) + assert result._datetime == self.expected + assert_datetime_equality(result._datetime, self.expected, 1) + def test_init_with_fold(self): before = arrow.Arrow(2017, 10, 29, 2, 0, tzinfo="Europe/Stockholm") after = arrow.Arrow(2017, 10, 29, 2, 0, tzinfo="Europe/Stockholm", fold=1) diff --git a/tests/test_formatter.py b/tests/test_formatter.py index 0b6c256cf..09ae0d75f 100644 --- a/tests/test_formatter.py +++ b/tests/test_formatter.py @@ -1,7 +1,11 @@ +try: + import zoneinfo +except ImportError: + from backports import zoneinfo + from datetime import datetime import pytest -import pytz from dateutil import tz as dateutil_tz from arrow import ( @@ -124,7 +128,7 @@ def test_timezone(self): @pytest.mark.parametrize("full_tz_name", make_full_tz_list()) def test_timezone_formatter(self, full_tz_name): # This test will fail if we use "now" as date as soon as we change from/to DST - dt = datetime(1986, 2, 14, tzinfo=pytz.timezone("UTC")).replace( + dt = datetime(1986, 2, 14, tzinfo=zoneinfo.ZoneInfo("UTC")).replace( tzinfo=dateutil_tz.gettz(full_tz_name) ) abbreviation = dt.tzname() diff --git a/tests/utils.py b/tests/utils.py index 95b47c166..7a74b7e46 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,11 +1,14 @@ -import pytz +try: + import zoneinfo +except ImportError: + from backports import zoneinfo from dateutil.zoneinfo import get_zonefile_instance def make_full_tz_list(): dateutil_zones = set(get_zonefile_instance().zones) - pytz_zones = set(pytz.all_timezones) - return dateutil_zones.union(pytz_zones) + zoneinfo_zones = set(zoneinfo.available_timezones()) + return dateutil_zones.union(zoneinfo_zones) def assert_datetime_equality(dt1, dt2, within=10):