This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: strptime doesn't account for %p in output
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, skip.montanaro
Priority: normal Keywords:

Created on 2004-05-10 13:18 by skip.montanaro, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg20750 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2004-05-10 13:18
Correct use of the "%p" format is not obvious:

    >>> time.strptime("3:15pm", '%H:%M%p')
    (1900, 1, 1, 3, 15, 0, 0, 1, -1)
    >>> time.strptime("3:15", '%H:%M')
    (1900, 1, 1, 3, 15, 0, 0, 1, -1)
    >>> time.strptime("3:15am", '%H:%M%p')
    (1900, 1, 1, 3, 15, 0, 0, 1, -1)

Other than successfully parsing 'am' or 'pm', there's no
indication in the returned time tuple of a 12-hour
offset in
the 'pm' case.  I think the hour field returned from
the first call
should be 15, not 3.
msg20751 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2004-05-10 13:21
Logged In: YES 
user_id=44345

Another corner case involving am/pm:

>>> time.strptime("15:15pm", '%H:%M%p')
(1900, 1, 1, 15, 15, 0, 0, 1, -1)
>>> time.strptime("15:15am", '%H:%M%p')
(1900, 1, 1, 15, 15, 0, 0, 1, -1)

Shouldn't both raise ValueError since the input hour was not
< 13?
msg20752 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2004-05-10 18:19
Logged In: YES 
user_id=357491

It's a misuse of %H.  If you look at the 'time' module docs (http://
docs.python.org/lib/module-time.html so you don't have to click to it), it 
states %H is "Hour (24-hour clock) as a decimal number" while %I is 
"Hour (12-hour clock) as a decimal number".  Use of %p is only 
triggered when %I is used since otherwise it is not supposed to be 
needed.

This also deals with your corner case, Skip.

If you feel this should be handled another way, let me know.  But since 
putting in extra info that is not used is not against the rules, the code 
just doesn't bother using %p information unless %I is used.  I guess I 
could raise an exception, but that seems harsh.  Could do a warning, but 
not sure if that is the right way to go either.
msg20753 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2004-05-10 18:54
Logged In: YES 
user_id=44345

I checked in a change to Doc/lib/libtime.tex (v. 1.64).  See
if that seems correct to you.
msg20754 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2004-05-11 01:15
Logged In: YES 
user_id=357491

Yep, works for me.  Thanks, Skip.
History
Date User Action Args
2022-04-11 14:56:04adminsetgithub: 40239
2004-05-10 13:18:52skip.montanarocreate