-
Notifications
You must be signed in to change notification settings - Fork 0
recognize fractional seconds with timezone offset in timestamp parser #16
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
Open
HrachShah
wants to merge
3
commits into
main
Choose a base branch
from
fix/utils-parse-combined-microsecond-timezone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,93 @@ | ||
| """Tests for log-analyzer-cli utility functions.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from datetime import datetime, timedelta, timezone | ||
|
|
||
| from log_analyzer_cli.utils import ( | ||
| _try_parse_datetime, | ||
| detect_log_level, | ||
| parse_timestamp, | ||
| ) | ||
|
|
||
|
|
||
| class TestTryParseDatetime: | ||
| """Tests for _try_parse_datetime.""" | ||
|
|
||
| def test_iso_with_microseconds_and_offset_colon(self): | ||
| # Python's datetime.isoformat() emits exactly this form | ||
| assert _try_parse_datetime("2026-06-21T19:25:00.123456+02:00") == datetime( | ||
| 2026, 6, 21, 19, 25, 0, 123456, | ||
| tzinfo=timezone(timedelta(hours=2)), | ||
| ) | ||
|
|
||
| def test_iso_with_microseconds_and_offset_no_colon(self): | ||
| # ISO 8601 also allows ±HHMM form | ||
| assert _try_parse_datetime("2026-06-21T19:25:00.123456+0200") == datetime( | ||
| 2026, 6, 21, 19, 25, 0, 123456, | ||
| tzinfo=timezone(timedelta(hours=2)), | ||
| ) | ||
|
|
||
| def test_space_separator_with_microseconds_and_offset(self): | ||
| # The space-separator variant also gets a combined format | ||
| assert _try_parse_datetime("2026-06-21 19:25:00.123456+02:00") == datetime( | ||
| 2026, 6, 21, 19, 25, 0, 123456, | ||
| tzinfo=timezone(timedelta(hours=2)), | ||
| ) | ||
|
|
||
| def test_iso_microseconds_with_z_suffix(self): | ||
| # Existing Z-substitution should still work for fractional seconds | ||
| assert _try_parse_datetime("2026-06-21T19:25:00.123Z") == datetime( | ||
| 2026, 6, 21, 19, 25, 0, 123000, | ||
| tzinfo=timezone.utc, | ||
| ) | ||
|
|
||
| def test_iso_microseconds_no_timezone(self): | ||
| # The pre-existing microsecond form must keep working | ||
| assert _try_parse_datetime("2026-06-21T19:25:00.123456") == datetime( | ||
| 2026, 6, 21, 19, 25, 0, 123456, | ||
| ) | ||
|
|
||
| def test_iso_no_microseconds_with_offset(self): | ||
| # The pre-existing offset form must keep working | ||
| assert _try_parse_datetime("2026-06-21T19:25:00+02:00") == datetime( | ||
| 2026, 6, 21, 19, 25, 0, | ||
| tzinfo=timezone(timedelta(hours=2)), | ||
| ) | ||
|
|
||
| def test_iso_no_microseconds_no_timezone(self): | ||
| # The pre-existing plain form must keep working | ||
| assert _try_parse_datetime("2026-06-21T19:25:00") == datetime( | ||
| 2026, 6, 21, 19, 25, 0, | ||
| ) | ||
|
|
||
|
|
||
| class TestParseTimestampIntegration: | ||
| """End-to-end checks through parse_timestamp.""" | ||
|
|
||
| def test_combined_form_extracts_full_datetime(self): | ||
| # parse_timestamp's first regex captures the whole ISO timestamp; | ||
| # the inner _try_parse_datetime must now handle microseconds+offset. | ||
| assert parse_timestamp( | ||
| "2026-06-21T19:25:00.123456+02:00 ERROR something failed" | ||
| ) == datetime(2026, 6, 21, 19, 25, 0, 123456, | ||
| tzinfo=timezone(timedelta(hours=2))) | ||
|
|
||
| def test_z_fractional_extracts_full_datetime(self): | ||
| assert parse_timestamp( | ||
| "2026-06-21T19:25:00.123Z ERROR something failed" | ||
| ) == datetime(2026, 6, 21, 19, 25, 0, 123000, tzinfo=timezone.utc) | ||
|
|
||
| def test_plain_form_still_parses(self): | ||
| # regression guard | ||
| assert parse_timestamp("2026-06-21 19:25:00 INFO ok") == datetime( | ||
| 2026, 6, 21, 19, 25, 0, | ||
| ) | ||
|
|
||
|
|
||
| class TestDetectLogLevel: | ||
| def test_returns_uppercase_level(self): | ||
| assert detect_log_level("2025-01-01 error: bad") == "ERROR" | ||
|
|
||
| def test_returns_unknown_for_plain_text(self): | ||
| assert detect_log_level("just a normal line") == "UNKNOWN" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space-separated timezone format without fractional seconds still drops valid timestamps.
Line 22 matches
YYYY-MM-DD HH:MM:SS+HH:MM(and...Z), but_try_parse_datetimestill has no%Y-%m-%d %H:%M:%S%zformat, so those lines returnNoneand lose timestamps.Suggested fix
formats = [ "%Y-%m-%d %H:%M:%S.%f", "%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M:%S.%f%z", "%Y-%m-%d %H:%M:%S.%f%z", "%Y-%m-%dT%H:%M:%S%z", + "%Y-%m-%d %H:%M:%S%z", "%d/%b/%Y:%H:%M:%S", "%b %d %H:%M:%S", "%Y/%m/%d %H:%M:%S", ]📝 Committable suggestion
🤖 Prompt for AI Agents