Skip to content

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

Open
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:fix-c-datetime-fromisoformat-subsecond-offset
Open

gh-152079: Fix C datetime.fromisoformat() dropping sub-second UTC offset#152087
tonghuaroot wants to merge 1 commit 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

datetime.fromisoformat() (C) drops the sub-second part of a UTC offset, breaking round-trips

1 participant