From 388866fa93baf691c0794f6adc8134e726adc387 Mon Sep 17 00:00:00 2001 From: Will Thong Date: Thu, 27 Jul 2023 18:15:45 +0100 Subject: [PATCH] Replace pytz dependency in tests External dependency `pytz` has already been removed from the main code, but this PR also removes it from the tests. Why have I changed the test timezone from CET to Europe/London? The tests were written with the [W3 examples](http://www.w3.org/TR/NOTE-datetime) in mind, but not accounting for the summertime offset. As such, they originally assumed that datetime(year=1997, month=7, day=16, hour=19, minute=20, tzinfo=ZoneInfo("CET")) would evaluate to 1997-07-16T19:20+01:00. But it doesn't; in summertime (July), CET is actually +02:00 and an example of a +01:00 timezone would be "Europe/London". With the shift to the built in ZoneInfo module, the test therefore needs to be adapted. --- RELEASE.md | 4 ++++ docs/install.rst | 1 - pelican/contents.py | 6 +++--- pelican/tests/test_utils.py | 22 +++++++++++++--------- pelican/utils.py | 6 +++--- 5 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 RELEASE.md diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000000..c62c8078bf --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,4 @@ +Release type: patch + +External dependency `pytz` has already been removed from the main code, but this PR also +removes it from the tests. diff --git a/docs/install.rst b/docs/install.rst index cdc17bb960..ea47311f80 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -64,7 +64,6 @@ automatically installed without any action on your part: * `pygments `_, for syntax highlighting * `docutils `_, for supporting reStructuredText as an input format -* `pytz `_, for timezone definitions * `blinker `_, an object-to-object and broadcast signaling system * `unidecode `_, for ASCII diff --git a/pelican/contents.py b/pelican/contents.py index b756f92dbf..4541e2ae5c 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -9,9 +9,9 @@ from urllib.parse import unquote, urljoin, urlparse, urlunparse try: - import zoneinfo + from zoneinfo import ZoneInfo except ModuleNotFoundError: - from backports import zoneinfo + from backports.zoneinfo import ZoneInfo from pelican.plugins import signals @@ -127,7 +127,7 @@ def __init__(self, content, metadata=None, settings=None, # manage timezone default_timezone = settings.get("TIMEZONE", "UTC") timezone = getattr(self, "timezone", default_timezone) - self.timezone = zoneinfo.ZoneInfo(timezone) + self.timezone = ZoneInfo(timezone) if hasattr(self, 'date'): self.date = set_date_tzinfo(self.date, timezone) diff --git a/pelican/tests/test_utils.py b/pelican/tests/test_utils.py index 2dab2ab447..4771870889 100644 --- a/pelican/tests/test_utils.py +++ b/pelican/tests/test_utils.py @@ -1,3 +1,4 @@ +from datetime import timezone import locale import logging import os @@ -6,7 +7,10 @@ from sys import platform from tempfile import mkdtemp -import pytz +try: + from zoneinfo import ZoneInfo +except ModuleNotFoundError: + from backports.zoneinfo import ZoneInfo from pelican import utils from pelican.generators import TemplatePagesGenerator @@ -50,21 +54,21 @@ def test_get_date(self): year=2012, month=11, day=22, hour=22, minute=11) date_hour_z = utils.SafeDatetime( year=2012, month=11, day=22, hour=22, minute=11, - tzinfo=pytz.timezone('UTC')) + tzinfo=timezone.utc) date_hour_est = utils.SafeDatetime( year=2012, month=11, day=22, hour=22, minute=11, - tzinfo=pytz.timezone('EST')) + tzinfo=ZoneInfo("EST")) date_hour_sec = utils.SafeDatetime( year=2012, month=11, day=22, hour=22, minute=11, second=10) date_hour_sec_z = utils.SafeDatetime( year=2012, month=11, day=22, hour=22, minute=11, second=10, - tzinfo=pytz.timezone('UTC')) + tzinfo=timezone.utc) date_hour_sec_est = utils.SafeDatetime( year=2012, month=11, day=22, hour=22, minute=11, second=10, - tzinfo=pytz.timezone('EST')) + tzinfo=ZoneInfo("EST")) date_hour_sec_frac_z = utils.SafeDatetime( year=2012, month=11, day=22, hour=22, minute=11, second=10, - microsecond=123000, tzinfo=pytz.timezone('UTC')) + microsecond=123000, tzinfo=timezone.utc) dates = { '2012-11-22': date, '2012/11/22': date, @@ -86,13 +90,13 @@ def test_get_date(self): iso_8601_date = utils.SafeDatetime(year=1997, month=7, day=16) iso_8601_date_hour_tz = utils.SafeDatetime( year=1997, month=7, day=16, hour=19, minute=20, - tzinfo=pytz.timezone('CET')) + tzinfo=ZoneInfo("Europe/London")) iso_8601_date_hour_sec_tz = utils.SafeDatetime( year=1997, month=7, day=16, hour=19, minute=20, second=30, - tzinfo=pytz.timezone('CET')) + tzinfo=ZoneInfo("Europe/London")) iso_8601_date_hour_sec_ms_tz = utils.SafeDatetime( year=1997, month=7, day=16, hour=19, minute=20, second=30, - microsecond=450000, tzinfo=pytz.timezone('CET')) + microsecond=450000, tzinfo=ZoneInfo("Europe/London")) iso_8601 = { '1997-07-16': iso_8601_date, '1997-07-16T19:20+01:00': iso_8601_date_hour_tz, diff --git a/pelican/utils.py b/pelican/utils.py index 8d91c487d4..d8cf15b47c 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -19,9 +19,9 @@ import dateutil.parser try: - import zoneinfo + from zoneinfo import ZoneInfo except ModuleNotFoundError: - from backports import zoneinfo + from backports.zoneinfo import ZoneInfo from markupsafe import Markup @@ -921,7 +921,7 @@ def file_watcher(path): def set_date_tzinfo(d, tz_name=None): """Set the timezone for dates that don't have tzinfo""" if tz_name and not d.tzinfo: - timezone = zoneinfo.ZoneInfo(tz_name) + timezone = ZoneInfo(tz_name) d = d.replace(tzinfo=timezone) return SafeDatetime( d.year, d.month, d.day, d.hour, d.minute, d.second, d.microsecond, d.tzinfo