Skip to content

Commit

Permalink
Replace pytz dependency with zoneinfo. Fix #2958 (#3161)
Browse files Browse the repository at this point in the history
  • Loading branch information
willthong committed Jul 26, 2023
1 parent 715c056 commit 1d2bf8e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 deletions.
15 changes: 10 additions & 5 deletions pelican/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import logging
import os
import re
from datetime import timezone
from html import unescape
from urllib.parse import unquote, urljoin, urlparse, urlunparse

import pytz
try:
import zoneinfo
except ModuleNotFoundError:
from backports import zoneinfo


from pelican.plugins import signals
from pelican.settings import DEFAULT_CONFIG
Expand Down Expand Up @@ -120,9 +125,9 @@ def __init__(self, content, metadata=None, settings=None,
self.date_format = self.date_format[1]

# manage timezone
default_timezone = settings.get('TIMEZONE', 'UTC')
timezone = getattr(self, 'timezone', default_timezone)
self.timezone = pytz.timezone(timezone)
default_timezone = settings.get("TIMEZONE", "UTC")
timezone = getattr(self, "timezone", default_timezone)
self.timezone = zoneinfo.ZoneInfo(timezone)

if hasattr(self, 'date'):
self.date = set_date_tzinfo(self.date, timezone)
Expand Down Expand Up @@ -525,7 +530,7 @@ def __init__(self, *args, **kwargs):
if self.date.tzinfo is None:
now = datetime.datetime.now()
else:
now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
now = datetime.datetime.utcnow().replace(tzinfo=timezone.utc)
if self.date > now:
self.status = 'draft'

Expand Down
20 changes: 11 additions & 9 deletions pelican/tools/pelican_quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

from jinja2 import Environment, FileSystemLoader

import pytz
try:
import zoneinfo
except ModuleNotFoundError:
from backports import zoneinfo

try:
import readline # NOQA
Expand All @@ -17,8 +20,8 @@
try:
import tzlocal
_DEFAULT_TIMEZONE = tzlocal.get_localzone().zone
except ImportError:
_DEFAULT_TIMEZONE = 'Europe/Rome'
except ModuleNotFoundError:
_DEFAULT_TIMEZONE = "Europe/Rome"

from pelican import __version__

Expand Down Expand Up @@ -158,16 +161,15 @@ def ask(question, answer=str, default=None, length=None):

def ask_timezone(question, default, tzurl):
"""Prompt for time zone and validate input"""
lower_tz = [tz.lower() for tz in pytz.all_timezones]
tz_dict = {tz.lower(): tz for tz in zoneinfo.available_timezones()}
while True:
r = ask(question, str, default)
r = r.strip().replace(' ', '_').lower()
if r in lower_tz:
r = pytz.all_timezones[lower_tz.index(r)]
r = r.strip().replace(" ", "_").lower()
if r in tz_dict.keys():
r = tz_dict[r]
break
else:
print('Please enter a valid time zone:\n'
' (check [{}])'.format(tzurl))
print("Please enter a valid time zone:\n" " (check [{}])".format(tzurl))
return r


Expand Down
15 changes: 9 additions & 6 deletions pelican/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

import dateutil.parser

try:
import zoneinfo
except ModuleNotFoundError:
from backports import zoneinfo
from markupsafe import Markup

import pytz


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -919,10 +921,11 @@ 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:
tz = pytz.timezone(tz_name)
d = tz.localize(d)
return SafeDatetime(d.year, d.month, d.day, d.hour, d.minute, d.second,
d.microsecond, d.tzinfo)
timezone = zoneinfo.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
)
return d


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ feedgenerator = ">=1.9"
jinja2 = ">=2.7"
pygments = ">=2.6"
python-dateutil = ">=2.8"
pytz = ">=2020.1"
rich = ">=10.1"
unidecode = ">=1.1"
markdown = {version = ">=3.1", optional = true}
backports-zoneinfo = {version = "^0.2.1", python = "<3.9"}

[tool.poetry.dev-dependencies]
BeautifulSoup4 = "^4.9"
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
version = "4.8.0"

requires = ['feedgenerator >= 1.9', 'jinja2 >= 2.7', 'pygments',
'docutils>=0.15', 'pytz >= 0a', 'blinker', 'unidecode',
'python-dateutil', 'rich']
'docutils>=0.15', 'blinker', 'unidecode', 'python-dateutil',
'rich', 'backports-zoneinfo[tzdata] >= 0.2; python_version<"3.9"']

entry_points = {
'console_scripts': [
Expand Down

0 comments on commit 1d2bf8e

Please sign in to comment.