Skip to content

gh-152079: Fix C datetime.fromisoformat() dropping sub-second UTC offset - #152087

Merged
StanFromIreland merged 2 commits into
python:mainfrom
tonghuaroot:fix-c-datetime-fromisoformat-subsecond-offset
Jun 25, 2026
Merged

StanFromIreland merged 2 commits into
python:mainfrom
tonghuaroot:fix-c-datetime-fromisoformat-subsecond-offset

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

The C accelerator's tzinfo_from_isoformat_results() collapsed any UTC offset with a zero
whole-second part to timezone.utc, discarding the parsed sub-second microseconds. As a
result '2020-06-15T12:34:56+00:00:00.000001' round-tripped through isoformat() /
fromisoformat() lost its 1-microsecond offset, while the pure-Python implementation
preserved it.

This short-circuits to timezone.utc only when both the whole-second and sub-second parts
are zero. A plain +00:00 offset still returns timezone.utc; the non-zero sub-second
case now falls through to the existing new_timezone(new_delta(...)) path and is
preserved, 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-Python
implementation.

…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.

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread Modules/_datetimemodule.c Resolved
@StanFromIreland StanFromIreland added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Jun 25, 2026
@StanFromIreland
StanFromIreland merged commit 6f9c76d into python:main Jun 25, 2026
56 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @StanFromIreland for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jun 25, 2026

Copy link
Copy Markdown

GH-152174 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Jun 25, 2026
@bedevere-app

bedevere-app Bot commented Jun 25, 2026

Copy link
Copy Markdown

GH-152175 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jun 25, 2026
@bedevere-app

bedevere-app Bot commented Jun 25, 2026

Copy link
Copy Markdown

GH-152176 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Jun 25, 2026
@StanFromIreland

Copy link
Copy Markdown
Member

Merged, thanks.

StanFromIreland added a commit that referenced this pull request Jun 25, 2026
…econd tz offset (GH-152087) (#152176)

(cherry picked from commit 6f9c76d)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
StanFromIreland added a commit that referenced this pull request Jun 25, 2026
…econd tz offset (GH-152087) (#152174)

(cherry picked from commit 6f9c76d)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
StanFromIreland added a commit that referenced this pull request Jun 25, 2026
…econd tz offset (GH-152087) (#152175)

(cherry picked from commit 6f9c76d)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants