Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dynamic scaling of time representations #690

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading