fix: correctly handle negative UTC offsets in isoformat - #137
Open
binggao1230 wants to merge 1 commit into
Open
Conversation
When a datetime with sub-microsecond precision (femtoseconds or yoctoseconds) had a negative UTC offset (e.g. -01:00), isoformat() placed the offset in the middle of the fractional-seconds string instead of appending it at the end. The old code split the base isoformat string on "+" to separate the UTC offset, so negative offsets (which use "-") were never detected and the offset ended up embedded inside the result: '2025-01-01T00:00:00-01:00.000000000000000000000040' Fix: use a regex to find the UTC offset suffix ([+-]HH:MM or [+-]HH:MM:SS) at the end of the string before inserting the high-precision fractional part. Fixes ni#52.
Collaborator
|
@gaoflow, thank you for contributing. Please use the project's pull request template, like you did in #136 |
Author
|
Sorry about that — description rewritten using the template, same as #136. Rechecked on current master while I was in there: 408 tests pass, lint and mypy clean, 29/29 CI green. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this Pull Request accomplish?
Fixes #52.
hightime.datetime.isoformat()built its result by splitting the basedatetime.isoformat()string on"+"to separate the UTC offset, then re-joining with"+". Negative offsets use"-", so the split found nothing and the high-precision fractional seconds were appended after the offset instead of after the seconds:This replaces the split with a regex match for the offset suffix (
[+-]HH:MMor[+-]HH:MM:SS) at the end of the string, so the fractional part is inserted after the seconds and the offset is re-attached last. It also removes thexfailfrom the existing negative-offset case intests/test_datetime.py, which now passes.Why should this Pull Request be merged?
The output was not a valid ISO 8601 string and could not be parsed back, for any sub-microsecond datetime west of UTC. Positive offsets and naive datetimes were unaffected, which is why it went unnoticed. The repository already had a test for the case, marked
xfailagainst #52.What testing has been done?
python -m pytest tests/test_datetime.py -k isoformat— 12 passed; the negative-offset case fails onmasterwith thexfailremoved, confirming the test covers the fixpython -m pytest tests/— 408 passedni-python-styleguide lint— cleanmypy— Success: no issues found in 8 source files