Skip to content

Commit

Permalink
use zoneinfo instead of pytz (#1179)
Browse files Browse the repository at this point in the history
Co-authored-by: Jad Chaar <jadchaar@users.noreply.github.com>
  • Loading branch information
saranti and jadchaar authored Nov 17, 2024
1 parent d05ca1d commit 2aaafcd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dynamic = ["version"]

[project.optional-dependencies]
test = [
"backports.zoneinfo==0.2.1;python_version<'3.9'",
"dateparser==1.*",
"pre-commit",
"pytest",
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements-tests.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-r requirements.txt
backports.zoneinfo==0.2.1;python_version<'3.9'
dateparser==1.*
pre-commit
pytest
Expand Down
15 changes: 15 additions & 0 deletions tests/test_arrow.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
try:
import zoneinfo
except ImportError:
from backports import zoneinfo

import pickle
import sys
import time
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
try:
import zoneinfo
except ImportError:
from backports import zoneinfo

from datetime import datetime, timezone

import pytest
import pytz
from dateutil import tz as dateutil_tz

from arrow import (
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit 2aaafcd

Please sign in to comment.