From 8a3ca21c90b21b38af2abd8a66a757e8d17228bd Mon Sep 17 00:00:00 2001 From: Ma Lu Date: Tue, 29 Oct 2024 15:53:34 +0100 Subject: [PATCH 1/2] fixed issue 687 --- logprep/util/time.py | 2 +- tests/unit/util/test_time.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/logprep/util/time.py b/logprep/util/time.py index 32a209289..4a01f5894 100644 --- a/logprep/util/time.py +++ b/logprep/util/time.py @@ -134,7 +134,7 @@ def parse_datetime( The parsed timestamp as datetime object. """ if source_format == "UNIX": - parsed_datetime = int(timestamp) if len(timestamp) <= 10 else int(timestamp) / 1000 + parsed_datetime = int(timestamp) if len(timestamp) <= 10 else int(timestamp) / 10 ** (len(timestamp) - 10) parsed_datetime = cls.from_timestamp(parsed_datetime) elif source_format == "ISO8601": parsed_datetime = cls.from_string(timestamp, set_missing_utc=False) diff --git a/tests/unit/util/test_time.py b/tests/unit/util/test_time.py index 13e33707b..f8d93d8fc 100644 --- a/tests/unit/util/test_time.py +++ b/tests/unit/util/test_time.py @@ -168,3 +168,29 @@ def test_parse_datetime_unix_timestamp_is_always_utc( assert timestamp.tzinfo.tzname(timestamp) == expected_timezone_name for attribute, value in expected.items(): assert getattr(timestamp, attribute) == value + + @pytest.mark.parametrize( + "timestamp, expected_timezone_name, expected", + [ + ( + "1615634593", + "UTC", + {"year": 2021, "month": 3, "day": 13, "hour": 11, "minute": 23, "second": 13}, + ), + ( + "1615634593000", + "UTC", + {"year": 2021, "month": 3, "day": 13, "hour": 11, "minute": 23, "second": 13}, + ), + ( + "1615634593000000", + "UTC", + {"year": 2021, "month": 3, "day": 13, "hour": 11, "minute": 23, "second": 13}, + ), + ], + ) + def test_parse_unix_timestamp(self, timestamp, expected_timezone_name, expected): + parsed_timestamp = TimeParser.parse_datetime(timestamp, "UNIX", ZoneInfo("UTC")) + assert parsed_timestamp.tzinfo.tzname(parsed_timestamp) == expected_timezone_name + for attribute, value in expected.items(): + assert getattr(parsed_timestamp, attribute) == value From d02fef34e9550a686d69a44f7b98f09a42a5d35c Mon Sep 17 00:00:00 2001 From: Ma Lu Date: Tue, 29 Oct 2024 15:57:31 +0100 Subject: [PATCH 2/2] blacked --- logprep/util/time.py | 6 +++++- tests/unit/util/test_time.py | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/logprep/util/time.py b/logprep/util/time.py index 4a01f5894..66046ca22 100644 --- a/logprep/util/time.py +++ b/logprep/util/time.py @@ -134,7 +134,11 @@ def parse_datetime( The parsed timestamp as datetime object. """ if source_format == "UNIX": - parsed_datetime = int(timestamp) if len(timestamp) <= 10 else int(timestamp) / 10 ** (len(timestamp) - 10) + parsed_datetime = ( + int(timestamp) + if len(timestamp) <= 10 + else int(timestamp) / 10 ** (len(timestamp) - 10) + ) parsed_datetime = cls.from_timestamp(parsed_datetime) elif source_format == "ISO8601": parsed_datetime = cls.from_string(timestamp, set_missing_utc=False) diff --git a/tests/unit/util/test_time.py b/tests/unit/util/test_time.py index f8d93d8fc..4599f6fda 100644 --- a/tests/unit/util/test_time.py +++ b/tests/unit/util/test_time.py @@ -168,22 +168,22 @@ def test_parse_datetime_unix_timestamp_is_always_utc( assert timestamp.tzinfo.tzname(timestamp) == expected_timezone_name for attribute, value in expected.items(): assert getattr(timestamp, attribute) == value - + @pytest.mark.parametrize( "timestamp, expected_timezone_name, expected", [ ( - "1615634593", + "1615634593", "UTC", {"year": 2021, "month": 3, "day": 13, "hour": 11, "minute": 23, "second": 13}, ), ( - "1615634593000", + "1615634593000", "UTC", {"year": 2021, "month": 3, "day": 13, "hour": 11, "minute": 23, "second": 13}, ), ( - "1615634593000000", + "1615634593000000", "UTC", {"year": 2021, "month": 3, "day": 13, "hour": 11, "minute": 23, "second": 13}, ),