]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-140715: Add `%F` format code support to `strptime()` (GH-140647)
authorJason Yalim, PhD <4813268+jyalim@users.noreply.github.com>
Mon, 9 Feb 2026 12:24:15 +0000 (05:24 -0700)
committerGitHub <noreply@github.com>
Mon, 9 Feb 2026 12:24:15 +0000 (13:24 +0100)
Also: add tests for the `%T` format code

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Doc/library/datetime.rst
Lib/_strptime.py
Lib/test/datetimetester.py
Lib/test/test_strptime.py
Lib/test/test_time.py
Misc/NEWS.d/next/Library/2025-10-27-00-13-04.gh-issue-140715.WkozE0.rst [new file with mode: 0644]

index 3ab3450032abe454c29d95025dd10c63dd3f0a34..7c172471b195d64695c157b530ed372ec4754c30 100644 (file)
@@ -2540,7 +2540,7 @@ requires, and these work on all supported platforms.
 |  ``%e``   | The day of the month as a      | ␣1, ␣2, ..., 31        |       |
 |           | space-padded decimal number.   |                        |       |
 +-----------+--------------------------------+------------------------+-------+
-|  ``%F``   | Equivalent to ``%Y-%m-%d``,    | 2025-10-11,            | \(0)  |
+|  ``%F``   | Equivalent to ``%Y-%m-%d``,    | 2025-10-11,            |       |
 |           | the ISO 8601 format.           | 1001-12-30             |       |
 +-----------+--------------------------------+------------------------+-------+
 |  ``%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.
    ``%G``, ``%u`` and ``%V`` were added.
 
 .. versionadded:: 3.12
-   ``%:z`` was added for :meth:`~.datetime.strftime`
+   ``%:z`` was added for :meth:`~.datetime.strftime`.
 
 .. versionadded:: 3.15
-   ``%:z`` was added for :meth:`~.datetime.strptime`
+   ``%:z`` and ``%F`` were added for :meth:`~.datetime.strptime`.
 
 Technical Detail
 ^^^^^^^^^^^^^^^^
index d011ddf8b181c3ceb31f9d2987560fbdba4038be..8b62ea734b7d1172c68687e4fcd2ea15e275b06d 100644 (file)
@@ -418,6 +418,7 @@ class TimeRE(dict):
         mapping['W'] = mapping['U'].replace('U', 'W')
 
         base.__init__(mapping)
+        base.__setitem__('F', self.pattern('%Y-%m-%d'))
         base.__setitem__('T', self.pattern('%H:%M:%S'))
         base.__setitem__('R', self.pattern('%H:%M'))
         base.__setitem__('r', self.pattern(self.locale_time.LC_time_ampm))
index 8d39299b3ff44221f61dcc77b2c8e3b60349e36f..3784909ee77839f4405f9c4c54f6eb685d0f24a8 100644 (file)
@@ -2193,6 +2193,13 @@ class TestDate(HarmlessMixedComparison, unittest.TestCase):
                 with self.assertRaises(TypeError):
                     self.theclass.fromisocalendar(*isocal)
 
+    def test_strptime_F_format(self):
+        test_date = "2025-10-26"
+        self.assertEqual(
+            self.theclass.strptime(test_date, "%F"),
+            self.theclass.strptime(test_date, "%Y-%m-%d")
+        )
+
 
 #############################################################################
 # datetime tests
@@ -3780,6 +3787,13 @@ class TestDateTime(TestDate):
         td = SubclassDatetime(2010, 10, 2, second=3)
         self.assertEqual(repr(td), "SubclassDatetime(2010, 10, 2, 0, 0, 3)")
 
+    def test_strptime_T_format(self):
+        test_time = "15:00:00"
+        self.assertEqual(
+            self.theclass.strptime(test_time, "%T"),
+            self.theclass.strptime(test_time, "%H:%M:%S")
+        )
+
 
 class TestSubclassDateTime(TestDateTime):
     theclass = SubclassDatetime
index 40e114aada67eb8f3edb117f4c3cc0f51e3451ff..0784ea6a4cf5d42e09e2a357019998e1293db9c6 100644 (file)
@@ -649,6 +649,20 @@ class StrptimeTests(unittest.TestCase):
                 time.strptime("Feb 29", "%b %d"),
                 time.strptime("Mar 1", "%b %d"))
 
+    def test_strptime_F_format(self):
+        test_date = "2025-10-26"
+        self.assertEqual(
+            time.strptime(test_date, "%F"),
+            time.strptime(test_date, "%Y-%m-%d")
+        )
+
+    def test_strptime_T_format(self):
+        test_time = "15:00:00"
+        self.assertEqual(
+            time.strptime(test_time, "%T"),
+            time.strptime(test_time, "%H:%M:%S")
+        )
+
 class Strptime12AMPMTests(unittest.TestCase):
     """Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""
 
index c360f4a64c266b28ae1c8e3127377591d2ba2b07..da0cf494bfa8ad6ed690c34e6e43ef9b6c28f706 100644 (file)
@@ -358,8 +358,8 @@ class TimeTestCase(unittest.TestCase):
         # 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', 'H', 'I',
-                          'j', 'm', 'M', 'p', 'S',
+        for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'F', 'H', 'I',
+                          'j', 'm', 'M', 'p', 'S', 'T',
                           'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
             format = '%' + directive
             if directive == 'd':
diff --git a/Misc/NEWS.d/next/Library/2025-10-27-00-13-04.gh-issue-140715.WkozE0.rst b/Misc/NEWS.d/next/Library/2025-10-27-00-13-04.gh-issue-140715.WkozE0.rst
new file mode 100644 (file)
index 0000000..c2bb69b
--- /dev/null
@@ -0,0 +1 @@
+Add ``'%F'`` support to :meth:`~datetime.datetime.strptime`.