Skip to content

Commit 5efc50d

Browse files
committed
Fix SF #658820, regex fixes for _strptime (Brett Cannon)
Disallow zero for days and months
1 parent 0940c62 commit 5efc50d

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Lib/_strptime.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,22 +306,23 @@ class TimeRE(dict):
306306

307307
def __init__(self, locale_time=LocaleTime()):
308308
"""Init inst with non-locale regexes and store LocaleTime object."""
309-
# XXX: should 0 be valid for:
310-
# day (d), julian day (j), month (m), and hour12 (I)?
311-
super(TimeRE,self).__init__({
309+
#XXX: Does 'Y' need to worry about having less or more than 4 digits?
310+
base = super(TimeRE, self)
311+
base.__init__({
312312
# The " \d" option is to make %c from ANSI C work
313-
'd': r"(?P<d>3[0-1]|[0-2]\d|\d| \d)",
313+
'd': r"(?P<d>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
314314
'H': r"(?P<H>2[0-3]|[0-1]\d|\d)",
315-
'I': r"(?P<I>0\d|1[0-2]|\d)",
316-
'j': r"(?P<j>(?:3[0-5]\d|36[0-6])|[0-2]\d\d|\d\d|\d)",
317-
'm': r"(?P<m>0\d|1[0-2]|\d)",
315+
'I': r"(?P<I>1[0-2]|0[1-9]|[1-9])",
316+
'j': r"(?P<j>36[0-6]|3[0-5]\d|[1-2]\d\d|0[1-9]\d|00[1-9]|[1-9]\d|0[1-9]|[1-9])",
317+
'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",
318318
'M': r"(?P<M>[0-5]\d|\d)",
319319
'S': r"(?P<S>6[0-1]|[0-5]\d|\d)",
320320
'U': r"(?P<U>5[0-3]|[0-4]\d|\d)",
321321
'w': r"(?P<w>[0-6])",
322-
'W': r"(?P<W>5[0-3]|[0-4]\d|\d)", # Same as U
322+
# W is set below by using 'U'
323323
'y': r"(?P<y>\d\d)",
324324
'Y': r"(?P<Y>\d\d\d\d)"})
325+
base.__setitem__('W', base.__getitem__('U'))
325326
self.locale_time = locale_time
326327

327328
def __getitem__(self, fetch):

0 commit comments

Comments
 (0)