Skip to content

Commit

Permalink
restore timezoner pre Python 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Jun 23, 2024
1 parent 3ff0365 commit 57dd92b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
56 changes: 56 additions & 0 deletions mkdocs_rss_plugin/timezoner_pre39.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#! python3 # noqa: E265


"""
Manage timezones for pages date(time)s using pytz module.
Meant to be dropped when Python 3.8 reaches EOL.
"""

# ############################################################################
# ########## Libraries #############
# ##################################

# standard library
from datetime import datetime

# 3rd party
import pytz
from mkdocs.plugins import get_plugin_logger

# package
from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME

# ############################################################################
# ########## Globals #############
# ################################


logger = get_plugin_logger(MKDOCS_LOGGER_NAME)


# ############################################################################
# ########## Functions ###########
# ################################


def set_datetime_zoneinfo(
input_datetime: datetime, config_timezone: str = "UTC"
) -> datetime:
"""Apply timezone to a naive datetime.
:param input_datetime: offset-naive datetime
:type input_datetime: datetime
:param config_timezone: name of timezone as registered in IANA database,
defaults to "UTC". Example : Europe/Paris.
:type config_timezone: str, optional
:return: offset-aware datetime
:rtype: datetime
"""
if input_datetime.tzinfo:
return input_datetime
elif not config_timezone:
return input_datetime.replace(tzinfo=pytz.utc)
else:
config_tz = pytz.timezone(config_timezone)
return config_tz.localize(input_datetime)
10 changes: 8 additions & 2 deletions mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# standard library
import logging
import sys
from collections.abc import Iterable
from datetime import date, datetime
from email.utils import format_datetime
Expand All @@ -15,9 +16,8 @@
from typing import Any, List, Tuple, Union
from urllib.parse import urlencode, urlparse, urlunparse

import markdown

# 3rd party
import markdown
from git import (
GitCommandError,
GitCommandNotFound,
Expand All @@ -41,6 +41,12 @@
from mkdocs_rss_plugin.models import PageInformation
from mkdocs_rss_plugin.timezoner import set_datetime_zoneinfo

# conditional imports
if sys.version_info < (3, 9):
from mkdocs_rss_plugin.timezoner_pre39 import set_datetime_zoneinfo
else:
from mkdocs_rss_plugin.timezoner import set_datetime_zoneinfo

# ############################################################################
# ########## Globals #############
# ################################
Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
GitPython>=3.1,<3.2
mkdocs>=1.5,<2
requests>=2.31,<3
pytz==2022.* ; python_version < "3.9"
tzdata==2024.* ; python_version >= "3.9" and sys_platform == "win32"
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sonar.projectKey=Guts_mkdocs-rss-plugin
# only=main

# Python versions
sonar.python.version=3.10, 3.11, 3.12
sonar.python.version=3.8, 3.9, 3.10, 3.11, 3.12

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
sonar.sources=mkdocs_rss_plugin
Expand Down

0 comments on commit 57dd92b

Please sign in to comment.