forked from datafold/data-diff
-
Notifications
You must be signed in to change notification settings - Fork 1
fix: replace fragile datetime string slicing with robust parser #20
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2d81900
fix: replace fragile datetime string slicing with robust parser (#6)
dtsong 3d513b4
fix: address review feedback for datetime parsing
dtsong 7a68568
fix: remove incorrect skipif and add missing edge case tests
dtsong 42fc013
fix: address remaining review suggestions
dtsong 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from datetime import datetime, timedelta, timezone | ||
|
|
||
| import pytest | ||
|
|
||
| from data_diff.databases.base import _parse_datetime | ||
|
|
||
|
|
||
| class TestParseDatetime: | ||
| def test_standard_microsecond_format(self): | ||
| result = _parse_datetime("2022-06-03 12:24:35.123456") | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35, 123456) | ||
|
|
||
| def test_millisecond_precision(self): | ||
| result = _parse_datetime("2022-06-03 12:24:35.123") | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35, 123000) | ||
|
|
||
| def test_no_fractional_seconds(self): | ||
| result = _parse_datetime("2022-06-03 12:24:35") | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35) | ||
|
|
||
| def test_nanosecond_precision_truncated(self): | ||
| result = _parse_datetime("2022-06-03 12:24:35.123456789") | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35, 123456) | ||
|
|
||
| def test_seven_fractional_digits_truncated(self): | ||
| result = _parse_datetime("2022-06-03 12:24:35.1234567") | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35, 123456) | ||
|
|
||
| def test_trailing_whitespace(self): | ||
| result = _parse_datetime("2022-06-03 12:24:35.123456 ") | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35, 123456) | ||
|
|
||
| def test_leading_whitespace(self): | ||
| result = _parse_datetime(" 2022-06-03 12:24:35.123456") | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35, 123456) | ||
|
|
||
| def test_timezone_offset(self): | ||
| result = _parse_datetime("2022-06-03T12:24:35+00:00") | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35, tzinfo=timezone.utc) | ||
|
|
||
| def test_z_suffix_utc(self): | ||
| result = _parse_datetime("2022-06-03T12:24:35Z") | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35, tzinfo=timezone.utc) | ||
|
|
||
| def test_nanosecond_precision_with_timezone(self): | ||
| result = _parse_datetime("2022-06-03T12:24:35.123456789+05:30") | ||
| expected_tz = timezone(timedelta(hours=5, minutes=30)) | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35, 123456, tzinfo=expected_tz) | ||
|
|
||
| def test_nanosecond_precision_with_z_suffix(self): | ||
| result = _parse_datetime("2022-06-03T12:24:35.123456789Z") | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35, 123456, tzinfo=timezone.utc) | ||
|
|
||
| def test_all_nines_nanoseconds_truncates_not_rounds(self): | ||
| result = _parse_datetime("2022-06-03 23:59:59.999999999") | ||
| assert result == datetime(2022, 6, 3, 23, 59, 59, 999999) | ||
|
|
||
| def test_negative_timezone_offset(self): | ||
| result = _parse_datetime("2022-06-03T12:24:35-05:00") | ||
| expected_tz = timezone(timedelta(hours=-5)) | ||
| assert result == datetime(2022, 6, 3, 12, 24, 35, tzinfo=expected_tz) | ||
|
|
||
| def test_trailing_dot_raises(self): | ||
| with pytest.raises(ValueError): | ||
| _parse_datetime("2022-06-03T12:24:35.") | ||
|
|
||
| def test_invalid_string_raises(self): | ||
| with pytest.raises(ValueError): | ||
| _parse_datetime("not-a-date") | ||
|
|
||
| def test_empty_string_raises(self): | ||
| with pytest.raises(ValueError): | ||
| _parse_datetime("") |
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.
pyproject.tomlsupports Python 3.10, butdatetime.fromisoformat()on 3.10 rejects RFC3339 UTC strings ending inZ._parse_datetime()now keeps timezone suffixes after truncating fractional digits, so inputs like2022-06-03T12:24:35.123456789ZraiseValueErrorin supported runtimes when a driver returns UTC timestamps inZform; the previousres[:23]path dropped that suffix and parsed successfully. This turns previously accepted query results into runtime failures forquery(..., datetime).Useful? React with 👍 / 👎.