summaryrefslogtreecommitdiffstats
path: root/Lib/_strptime.py
diff options
context:
space:
mode:
authorNoor Michael <nsmichael31@gmail.com>2021-03-03 16:58:57 (GMT)
committerGitHub <noreply@github.com>2021-03-03 16:58:57 (GMT)
commit04f6fbb6969e9860783b9ab4dc24b6fe3c6dab8d (patch)
tree7f6d18531f446b876b2f375f1a341a18a2a56775 /Lib/_strptime.py
parent3b4b2cf418707c79f96689e401e3c703c0fdd4d2 (diff)
downloadcpython-04f6fbb6969e9860783b9ab4dc24b6fe3c6dab8d.zip
cpython-04f6fbb6969e9860783b9ab4dc24b6fe3c6dab8d.tar.gz
cpython-04f6fbb6969e9860783b9ab4dc24b6fe3c6dab8d.tar.bz2
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
Diffstat (limited to 'Lib/_strptime.py')
-rw-r--r--Lib/_strptime.py2
1 files changed, 1 insertions, 1 deletions
diff --git a/Lib/_strptime.py b/Lib/_strptime.py
index 5df37f5..b97dfcc 100644
--- a/Lib/_strptime.py
+++ b/Lib/_strptime.py
@@ -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'),