cpython
cpython copied to clipboard
gh-128074: Add support `+HH` format as time zone in `datetime.strptime`
I made the minute (MM) part optional for time zone.
New behavior:
>>> from datetime import datetime
>>> datetime.strptime("2023-05-25T15:35:05.666+11", "%Y-%m-%dT%H:%M:%S.%f%z")
datetime.datetime(2023, 5, 25, 15, 35, 5, 666000, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600)))
Old behavior:
>>> from datetime import datetime
>>> datetime.strptime("2023-05-25T15:35:05.666+11", "%Y-%m-%dT%H:%M:%S.%f%z")
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
datetime.strptime("2023-05-25T15:35:05.666+11", "%Y-%m-%dT%H:%M:%S.%f%z")
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../cpython/Lib/_strptime.py", line 700, in _strptime_datetime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
File ".../cpython/Lib/_strptime.py", line 453, in _strptime
raise ValueError("time data %r does not match format %r" %
(data_string, format))
ValueError: time data '2023-05-25T15:35:05.666+11' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'
- Issue: gh-128074
📚 Documentation preview 📚: https://cpython-previews--130390.org.readthedocs.build/