Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use zoneinfo for timezone handling instead of pytz #1179

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have the proper coverage for all timezones? We primarily used pytz to get greater coverage of time zones for the sake of tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just compared them and it does have the same coverage as pytz (Windows 11). However, from the zoneinfo docs:

The zoneinfo module does not directly provide time zone data, and instead pulls time zone information from the system time zone database or the first-party PyPI package tzdata, if available. Some systems, including notably Windows systems, do not have an IANA database available, and so for projects targeting cross-platform compatibility that require time zone data, it is recommended to declare a dependency on tzdata. If neither system data nor tzdata are available, all calls to ZoneInfo will raise ZoneInfoNotFoundError.



def assert_datetime_equality(dt1, dt2, within=10):
Expand Down
Loading