-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix handling of datetime with less than 3 fractional digits for …
…seconds (#14) (#16) * fix: fix handling of datetime with less than 3 fractional digits for seconds (#14) * Fix pyright error: parser is not a known member of module dateutil * Add tests for parse_time
- Loading branch information
Showing
7 changed files
with
118 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from datetime import datetime, timezone | ||
|
||
import dateutil.parser as dateutil_parser | ||
|
||
|
||
def parse_time(time_string: str) -> datetime: | ||
timestamp = dateutil_parser.isoparse(time_string) | ||
if timestamp.tzinfo is None: | ||
# The logs may come without the timezone information. We want it to be interpreted as UTC, not local time. | ||
timestamp = timestamp.replace(tzinfo=timezone.utc) | ||
return timestamp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from datetime import datetime, timedelta, timezone | ||
|
||
import pytest | ||
|
||
from aidial_analytics_realtime.time import parse_time | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"time_string, expected", | ||
[ | ||
( | ||
"2011-12-03T10:15:30", | ||
datetime(2011, 12, 3, 10, 15, 30, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2011-12-03T10:15:30+01:00", | ||
datetime( | ||
2011, 12, 3, 10, 15, 30, tzinfo=timezone(timedelta(hours=1)) | ||
), | ||
), | ||
( | ||
"2011-12-03T10:15:30.1", | ||
datetime(2011, 12, 3, 10, 15, 30, 100000, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2011-12-03T10:15:30.12", | ||
datetime(2011, 12, 3, 10, 15, 30, 120000, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2011-12-03T10:15:30.123", | ||
datetime(2011, 12, 3, 10, 15, 30, 123000, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2011-12-03T10:15:30.1234", | ||
datetime(2011, 12, 3, 10, 15, 30, 123400, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2011-12-03T10:15:30.12345", | ||
datetime(2011, 12, 3, 10, 15, 30, 123450, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2011-12-03T10:15:30.123456", | ||
datetime(2011, 12, 3, 10, 15, 30, 123456, tzinfo=timezone.utc), | ||
), | ||
( | ||
"2011-12-03T10:15:30.1234567", | ||
datetime(2011, 12, 3, 10, 15, 30, 123456, tzinfo=timezone.utc), | ||
), # Python's datetime supports up to microsecond precision | ||
], | ||
) | ||
def test_parse_time(time_string: str, expected: datetime): | ||
assert parse_time(time_string) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"time_string", | ||
[ | ||
"2011-12-03T10:15:30.", # No fractional part | ||
"2011-12-03T10:15:30+01:00[Europe/Paris]", # Named timezones are not supported | ||
], | ||
) | ||
def test_parse_time_should_fail(time_string: str): | ||
with pytest.raises(ValueError): | ||
parse_time(time_string) |