Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
da72c43
init %C support
jyalim Feb 17, 2026
f2d1080
merge
jyalim Mar 14, 2026
fca4b98
tests passed
jyalim Mar 15, 2026
58947df
remove trailing whitespace
jyalim Mar 15, 2026
590b2b8
📜🤖 Added by blurb_it.
blurb-it[bot] Mar 15, 2026
ea86e32
Add %C to Py3.15 implemention list on doc page
jyalim Mar 15, 2026
ed548ae
better comment for %C regex C99 compat
jyalim Mar 15, 2026
1c54250
Merge remote-tracking branch 'upstream/main' into fix-issue-140715--C
jyalim Mar 28, 2026
071d28e
remove trailing whitespace in comment
jyalim Mar 29, 2026
b2a57c4
Merge remote-tracking branch 'upstream/main' into fix-issue-140715--C
jyalim Mar 29, 2026
eb163e4
remove exception branch
jyalim Mar 31, 2026
3eece9e
remove variables
jyalim Mar 31, 2026
77280db
remove variables from tests
jyalim Mar 31, 2026
c2ef261
simplify and make %C logic consistent
jyalim Mar 31, 2026
6de7669
Merge remote-tracking branch 'upstream' into fix-issue-140715--C
jyalim Mar 31, 2026
b3a5046
merge upstream
jyalim Apr 21, 2026
83b4ba3
Merge remote-tracking branch 'upstream' into fix-issue-140715--C
jyalim May 22, 2026
90e4a15
restore e
jyalim May 23, 2026
5bf1155
fix non-year directive error; add supporting tests
jyalim May 23, 2026
54bd9c1
roll python version to 3.16 for %C implementation in Doc
jyalim May 23, 2026
5c23952
Merge remote-tracking branch 'upstream' into fix-issue-140715--C
jyalim May 23, 2026
db98815
Merge remote-tracking branch 'upstream' into fix-issue-140715--C
jyalim Aug 8, 2026
08f47c7
merge upstream
jyalim Nov 11, 2026
726cc39
restored `%Nf` line in doc
jyalim Nov 12, 2026
37bf74f
Merge remote-tracking branch 'upstream' into fix-issue-140715--C
jyalim Nov 12, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2565,9 +2565,11 @@ requires, and these work on all supported platforms.
| | || Di 16 Aug 21:30:00 | |
| | | 1988 (de_DE) | |
+-----------+--------------------------------+------------------------+-------+
| ``%C`` | The year divided by 100 and | 01, 02, ..., 99 | \(0) |
| ``%C`` | The year divided by 100 and | 01, 02, ..., 99 | |
| | truncated to an integer as a | | |
| | zero-padded decimal number. | | |
| | For :meth:`!strptime`, | | |
| | zero-padding is not required. | | |
+-----------+--------------------------------+------------------------+-------+
| ``%d`` | Day of the month as a | 01, 02, ..., 31 | \(9), |
| | zero-padded decimal number. | | \(10) |
Expand Down Expand Up @@ -2726,6 +2728,7 @@ differences between platforms in handling of unsupported format specifiers.

.. versionadded:: 3.16
``%Nf`` was added.
``%C`` was added for :meth:`~.datetime.strptime`.


Technical detail
Expand Down
10 changes: 8 additions & 2 deletions Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ def __init__(self, locale_time=None):
# W is set below by using 'U'
'y': r"(?P<y>\d\d)",
'Y': r"(?P<Y>\d\d\d\d)",
# follow C99 specification of parsing up to two digits for
# first hundred centuries, without requiring zero-padding

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this, I fear not requiring padding may be messy for users since our parser is greedy. I suggest we require them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current expression, (?P<C>\d{1,2}) is consistent with C's strptime and the C99+ specification allowing a leading zero but not requiring it. I can easily change it to (?P<C>\d{2}) and specify that leading zeros are required, but again, this will then be inconsistent with C99. Would you still like to proceed with the suggested change?

'C': r"(?P<C>\d{1,2})",
# See gh-121237: "z" must support colons for backwards compatibility.
'z': r"(?P<z>([+-]\d\d:?[0-5]\d(:?[0-5]\d(\.\d{1,6})?)?)|(?-i:Z))?",
':z': r"(?P<colon_z>([+-]\d\d:[0-5]\d(:[0-5]\d(\.\d{1,6})?)?)|(?-i:Z))?",
Expand Down Expand Up @@ -420,7 +423,6 @@ def __init__(self, locale_time=None):
'Op': mapping['p'],
'W': mapping['U'].replace('U', 'W'),
})
mapping['W'] = mapping['U'].replace('U', 'W')

@jyalim jyalim Mar 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also brought this dictionary update into the update above it for greater stylistic consistency.


base.__init__(mapping)
base.__setitem__('D', self.pattern('%m/%d/%y'))
Expand Down Expand Up @@ -471,7 +473,7 @@ def pattern(self, format):
def repl(m):
directive = m.group()[1:] # exclude `%` symbol
match directive:
case 'Y' | 'y' | 'G':
case 'Y' | 'y' | 'G' | 'C':
nonlocal year_in_format
year_in_format = True
case 'd':
Expand Down Expand Up @@ -628,6 +630,10 @@ def parse_int(s):
year += 2000
else:
year += 1900
elif group_key == 'C' and 'y' not in found_dict:
# C99 support for century in [0,99] (years 0-9999).
century = parse_int(found_dict[group_key])
year = century * 100
elif group_key == 'Y':
year = int(found_dict['Y'])
elif group_key == 'G':
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,34 @@ def test_strptime_n_and_t_format(self):
self.theclass(2026, 2, 3),
)

def test_strptime_C_format(self):
# verify zero-padding, modern cent., last supported cent.
for c in ('01', '20', '99'):
expected_year = int(c) * 100
with self.subTest(format_directive="C", century=c):
self.assertEqual(
self.theclass.strptime(c, "%C"),
self.theclass(expected_year, 1, 1)
)

def test_strptime_C_y_format(self):
# verify %y correctly augmented by century %C
for y in ('0001', '1687', '1991', '2026'):
with self.subTest(format_directive="%C%y", year=y):
self.assertEqual(
self.theclass.strptime(y, "%C%y"),
self.theclass(int(y), 1, 1)
)

def test_strptime_C_d_format(self):
# verify %C provides a year directive, allowing days in format
for day_directive in ('%d', '%e'):
format_directive = f"%C-%m-{day_directive}"
with self.subTest(format_directive=format_directive):
self.assertEqual(
self.theclass.strptime('20-05-22', format_directive),
self.theclass(2000, 5, 22)
)

#############################################################################
# datetime tests
Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,35 @@ def test_strptime_n_and_t_format(self):
),
)

def test_strptime_C_format(self):
# verify zero-padding, modern cent., last supported cent.
for c in ('01', '20', '99'):
expected_year = int(c) * 100
with self.subTest(format_directive="C", century=c):
self.assertEqual(
time.strptime(c, "%C"),
time.strptime(f'{expected_year:04d}-01-01', '%Y-%m-%d'),
)

def test_strptime_C_y_format(self):
# verify %y correctly augmented by century %C
for year in ('0001', '1687', '1991', '2026'):
with self.subTest(format_directive="%C%y", year=year):
self.assertEqual(
time.strptime(year, '%C%y'),
time.strptime(f'{year}-01-01', '%Y-%m-%d'),
)

def test_strptime_C_d_format(self):
# verify %C provides a year directive, allowing days in format
for day_directive in ('%d', '%e'):
format_directive = f"%C-%m-{day_directive}"
with self.subTest(format_directive=format_directive):
self.assertEqual(
time.strptime('20-05-22', format_directive),
time.strptime(f'2000-05-22', '%Y-%m-%d'),
)

class Strptime12AMPMTests(unittest.TestCase):
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""

Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,10 @@ def test_strptime(self):
# Should be able to go round-trip from strftime to strptime without
# raising an exception.
tt = time.gmtime(self.t)
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'D', 'e', 'F', 'H', 'I',
'j', 'm', 'M', 'n', 'p', 'S', 't', 'T',
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
for directive in ('a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e',
'F', 'H', 'I', 'j', 'm', 'M', 'n', 'p', 'S',
't', 'T', 'U', 'w', 'W', 'x', 'X', 'y', 'Y',
'Z', '%'):
format = '%' + directive
if directive in ('d', 'e'):
format += ',%Y' # Avoid GH-70647.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``%C`` support to :meth:`~datetime.datetime.strptime`.
Loading