Skip to content

Commit

Permalink
Add behaviour to always use datetime.UTC if there is no zoneinfo avai…
Browse files Browse the repository at this point in the history
…lable
  • Loading branch information
Miauwkeru committed Oct 9, 2023
1 parent 2e2eb62 commit 92e1675
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions flow/record/fieldtypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
from urllib.parse import urlparse

try:
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
try:
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
except ImportError:
from backports.zoneinfo import ZoneInfo, ZoneInfoNotFoundError
HAS_ZONE_INFO = True
except ImportError:
from backports.zoneinfo import ZoneInfo, ZoneInfoNotFoundError
warnings.waring("Could not use zoneinfo, using the default timezone 'UTC'.")
HAS_ZONE_INFO = False

Check warning on line 25 in flow/record/fieldtypes/__init__.py

View check run for this annotation

Codecov / codecov/patch

flow/record/fieldtypes/__init__.py#L24-L25

Added lines #L24 - L25 were not covered by tests


from flow.record.base import FieldType

Expand Down Expand Up @@ -50,9 +56,13 @@ def flow_record_tz(*, default_tz: str = "UTC") -> Optional[ZoneInfo | UTC]:
Returns:
None if ``FLOW_RECORD_TZ=NONE`` otherwise ``ZoneInfo(FLOW_RECORD_TZ)`` or ``UTC`` if ZoneInfo is not found.
"""
if not HAS_ZONE_INFO:
return UTC

Check warning on line 60 in flow/record/fieldtypes/__init__.py

View check run for this annotation

Codecov / codecov/patch

flow/record/fieldtypes/__init__.py#L60

Added line #L60 was not covered by tests

tz = os.environ.get("FLOW_RECORD_TZ", default_tz)
if tz.upper() == "NONE":
return None

try:
return ZoneInfo(tz)
except ZoneInfoNotFoundError as exc:
Expand Down

0 comments on commit 92e1675

Please sign in to comment.