Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,18 +399,18 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
hour = 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 in ('', locale_time.am_pm[0]):
# We're in AM so the hour is correct unless we're
# looking at 12 midnight.
# 12 midnight == 12 AM == hour 0
if hour == 12:
hour = 0
elif ampm == locale_time.am_pm[1]:
if 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
if hour != 12:
hour += 12
else:
# We're in AM so the hour is correct unless we're
# looking at 12 midnight.
# 12 midnight == 12 AM == hour 0
if hour == 12:
hour = 0
elif group_key == 'M':
minute = int(found_dict['M'])
elif group_key == 'S':
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,12 @@ def test_strptime(self):
with self.assertRaises(ValueError): strptime("-2400", "%z")
with self.assertRaises(ValueError): strptime("-000", "%z")

def test_strptime_ampm(self):
for hour in range(0, 24):
with self.subTest(hour=hour):
hour_date = (1999, 3, 17, hour, 44, 55, 2, 76, 0)
self.assertEqual(self.theclass.strptime(_time.strftime("%I %p", hour_date), "%I %p").hour, hour)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is I think too long for PEP8. If I may suggest a refactoring for this function that I think makes it a bit cleaner:

    def test_strptime_ampm(self):
        dt = datetime(1999, 3, 17, 0, 44, 55, 2, 76, 0)
        for hour in range(0, 24):
            with self.subTest(hour=hour):
                new_dt = dt.replace(hour=hour)
                dt_str = new_dt.strftime("%I %p")

                self.assertEqual(self.theclass.strptime(dt_str, "%I %p").hour,
                                 hour)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I likely opted to ignore line length, since much of the rest of the file has PEP8 issues, but I can change that.

Out of curiosity, why do you feel that using dt.replace is cleaner than generating a new datetime value directly each iteration?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why do you feel that using dt.replace is cleaner than generating a new datetime value directly each iteration?

Technically replace does generate a new value every iteration (datetime is immutable), but I get what you mean. I mainly thought it was more readable because:

  1. I find the time.strftime interface to be a bit more "low level" and harder to read than an strftime call on a datetime object.
  2. Using replace(hour=hour) makes it more obvious that we didn't have any sort of off-by-one error when constructing the time tuple - it would be hard to notice in a tuple if you were actually cycling through different values for the minute instead of hour.


def test_strptime_single_digit(self):
# bpo-34903: Check that single digit dates and times are allowed.

Expand Down