1

In Python 3.7 and 2.7, it seems that strptime() will parse a time where the seconds is set to 60 and 61:

>>> import time
>>> time.strptime('12:00:60', '%H:%M:%S')
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=12, tm_min=0, tm_sec=60, tm_wday=0, tm_yday=1, tm_isdst=-1)
>>> time.strptime('12:00:61', '%H:%M:%S')
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=12, tm_min=0, tm_sec=61, tm_wday=0, tm_yday=1, tm_isdst=-1)
>>> time.strptime('12:00:62', '%H:%M:%S')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 571, in _strptime_time
    tt = _strptime(data_string, format)[0]
  File "C:\Users\Al\AppData\Local\Programs\Python\Python37\lib\_strptime.py", line 362, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: 2 (but not 62 or higher):

What's the reason for this? It won't work if I set the hour to 25 or the minute to 61, but why does seconds apparently get a pass?

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92

0 Answers0