Skip to content
1 change: 1 addition & 0 deletions Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `'%F'` support to :meth:`~datetime.datetime.strptime`.
Loading