Skip to content

datetime.time.fromisoformat loses subsecond-only UTC offsets #158702

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: Python datetime documentation, time.fromisoformat: "Return a time corresponding to a time_string in any valid ISO 8601 format, with the following exceptions:" and "Time zone offsets may have fractional seconds." The time.isoformat documentation specifies that UTC offsets can be emitted as '+HH:MM:SS.ffffff'.

Expected: datetime.timedelta(microseconds=1)

Actual: datetime.timedelta(0)

import datetime as d

try:
    expected = d.timedelta(microseconds=1)
    t = d.time(12, 34, 56, tzinfo=d.timezone(expected))
    s = t.isoformat()
    if s != "12:34:56+00:00:00.000001":
        print("REFUTATION REJECTED: reported input was not reproduced")
    else:
        # Valid time and offset, in documented +HH:MM:SS.ffffff form.
        actual = d.time.fromisoformat(s).utcoffset()
        if actual != expected:
            print(f"REFUTATION CONFIRMED: {s!r} actual={actual!r} expected={expected!r}")
        else:
            print("REFUTATION REJECTED: actual equals documented expectation")
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: '12:34:56+00:00:00.000001' actual=datetime.timedelta(0) expected=datetime.timedelta(microseconds=1)

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-c2

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