gh-152079: Fix C datetime.fromisoformat() dropping sub-second UTC offset#152087
Open
tonghuaroot wants to merge 1 commit into
Open
gh-152079: Fix C datetime.fromisoformat() dropping sub-second UTC offset#152087tonghuaroot wants to merge 1 commit into
tonghuaroot wants to merge 1 commit into
Conversation
…TC offset The C accelerator's tzinfo_from_isoformat_results() collapsed any offset with a zero whole-second part to timezone.utc, discarding the parsed sub-second microseconds. So '+00:00:00.000001' round-tripped through isoformat()/fromisoformat() lost its 1-microsecond offset, while pure Python preserved it. Only short-circuit to UTC when both the whole-second and sub-second parts are zero.
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.
The C accelerator's
tzinfo_from_isoformat_results()collapsed any UTC offset with a zerowhole-second part to
timezone.utc, discarding the parsed sub-second microseconds. As aresult
'2020-06-15T12:34:56+00:00:00.000001'round-tripped throughisoformat()/fromisoformat()lost its 1-microsecond offset, while the pure-Python implementationpreserved it.
This short-circuits to
timezone.utconly when both the whole-second and sub-second partsare zero. A plain
+00:00offset still returnstimezone.utc; the non-zero sub-secondcase now falls through to the existing
new_timezone(new_delta(...))path and ispreserved, matching pure Python.
I verified against a full C-vs-pure-Python differential corpus that the only inputs whose
behaviour changes are exactly these zero-whole-second sub-second offsets (75 previously
divergent cases now agree) and that no new divergence is introduced.
A round-trip regression test is added; it runs under both the C (
_Fast) and pure-Python(
_Pure) test classes.Fixes #152079. Sibling to #152060, a separate
fromisoformat()defect in the pure-Pythonimplementation.