Skip to content

[3.9] bpo-43295: Fix error handling of datetime.strptime format string '%z' (GH-24627)#25695

Merged
pganssle merged 1 commit into
python:3.9from
miss-islington:backport-04f6fbb-3.9
May 20, 2021
Merged

[3.9] bpo-43295: Fix error handling of datetime.strptime format string '%z' (GH-24627)#25695
pganssle merged 1 commit into
python:3.9from
miss-islington:backport-04f6fbb-3.9

Conversation

@miss-islington

@miss-islington miss-islington commented Apr 28, 2021

Copy link
Copy Markdown
Contributor

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
(cherry picked from commit 04f6fbb)

Co-authored-by: Noor Michael nsmichael31@gmail.com

https://bugs.python.org/issue43295

…pythonGH-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
(cherry picked from commit 04f6fbb)

Co-authored-by: Noor Michael <nsmichael31@gmail.com>

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

I created a new backport, the previous one was stuck (PR #24728).

@iritkatriel

Copy link
Copy Markdown
Member

Should this be merged?

@pganssle pganssle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@iritkatriel I believe so! Thanks!

@pganssle pganssle merged commit c87b81d into python:3.9 May 20, 2021
@bedevere-bot

Copy link
Copy Markdown

@pganssle: Please replace # with GH- in the commit message next time. Thanks!

@miss-islington miss-islington deleted the backport-04f6fbb-3.9 branch May 20, 2021 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants