| ``%d`` | Day of the month as a | 01, 02, ..., 31 | \(9) |
| | zero-padded decimal number. | | |
+-----------+--------------------------------+------------------------+-------+
-| ``%D`` | Equivalent to ``%m/%d/%y``. | 11/10/2025 | \(9), |
-| | | | \(0) |
+| ``%D`` | Equivalent to ``%m/%d/%y``. | 11/28/25 | \(9) |
+| | | | |
+-----------+--------------------------------+------------------------+-------+
| ``%e`` | The day of the month as a | ␣1, ␣2, ..., 31 | |
| | space-padded decimal number. | | |
``%:z`` was added for :meth:`~.datetime.strftime`.
.. versionadded:: 3.15
- ``%:z`` and ``%F`` were added for :meth:`~.datetime.strptime`.
+ ``%:z``, ``%F``, and ``%D`` were added for :meth:`~.datetime.strptime`.
Technical Detail
^^^^^^^^^^^^^^^^
mapping['W'] = mapping['U'].replace('U', 'W')
base.__init__(mapping)
+ base.__setitem__('D', self.pattern('%m/%d/%y'))
base.__setitem__('F', self.pattern('%Y-%m-%d'))
base.__setitem__('T', self.pattern('%H:%M:%S'))
base.__setitem__('R', self.pattern('%H:%M'))
self.theclass.strptime(test_date, "%Y-%m-%d")
)
+ def test_strptime_D_format(self):
+ test_date = "11/28/25"
+ self.assertEqual(
+ self.theclass.strptime(test_date, "%D"),
+ self.theclass.strptime(test_date, "%m/%d/%y")
+ )
+
#############################################################################
# datetime tests
def test_strptime_exception_context(self):
# check that this doesn't chain exceptions needlessly (see #17572)
with self.assertRaises(ValueError) as e:
- _strptime._strptime_time('', '%D')
+ _strptime._strptime_time('', '%!')
self.assertTrue(e.exception.__suppress_context__)
# additional check for stray % branch
with self.assertRaises(ValueError) as e:
time.strptime(test_time, "%H:%M:%S")
)
+ def test_strptime_D_format(self):
+ test_date = "11/28/25"
+ self.assertEqual(
+ time.strptime(test_date, "%D"),
+ time.strptime(test_date, "%m/%d/%y")
+ )
+
class Strptime12AMPMTests(unittest.TestCase):
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
# 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', 'F', 'H', 'I',
+ for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'D', 'F', 'H', 'I',
'j', 'm', 'M', 'p', 'S', 'T',
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
format = '%' + directive
def test_strptime_exception_context(self):
# check that this doesn't chain exceptions needlessly (see #17572)
with self.assertRaises(ValueError) as e:
- time.strptime('', '%D')
+ time.strptime('', '%!')
self.assertTrue(e.exception.__suppress_context__)
# additional check for stray % branch
with self.assertRaises(ValueError) as e:
--- /dev/null
+Add ``'%D'`` support to :meth:`~datetime.datetime.strptime`.