Skip to content

Commit 2bdb614

Browse files
committedAug 29, 2002
strptime(): The code that was adding 12 to PM hours was incorrect
because it added it to 12 PM too. 12 PM should be hour 12 not hour 24. Also cleaned up a minor style nit. There are more style problems in this file that I'll clean up next (but I didn't want them to overwhelm the substance of this fix).
1 parent 375e0ee commit 2bdb614

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed
 

‎Lib/_strptime.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,19 @@ def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
415415
hour = int(found_dict['H'])
416416
else:
417417
hour = int(found_dict['I'])
418-
if found_dict.has_key('p'):
419-
if found_dict['p'] == locale_time.am_pm[1]:
418+
ampm = found_dict.get('p')
419+
if ampm == locale_time.am_pm[0]:
420+
# We're in AM so the hour is correct unless we're
421+
# looking at 12 midnight.
422+
# 12 midnight == 12 AM == hour 0
423+
if hour == 12:
424+
hour = 0
425+
elif ampm == locale_time.am_pm[1]:
426+
# We're in PM so we need to add 12 to the hour unless
427+
# we're looking at 12 noon.
428+
# 12 noon == 12 PM == hour 12
429+
if hour != 12:
420430
hour += 12
421-
else:
422-
if hour is 12:
423-
hour = 0
424431
elif group_key is 'M':
425432
minute = int(found_dict['M'])
426433
elif group_key is 'S':

0 commit comments

Comments
 (0)
Please sign in to comment.