diff --git a/Lib/_strptime.py b/Lib/_strptime.py index a83d0a6f344cae..152368da1099f5 100644 --- a/Lib/_strptime.py +++ b/Lib/_strptime.py @@ -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 diff --git a/Lib/test/test_strptime.py b/Lib/test/test_strptime.py index d70837ea63f547..f31069e86c8f9b 100644 --- a/Lib/test/test_strptime.py +++ b/Lib/test/test_strptime.py @@ -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) diff --git a/Misc/NEWS.d/next/Library/2026-11-25-15-22-00.gh-issue-162812.Rk4S6f.rst b/Misc/NEWS.d/next/Library/2026-11-25-15-22-00.gh-issue-162812.Rk4S6f.rst new file mode 100644 index 00000000000000..9c8a014dfb52dd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-25-15-22-00.gh-issue-162812.Rk4S6f.rst @@ -0,0 +1,2 @@ +Fix :func:`time.strptime` interpreting 12-hour times as PM in locales +with empty AM and PM strings.