classification
Title: [ValueError] _strptime.py can't handle 12-hr format strings using '0' instead of '12' for noon and midnight
Type: behavior Stage: resolved
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: CatPaw Freed, ammar2
Priority: normal Keywords:

Created on 2018-05-24 07:29 by CatPaw Freed, last changed 2018-05-24 09:10 by CatPaw Freed. This issue is now closed.

Messages (3)
msg317541 - (view) Author: CatPaw Freed (CatPaw Freed) Date: 2018-05-24 07:29
I have to process some data which date was specified in 12-hour format, eg. 9/24/2017 0:39:41 PM.
I call datetime.datetime.strptime with "%m/%d/%Y %I:%M:%S %p" format.
However, it throws me an error [File "C:\Python27\lib\_strptime.py", line 332, in _strptime (data_string, format)) ValueError: time data '9/24/2017 0:39:41 PM' does not match format '%m/%d/%Y %I:%M:%S %p'].
Then, I found out that, in _strptime.py, the regex patterns are defined as below.

            'd': r"(?P<d>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
            'f': r"(?P<f>[0-9]{1,6})",
            'H': r"(?P<H>2[0-3]|[0-1]\d|\d)",
            'I': r"(?P<I>1[0-2]|0[1-9]|[1-9])",
            'j': r"(?P<j>36[0-6]|3[0-5]\d|[1-2]\d\d|0[1-9]\d|00[1-9]|[1-9]\d|0[1-9]|[1-9])",
            'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",
            'M': r"(?P<M>[0-5]\d|\d)",
            'S': r"(?P<S>6[0-1]|[0-5]\d|\d)",
            'U': r"(?P<U>5[0-3]|[0-4]\d|\d)",
            'w': r"(?P<w>[0-6])",

I'm a newbie in python world. Is it a bug?
I changed regex pattern of 'I' to r"(?P<I>1[0-2]|0[0-9]|[0-9])" and it works.
I would like to know why 0 is excluded.
May be, I have missed something.
msg317542 - (view) Author: Ammar Askar (ammar2) * (Python triager) Date: 2018-05-24 07:47
0 is excluded because it is not a valid hour in the 12 hour clock.

0:39pm is not a valid time on a 12 hour clock. After 11am you reach 12pm, then 1pm. See the table on the right here: https://en.wikipedia.org/wiki/12-hour_clock

Your data source is encoding the time incorrectly, see this thread here for some solutions: https://stackoverflow.com/a/33317167
msg317546 - (view) Author: CatPaw Freed (CatPaw Freed) Date: 2018-05-24 09:10
Thanks for answering my question and giving me a solution.
This issue has been dawned on me that these convention are quite strange. I feel confused when thinking of 12:00am/pm vs 0:00am/pm. (@_@) (^~^)
History
Date User Action Args
2018-05-24 09:10:54CatPaw Freedsetmessages: + msg317546
2018-05-24 07:47:55ammar2setstatus: open -> closed

type: crash -> behavior

nosy: + ammar2
messages: + msg317542
resolution: not a bug
stage: resolved
2018-05-24 07:29:39CatPaw Freedcreate