Skip to content

Commit ade347d

Browse files
committed
bpo-36967: Eliminate unnecessary check in _strptime when determining AM/PM
1 parent 9ffca67 commit ade347d

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

Lib/_strptime.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,18 +399,18 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
399399
hour = int(found_dict['I'])
400400
ampm = found_dict.get('p', '').lower()
401401
# If there was no AM/PM indicator, we'll treat this like AM
402-
if ampm in ('', locale_time.am_pm[0]):
403-
# We're in AM so the hour is correct unless we're
404-
# looking at 12 midnight.
405-
# 12 midnight == 12 AM == hour 0
406-
if hour == 12:
407-
hour = 0
408-
elif ampm == locale_time.am_pm[1]:
402+
if ampm == locale_time.am_pm[1]:
409403
# We're in PM so we need to add 12 to the hour unless
410404
# we're looking at 12 noon.
411405
# 12 noon == 12 PM == hour 12
412406
if hour != 12:
413407
hour += 12
408+
else:
409+
# We're in AM so the hour is correct unless we're
410+
# looking at 12 midnight.
411+
# 12 midnight == 12 AM == hour 0
412+
if hour == 12:
413+
hour = 0
414414
elif group_key == 'M':
415415
minute = int(found_dict['M'])
416416
elif group_key == 'S':

Lib/test/datetimetester.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,6 +2536,12 @@ def test_strptime(self):
25362536
with self.assertRaises(ValueError): strptime("-2400", "%z")
25372537
with self.assertRaises(ValueError): strptime("-000", "%z")
25382538

2539+
def test_strptime_ampm(self):
2540+
for hour in range(0, 24):
2541+
with self.subTest(hour=hour):
2542+
hour_date = (1999, 3, 17, hour, 44, 55, 2, 76, 0)
2543+
self.assertEqual(self.theclass.strptime(_time.strftime("%I %p", hour_date), "%I %p").hour, hour)
2544+
25392545
def test_strptime_single_digit(self):
25402546
# bpo-34903: Check that single digit dates and times are allowed.
25412547

0 commit comments

Comments
 (0)