diff --git a/logprep/util/time.py b/logprep/util/time.py index 32a209289..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) / 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..4599f6fda 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