Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def parse_int(s):
hour = parse_int(found_dict['I'])
ampm = found_dict.get('p', '').lower()
# If there was no AM/PM indicator, we'll treat this like AM
if ampm == locale_time.am_pm[1]:
if ampm and ampm == locale_time.am_pm[1]:
# We're in PM so we need to add 12 to the hour unless
# we're looking at 12 noon.
# 12 noon == 12 PM == hour 12
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,17 @@ def test_strptime_n_and_t_format(self):
class Strptime12AMPMTests(unittest.TestCase):
"""Test a _strptime regression in '%I %p' at 12 noon (12 PM)"""

def test_empty_am_pm(self):
locale_time = _strptime.LocaleTime()
locale_time.am_pm = ('', '')
time_re = _strptime.TimeRE(locale_time)
with (
support.swap_attr(_strptime, '_TimeRE_cache', time_re),
support.swap_attr(_strptime, '_regex_cache', {}),
):
self.assertEqual(time.strptime('01', '%I').tm_hour, 1)
self.assertEqual(time.strptime('12', '%I').tm_hour, 0)

def test_twelve_noon_midnight(self):
eq = self.assertEqual
eq(time.strptime('12 PM', '%I %p')[3], 12)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`time.strptime` interpreting 12-hour times as PM in locales
with empty AM and PM strings.
Loading