Skip to content

datetime.fromisoformat loses fractional UTC offsets smaller than one second #158703

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: Python datetime documentation, datetime.fromisoformat(): "Return a datetime corresponding to a date_string in any valid ISO 8601 format, with the following exceptions:" followed by "Time zone offsets may have fractional seconds." datetime.isoformat() documents its aware output as "YYYY-MM-DDTHH:MM:SS.ffffff+HH:MM[:SS[.ffffff]]".

Expected: Parsing '2024-01-02T12:00:00+00:00:00.000001' preserves utcoffset() == timedelta(microseconds=1).

Actual: Parsing returns a datetime with a zero UTC offset.

import datetime as dt

try:
    offset = dt.timedelta(microseconds=1)
    d = dt.datetime(2024, 1, 2, 12, tzinfo=dt.timezone(offset))
    s = d.isoformat()
    if s != "2024-01-02T12:00:00+00:00:00.000001":
        print("REFUTATION REJECTED: input does not match documented isoformat output")
    else:
        expected = offset // dt.timedelta(microseconds=1)
        actual = dt.datetime.fromisoformat(s).utcoffset() // dt.timedelta(microseconds=1)
        if actual != expected:
            print(f"REFUTATION CONFIRMED: {s!r} actual: {actual} expected: {expected} (microseconds)")
        else:
            print("REFUTATION REJECTED: fractional-second offset preserved")
except Exception as e:
    print(f"REFUTATION REJECTED: {type(e).__name__}: {e}")

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library datetime:

REFUTATION CONFIRMED: '2024-01-02T12:00:00+00:00:00.000001' actual: 0 expected: 1 (microseconds)

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/datetime-20261003-014717-c4

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions