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 (#86)

Add behaviour to always use datetime.UTC if there is no zoneinfo available

---------

Co-authored-by: Yun Zheng Hu <hu@fox-it.com>
  • Loading branch information
Miauwkeru and yunzheng committed Oct 11, 2023
1 parent 5e0c39e commit 26574bf
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions flow/record/fieldtypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
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
HAS_ZONE_INFO = False

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

View check run for this annotation

Codecov / codecov/patch

flow/record/fieldtypes/__init__.py#L24

Added line #L24 was not covered by tests


from flow.record.base import FieldType

Expand Down Expand Up @@ -47,9 +52,16 @@ 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.
"""

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

if not HAS_ZONE_INFO:
if tz != "UTC":
warnings.warn("Cannot use FLOW_RECORD_TZ due to missing zoneinfo module, defaulting to 'UTC'.")
return UTC

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

View check run for this annotation

Codecov / codecov/patch

flow/record/fieldtypes/__init__.py#L61-L63

Added lines #L61 - L63 were not covered by tests

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

0 comments on commit 26574bf

Please sign in to comment.