Skip to content

Python 3.15 datetime.strptime() %z behaviour change #150105

@suve

Description

@suve

Bug report

Bug description:

If you use %z in the format string for datetime.strptime(), Python 3.14.4 requires offset information to be present and will produce a ValueError if it's missing...

# Python 3.14
>>> import datetime
>>> datetime.datetime.strptime("2026-05-19T20:21:22Z", "%Y-%m-%dT%H:%M:%S%z")
datetime.datetime(2026, 5, 19, 20, 21, 22, tzinfo=datetime.timezone.utc)
>>> datetime.datetime.strptime("2026-05-19T20:21:22", "%Y-%m-%dT%H:%M:%S%z")
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    datetime.datetime.strptime("2026-05-19T20:21:22", "%Y-%m-%dT%H:%M:%S%z")
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.14/_strptime.py", line 815, in _strptime_datetime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.14/_strptime.py", line 555, in _strptime
    raise ValueError("time data %r does not match format %r" %
                     (data_string, format))
ValueError: time data '2026-05-19T20:21:22' does not match format '%Y-%m-%dT%H:%M:%S%z'

...whereas Python 3.15-beta1 treats it as optional and will create a naive datetime object, instead.

# Python 3.15-beta1
>>> import datetime
>>> datetime.datetime.strptime("2026-05-19T20:21:22Z", "%Y-%m-%dT%H:%M:%S%z")
datetime.datetime(2026, 5, 19, 20, 21, 22, tzinfo=datetime.timezone.utc)
>>> datetime.datetime.strptime("2026-05-19T20:21:22", "%Y-%m-%dT%H:%M:%S%z")
datetime.datetime(2026, 5, 19, 20, 21, 22)

Looking through the documentation for Python 3.15.0b1 for mentions of "3.15" or "%z" yields only this single note:

Added in version 3.15: %D, %F, %n, %t, and %:z were added for strptime().

This mentions %:z, but not %z. Thus, I'm not sure if this behaviour change is intentional and just needs documenting, or if it's an actual bug.

Operating System: Fedora Linux 44
CPython versions: 3.15.0b1, 3.14.4

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirpendingThe issue will be closed if no feedback is provided

    Projects

    Status

    No status

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions