Skip to content

Commit

Permalink
add dynamic scaling of time representations (#690)
Browse files Browse the repository at this point in the history
* fixed issue 687

* blacked
  • Loading branch information
Malutthias authored Oct 30, 2024
1 parent a36c759 commit 8806109
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion logprep/util/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/util/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8806109

Please sign in to comment.