Skip to content

Commit

Permalink
Switched to using built-in zoneinfo in favour of pytz
Browse files Browse the repository at this point in the history
* On python >= 3.9, the builtin package zoneinfo will is used
* On python < 3.9, the extra package `backports.zoneinfo` is used
  • Loading branch information
GLEF1X committed Nov 7, 2022
1 parent 5057d0f commit c0c0b92
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
23 changes: 4 additions & 19 deletions glQiwiApi/utils/date_conversion.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
import sys
from datetime import datetime, timezone
from typing import Optional

DEFAULT_QIWI_TIMEZONE = 'Europe/Moscow'

if sys.version_info < (3, 9, 0):
try:
import pytz
except ImportError:
raise RuntimeError(
'glQiwiApi requires `pytz` package for python < 3.9'
'as an alternative to a new builtin `zoneinfo`. '
'Please install pytz using pip install pytz.'
)

def to_moscow(obj: datetime) -> datetime:
return obj.astimezone(pytz.timezone(DEFAULT_QIWI_TIMEZONE))

else:
try:
import zoneinfo

def to_moscow(obj: datetime) -> datetime:
return obj.astimezone(zoneinfo.ZoneInfo(DEFAULT_QIWI_TIMEZONE))
except ImportError:
import backports.zoneinfo as zoneinfo


def datetime_to_utc_in_iso_format(obj: datetime) -> str:
Expand All @@ -37,4 +22,4 @@ def datetime_to_iso8601_with_moscow_timezone(obj: Optional[datetime]) -> str:
"""
if not isinstance(obj, datetime):
return '' # pragma: no cover
return to_moscow(obj).isoformat(timespec='seconds')
return obj.astimezone(zoneinfo.ZoneInfo(DEFAULT_QIWI_TIMEZONE)).isoformat(timespec='seconds')
77 changes: 67 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.7"
pytz = { version = "2022.6", python = "<3.9"}
"backports.zoneinfo" = { version = "^0.2.1", extras = ["tzdata"], python = "<3.9"}
aiohttp = "^3.8.3"
pydantic = "^1.10.2"
typing_extensions = { version = "^4.0.1", python = "<=3.7" }
Expand Down

0 comments on commit c0c0b92

Please sign in to comment.