]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
bpo-43295: Fix error handling of datetime.strptime format string '%z' (GH-24627)
authorNoor Michael <nsmichael31@gmail.com>
Wed, 3 Mar 2021 16:58:57 +0000 (10:58 -0600)
committerGitHub <noreply@github.com>
Wed, 3 Mar 2021 16:58:57 +0000 (08:58 -0800)
commit04f6fbb6969e9860783b9ab4dc24b6fe3c6dab8d
tree7f6d18531f446b876b2f375f1a341a18a2a56775
parent3b4b2cf418707c79f96689e401e3c703c0fdd4d2
bpo-43295: Fix error handling of datetime.strptime format string '%z' (GH-24627)

Previously, `datetime.strptime` would match `'z'` with the format string `'%z'` (for UTC offsets), throwing an `IndexError` by erroneously trying to parse `'z'` as a timestamp. As a special case, `'%z'` matches the string `'Z'` which is equivalent to the offset `'+00:00'`, however this behavior is not defined for lowercase `'z'`.

This change ensures a `ValueError` is thrown when encountering the original example, as follows:

```
>>> from datetime import datetime
>>> datetime.strptime('z', '%z')
ValueError: time data 'z' does not match format '%z'
```

Automerge-Triggered-By: GH:pganssle
Lib/_strptime.py
Lib/test/datetimetester.py
Misc/NEWS.d/next/Library/2021-02-22-22-54-40.bpo-43295.h_ffu7.rst [new file with mode: 0644]