mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
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
This commit is contained in:
parent
3b4b2cf418
commit
04f6fbb696
3 changed files with 4 additions and 1 deletions
|
@ -201,7 +201,7 @@ class TimeRE(dict):
|
|||
#XXX: Does 'Y' need to worry about having less or more than
|
||||
# 4 digits?
|
||||
'Y': r"(?P<Y>\d\d\d\d)",
|
||||
'z': r"(?P<z>[+-]\d\d:?[0-5]\d(:?[0-5]\d(\.\d{1,6})?)?|Z)",
|
||||
'z': r"(?P<z>[+-]\d\d:?[0-5]\d(:?[0-5]\d(\.\d{1,6})?)?|(?-i:Z))",
|
||||
'A': self.__seqToRE(self.locale_time.f_weekday, 'A'),
|
||||
'a': self.__seqToRE(self.locale_time.a_weekday, 'a'),
|
||||
'B': self.__seqToRE(self.locale_time.f_month[1:], 'B'),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue