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 authored Oct 11, 2023
1 parent 2e2eb62 commit ccfa214
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


from flow.record.base import FieldType

Expand Down Expand Up @@ -50,9 +55,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

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

0 comments on commit ccfa214

Please sign in to comment.