Skip to content

Commit d99f3fc

Browse files
gh-140715: Add %F format code support to strptime() (GH-140647)
Also: add tests for the `%T` format code Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
1 parent aa6ed80 commit d99f3fc

File tree

6 files changed

+35
-5
lines changed

6 files changed

+35
-5
lines changed

Doc/library/datetime.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,7 +2540,7 @@ requires, and these work on all supported platforms.
25402540
| ``%e`` | The day of the month as a | ␣1, ␣2, ..., 31 | |
25412541
| | space-padded decimal number. | | |
25422542
+-----------+--------------------------------+------------------------+-------+
2543-
| ``%F`` | Equivalent to ``%Y-%m-%d``, | 2025-10-11, | \(0) |
2543+
| ``%F`` | Equivalent to ``%Y-%m-%d``, | 2025-10-11, | |
25442544
| | the ISO 8601 format. | 1001-12-30 | |
25452545
+-----------+--------------------------------+------------------------+-------+
25462546
| ``%g`` | Last 2 digits of ISO 8601 year | 00, 01, ..., 99 | \(0) |
@@ -2673,10 +2673,10 @@ differences between platforms in handling of unsupported format specifiers.
26732673
``%G``, ``%u`` and ``%V`` were added.
26742674

26752675
.. versionadded:: 3.12
2676-
``%:z`` was added for :meth:`~.datetime.strftime`
2676+
``%:z`` was added for :meth:`~.datetime.strftime`.
26772677

26782678
.. versionadded:: 3.15
2679-
``%:z`` was added for :meth:`~.datetime.strptime`
2679+
``%:z`` and ``%F`` 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__('F', self.pattern('%Y-%m-%d'))
421422
base.__setitem__('T', self.pattern('%H:%M:%S'))
422423
base.__setitem__('R', self.pattern('%H:%M'))
423424
base.__setitem__('r', self.pattern(self.locale_time.LC_time_ampm))

Lib/test/datetimetester.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,13 @@ def test_fromisocalendar_type_errors(self):
21932193
with self.assertRaises(TypeError):
21942194
self.theclass.fromisocalendar(*isocal)
21952195

2196+
def test_strptime_F_format(self):
2197+
test_date = "2025-10-26"
2198+
self.assertEqual(
2199+
self.theclass.strptime(test_date, "%F"),
2200+
self.theclass.strptime(test_date, "%Y-%m-%d")
2201+
)
2202+
21962203

21972204
#############################################################################
21982205
# datetime tests
@@ -3780,6 +3787,13 @@ def test_repr_subclass(self):
37803787
td = SubclassDatetime(2010, 10, 2, second=3)
37813788
self.assertEqual(repr(td), "SubclassDatetime(2010, 10, 2, 0, 0, 3)")
37823789

3790+
def test_strptime_T_format(self):
3791+
test_time = "15:00:00"
3792+
self.assertEqual(
3793+
self.theclass.strptime(test_time, "%T"),
3794+
self.theclass.strptime(test_time, "%H:%M:%S")
3795+
)
3796+
37833797

37843798
class TestSubclassDateTime(TestDateTime):
37853799
theclass = SubclassDatetime

Lib/test/test_strptime.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,20 @@ def test_mar1_comes_after_feb29_even_when_omitting_the_year(self):
649649
time.strptime("Feb 29", "%b %d"),
650650
time.strptime("Mar 1", "%b %d"))
651651

652+
def test_strptime_F_format(self):
653+
test_date = "2025-10-26"
654+
self.assertEqual(
655+
time.strptime(test_date, "%F"),
656+
time.strptime(test_date, "%Y-%m-%d")
657+
)
658+
659+
def test_strptime_T_format(self):
660+
test_time = "15:00:00"
661+
self.assertEqual(
662+
time.strptime(test_time, "%T"),
663+
time.strptime(test_time, "%H:%M:%S")
664+
)
665+
652666
class Strptime12AMPMTests(unittest.TestCase):
653667
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
654668

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', 'H', 'I',
362-
'j', 'm', 'M', 'p', 'S',
361+
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'F', 'H', 'I',
362+
'j', 'm', 'M', 'p', 'S', 'T',
363363
'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
364364
format = '%' + directive
365365
if directive == 'd':
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``'%F'`` support to :meth:`~datetime.datetime.strptime`.

0 commit comments

Comments
 (0)