Skip to content

Commit 185a6e9

Browse files
gh-140715: Add %D format code support to strptime() (GH-144819)
* %D support for strptime, including test and Doc update * additional %D test * change documentation example date for %D so it is more legible to non-US readers * change testing date for %D so it is more legible to non-US readers * mv News blurb to Library, consistent with previous %F change * change invalid format code from %D to C-standard unused %! * Fix erroneous and misleading example Doc to %y from %Y, use correct C99+ definition for C99 %D; update additional tests --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 1797508 commit 185a6e9

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

Doc/library/datetime.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,8 +2534,8 @@ requires, and these work on all supported platforms.
25342534
| ``%d`` | Day of the month as a | 01, 02, ..., 31 | \(9) |
25352535
| | zero-padded decimal number. | | |
25362536
+-----------+--------------------------------+------------------------+-------+
2537-
| ``%D`` | Equivalent to ``%m/%d/%y``. | 11/10/2025 | \(9), |
2538-
| | | | \(0) |
2537+
| ``%D`` | Equivalent to ``%m/%d/%y``. | 11/28/25 | \(9) |
2538+
| | | | |
25392539
+-----------+--------------------------------+------------------------+-------+
25402540
| ``%e`` | The day of the month as a | ␣1, ␣2, ..., 31 | |
25412541
| | space-padded decimal number. | | |
@@ -2676,7 +2676,7 @@ differences between platforms in handling of unsupported format specifiers.
26762676
``%:z`` was added for :meth:`~.datetime.strftime`.
26772677

26782678
.. versionadded:: 3.15
2679-
``%:z`` and ``%F`` were added for :meth:`~.datetime.strptime`.
2679+
``%:z``, ``%F``, and ``%D`` were added for :meth:`~.datetime.strptime`.
26802680

26812681
Technical Detail
26822682
^^^^^^^^^^^^^^^^

Lib/_strptime.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ def __init__(self, locale_time=None):
418418
mapping['W'] = mapping['U'].replace('U', 'W')
419419

420420
base.__init__(mapping)
421+
base.__setitem__('D', self.pattern('%m/%d/%y'))
421422
base.__setitem__('F', self.pattern('%Y-%m-%d'))
422423
base.__setitem__('T', self.pattern('%H:%M:%S'))
423424
base.__setitem__('R', self.pattern('%H:%M'))

Lib/test/datetimetester.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,13 @@ def test_strptime_F_format(self):
22002200
self.theclass.strptime(test_date, "%Y-%m-%d")
22012201
)
22022202

2203+
def test_strptime_D_format(self):
2204+
test_date = "11/28/25"
2205+
self.assertEqual(
2206+
self.theclass.strptime(test_date, "%D"),
2207+
self.theclass.strptime(test_date, "%m/%d/%y")
2208+
)
2209+
22032210

22042211
#############################################################################
22052212
# datetime tests

Lib/test/test_strptime.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def test_ValueError(self):
286286
def test_strptime_exception_context(self):
287287
# check that this doesn't chain exceptions needlessly (see #17572)
288288
with self.assertRaises(ValueError) as e:
289-
_strptime._strptime_time('', '%D')
289+
_strptime._strptime_time('', '%!')
290290
self.assertTrue(e.exception.__suppress_context__)
291291
# additional check for stray % branch
292292
with self.assertRaises(ValueError) as e:
@@ -663,6 +663,13 @@ def test_strptime_T_format(self):
663663
time.strptime(test_time, "%H:%M:%S")
664664
)
665665

666+
def test_strptime_D_format(self):
667+
test_date = "11/28/25"
668+
self.assertEqual(
669+
time.strptime(test_date, "%D"),
670+
time.strptime(test_date, "%m/%d/%y")
671+
)
672+
666673
class Strptime12AMPMTests(unittest.TestCase):
667674
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
668675

Lib/test/test_time.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ 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', 'F', 'H', 'I',
361+
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'D', 'F', 'H', 'I',
362362
'j', 'm', 'M', 'p', 'S', 'T',
363363
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
364364
format = '%' + directive
@@ -379,7 +379,7 @@ def test_strptime_bytes(self):
379379
def test_strptime_exception_context(self):
380380
# check that this doesn't chain exceptions needlessly (see #17572)
381381
with self.assertRaises(ValueError) as e:
382-
time.strptime('', '%D')
382+
time.strptime('', '%!')
383383
self.assertTrue(e.exception.__suppress_context__)
384384
# additional check for stray % branch
385385
with self.assertRaises(ValueError) as e:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``'%D'`` support to :meth:`~datetime.datetime.strptime`.

0 commit comments

Comments
 (0)