Skip to content

Commit 5bf1155

Browse files
committed
fix non-year directive error; add supporting tests
1 parent 90e4a15 commit 5bf1155

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

Lib/_strptime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def pattern(self, format):
471471
def repl(m):
472472
directive = m.group()[1:] # exclude `%` symbol
473473
match directive:
474-
case 'Y' | 'y' | 'G':
474+
case 'Y' | 'y' | 'G' | 'C':
475475
nonlocal year_in_format
476476
year_in_format = True
477477
case 'd':

Lib/test/datetimetester.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,16 @@ def test_strptime_C_y_format(self):
22492249
self.theclass(int(y), 1, 1)
22502250
)
22512251

2252+
def test_strptime_C_d_format(self):
2253+
# verify %C provides a year directive, allowing days in format
2254+
for day_directive in ('%d', '%e'):
2255+
format_directive = f"%C-%m-{day_directive}"
2256+
with self.subTest(format_directive=format_directive):
2257+
self.assertEqual(
2258+
self.theclass.strptime('20-05-22', format_directive),
2259+
self.theclass(2000, 5, 22)
2260+
)
2261+
22522262
#############################################################################
22532263
# datetime tests
22542264

Lib/test/test_strptime.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,16 @@ def test_strptime_C_y_format(self):
701701
time.strptime(f'{year}-01-01', '%Y-%m-%d'),
702702
)
703703

704+
def test_strptime_C_d_format(self):
705+
# verify %C provides a year directive, allowing days in format
706+
for day_directive in ('%d', '%e'):
707+
format_directive = f"%C-%m-{day_directive}"
708+
with self.subTest(format_directive=format_directive):
709+
self.assertEqual(
710+
time.strptime('20-05-22', format_directive),
711+
time.strptime(f'2000-05-22', '%Y-%m-%d'),
712+
)
713+
704714
class Strptime12AMPMTests(unittest.TestCase):
705715
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
706716

0 commit comments

Comments
 (0)