Skip to content

Commit da72c43

Browse files
committed
init %C support
1 parent 2f7634c commit da72c43

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Lib/_strptime.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ def __init__(self, locale_time=None):
371371
# W is set below by using 'U'
372372
'y': r"(?P<y>\d\d)",
373373
'Y': r"(?P<Y>\d\d\d\d)",
374+
# follow C99 specification of only parsing two digits for
375+
# first hundred centuries
376+
'C': r"(?P<C>\d\d)",
374377
# See gh-121237: "z" must support colons for backwards compatibility.
375378
'z': r"(?P<z>([+-]\d\d:?[0-5]\d(:?[0-5]\d(\.\d{1,6})?)?)|(?-i:Z))?",
376379
':z': r"(?P<colon_z>([+-]\d\d:[0-5]\d(:[0-5]\d(\.\d{1,6})?)?)|(?-i:Z))?",
@@ -415,7 +418,6 @@ def __init__(self, locale_time=None):
415418
'Op': mapping['p'],
416419
'W': mapping['U'].replace('U', 'W'),
417420
})
418-
mapping['W'] = mapping['U'].replace('U', 'W')
419421

420422
base.__init__(mapping)
421423
base.__setitem__('D', self.pattern('%m/%d/%y'))
@@ -621,6 +623,15 @@ def parse_int(s):
621623
month = locale_time.f_month.index(found_dict['B'].lower())
622624
elif group_key == 'b':
623625
month = locale_time.a_month.index(found_dict['b'].lower())
626+
elif group_key == 'C' and 'y' not in found_dict:
627+
# C99 support for century in [0,99] (years 0-9999).
628+
century = parse_int(found_dict[group_key])
629+
year = century * 100
630+
if century > 0:
631+
year = century * 100
632+
else:
633+
# ValueError fix, since MINYEAR = 1 in Lib/_pydatetime.py
634+
year = 1
624635
elif group_key == 'd':
625636
day = parse_int(found_dict['d'])
626637
elif group_key == 'H':

Lib/test/test_strptime.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,13 @@ def test_strptime_D_format(self):
670670
time.strptime(test_date, "%m/%d/%y")
671671
)
672672

673+
def test_strptime_C_format(self):
674+
test_century = "21"
675+
self.assertEqual(
676+
time.strptime(test_century, "%C").tm_year,
677+
int(test_century)*100
678+
)
679+
673680
class Strptime12AMPMTests(unittest.TestCase):
674681
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
675682

Lib/test/test_time.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ def test_strptime(self):
358358
# Should be able to go round-trip from strftime to strptime without
359359
# raising an exception.
360360
tt = time.gmtime(self.t)
361-
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'D', 'F', 'H', 'I',
362-
'j', 'm', 'M', 'p', 'S', 'T',
361+
for directive in ('a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'F', 'H',
362+
'I', 'j', 'm', 'M', 'p', 'S', 'T',
363363
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
364364
format = '%' + directive
365365
if directive == 'd':

0 commit comments

Comments
 (0)