Support `%:z` in `datetime.datetime.strptime` · Issue #121237 · python/cpython · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Support %:z in datetime.datetime.strptime #121237

Open
@za3k

Description

@za3k

Bug report

Bug description:

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes documents "strftime() and strptime() Format Codes". It mentions the new %:z variant of %z, introduced in Python 3.12

>>> datetime.datetime.strptime("2023-07-17T13:58:49-07:00", "%Y-%m-%dT%H:%M:%S%z")
datetime.datetime(2023, 7, 17, 13, 58, 49, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=61200)))
>>> datetime.datetime.strptime("2023-07-17T13:58:49-07:00", "%Y-%m-%dT%H:%M:%S%:z")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.12/_strptime.py", line 554, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/_strptime.py", line 325, in _strptime
    raise ValueError("'%s' is a bad directive in format '%s'" %
ValueError: ':' is a bad directive in format '%Y-%m-%dT%H:%M:%S%:z'

But passing "%:z" to strptime raises an error. This is a bug because strptime and strftime should have the same format codes, according to the docs.

Since %z already parses this format when passed to strptime, I think %:z should just do the same thing.

Workaround: Change %:z to %z in your format string for strptime.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      TMZ Celebrity News – Breaking Stories, Videos & Gossip

      Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

      Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

      🎥 Watch TMZ Live

      TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.