diff --git a/Lib/_strptime.py b/Lib/_strptime.py index d011ddf8b181c3..8b62ea734b7d11 100644 --- a/Lib/_strptime.py +++ b/Lib/_strptime.py @@ -418,6 +418,7 @@ def __init__(self, locale_time=None): 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)) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 7df27206206268..6abb704f08865f 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -1212,6 +1212,20 @@ def test_strptime_leap_year(self): date.strptime('20-03-14', '%y-%m-%d') date.strptime('02-29,2024', '%m-%d,%Y') + def test_strptime_C99_shorthand_year_month_day(self): + formats = dict(short="%F",long="%Y-%m-%d") + test_date = "2025-10-26" + shorthand = datetime.strptime(test_date,formats["short"]) + long_hand = datetime.strptime(test_date,formats["long"]) + self.assertEqual(shorthand,long_hand) + + def test_strptime_C99_shorthand_hour_minute_second(self): + formats = dict(short="%T",long="%H:%M:%S") + test_time = "15:00:00" + shorthand = datetime.strptime(test_time,formats["short"]) + long_hand = datetime.strptime(test_time,formats["long"]) + self.assertEqual(shorthand,long_hand) + class SubclassDate(date): sub_var = 1 diff --git a/Lib/test/test_strptime.py b/Lib/test/test_strptime.py index 40e114aada67eb..02617a58130335 100644 --- a/Lib/test/test_strptime.py +++ b/Lib/test/test_strptime.py @@ -649,6 +649,22 @@ def test_mar1_comes_after_feb29_even_when_omitting_the_year(self): time.strptime("Feb 29", "%b %d"), time.strptime("Mar 1", "%b %d")) + def test_shorthand_year_month_day(self): + # Test that token '%F' is equivalent to '%Y-%m-%d' + formats = dict(short="%F",long="%Y-%m-%d") + test_date = "2025-10-26" + shorthand = time.strptime(test_date,formats["short"]) + long_hand = time.strptime(test_date,formats["long"]) + self.assertEqual(shorthand,long_hand) + + def test_shorthand_hour_minute_second(self): + # Test that token '%T' is equivalent to '%H:%M:%S' + formats = dict(short="%T",long="%H:%M:%S") + test_time = "15:00:00" + shorthand = time.strptime(test_time,formats["short"]) + long_hand = time.strptime(test_time,formats["long"]) + self.assertEqual(shorthand,long_hand) + class Strptime12AMPMTests(unittest.TestCase): """Test a _strptime regression in '%I %p' at 12 noon (12 PM)""" 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 index 00000000000000..19d281c7cc9c49 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-27-00-13-04.gh-issue-140715.WkozE0.rst @@ -0,0 +1 @@ +Add `'%F'` support to :meth:`~datetime.datetime.strptime`.