From 92e1675740f041f2e1e12ec47ac4e323e2db9320 Mon Sep 17 00:00:00 2001 From: Miauwkeru Date: Mon, 9 Oct 2023 12:09:44 +0000 Subject: [PATCH 1/4] Add behaviour to always use datetime.UTC if there is no zoneinfo available --- flow/record/fieldtypes/__init__.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/flow/record/fieldtypes/__init__.py b/flow/record/fieldtypes/__init__.py index f636df7..685dbba 100644 --- a/flow/record/fieldtypes/__init__.py +++ b/flow/record/fieldtypes/__init__.py @@ -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 + from flow.record.base import FieldType @@ -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 + tz = os.environ.get("FLOW_RECORD_TZ", default_tz) if tz.upper() == "NONE": return None + try: return ZoneInfo(tz) except ZoneInfoNotFoundError as exc: From 7b9f958578a83eeb0eaf561b41ded8dd5f11adc5 Mon Sep 17 00:00:00 2001 From: Miauwkeru Date: Mon, 9 Oct 2023 13:59:56 +0000 Subject: [PATCH 2/4] Fix linting --- flow/record/fieldtypes/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flow/record/fieldtypes/__init__.py b/flow/record/fieldtypes/__init__.py index 685dbba..ec5d774 100644 --- a/flow/record/fieldtypes/__init__.py +++ b/flow/record/fieldtypes/__init__.py @@ -62,7 +62,7 @@ def flow_record_tz(*, default_tz: str = "UTC") -> Optional[ZoneInfo | UTC]: tz = os.environ.get("FLOW_RECORD_TZ", default_tz) if tz.upper() == "NONE": return None - + try: return ZoneInfo(tz) except ZoneInfoNotFoundError as exc: From 7d63d783ff31513bd13b76c793347b755dfcd09c Mon Sep 17 00:00:00 2001 From: Miauwkeru Date: Tue, 10 Oct 2023 09:04:17 +0000 Subject: [PATCH 3/4] Add suggestions --- flow/record/fieldtypes/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/flow/record/fieldtypes/__init__.py b/flow/record/fieldtypes/__init__.py index ec5d774..568253d 100644 --- a/flow/record/fieldtypes/__init__.py +++ b/flow/record/fieldtypes/__init__.py @@ -21,7 +21,6 @@ from backports.zoneinfo import ZoneInfo, ZoneInfoNotFoundError HAS_ZONE_INFO = True except ImportError: - warnings.waring("Could not use zoneinfo, using the default timezone 'UTC'.") HAS_ZONE_INFO = False @@ -56,13 +55,15 @@ 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 tz = os.environ.get("FLOW_RECORD_TZ", default_tz) if tz.upper() == "NONE": return None + if not HAS_ZONE_INFO: + 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: From 4a69b7f92a3fda00bc93ae5ff957336b24a40e7b Mon Sep 17 00:00:00 2001 From: Miauwkeru Date: Tue, 10 Oct 2023 13:18:55 +0200 Subject: [PATCH 4/4] Update flow/record/fieldtypes/__init__.py Co-authored-by: Yun Zheng Hu --- flow/record/fieldtypes/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flow/record/fieldtypes/__init__.py b/flow/record/fieldtypes/__init__.py index 568253d..fe31d2d 100644 --- a/flow/record/fieldtypes/__init__.py +++ b/flow/record/fieldtypes/__init__.py @@ -61,7 +61,8 @@ def flow_record_tz(*, default_tz: str = "UTC") -> Optional[ZoneInfo | UTC]: return None if not HAS_ZONE_INFO: - warnings.warn("Cannot use FLOW_RECORD_TZ due to missing zoneinfo module, defaulting to 'UTC'.") + if tz != "UTC": + warnings.warn("Cannot use FLOW_RECORD_TZ due to missing zoneinfo module, defaulting to 'UTC'.") return UTC try: