Skip to content

Conversation

@donBarbos
Copy link
Contributor

@donBarbos donBarbos commented Feb 21, 2025

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'

📚 Documentation preview 📚: https://cpython-previews--130390.org.readthedocs.build/

Copy link
Member

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has conflicts now.

@donBarbos
Copy link
Contributor Author

After adding the %:z directive, I merged the main branch here and changed the regex for %:z so that it could also only read HH format
cc @pganssle I think this PR could be reviewed after adding %:z directive

@donBarbos donBarbos changed the title gh-128074: Add support +HH format as time zone in datetime.strptime gh-128074: Support +HH format as time zone for strptime Sep 19, 2025
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.

2 participants