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: datetime.strptime parses input wrong
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, ezio.melotti, heidar.rafn, hieu.nguyen, vstinner
Priority: normal Keywords: patch

Created on 2011-09-02 14:33 by heidar.rafn, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
issue12886.patch hieu.nguyen, 2012-10-23 12:07 review
issue12886.patch hieu.nguyen, 2012-10-23 22:33 review
Messages (7)
msg143400 - (view) Author: Heiðar Rafn Harðarson (heidar.rafn) Date: 2011-09-02 14:33
When using datetime.strptime or time.strptime to parse string representing timestamp with the format string '%Y%m%dT%H%M%S' then a strange behavior happens when the input string does not contain the seconds: the minute part is split and first digit used as minutes and second digit as seconds !

According to documentation %M shall contain Minute as a decimal number [00,59] and %S shall contain Second as a decimal number [00,61]

Here are few examples to show this:
------------------
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.strptime('20110817T1234','%Y%m%dT%H%M%S')
datetime.datetime(2011, 8, 17, 12, 3, 4)
=>ERROR no seconds in input string: minute=3, second=4
=>I would expect exception ValueError or datetime.datetime(2011, 8, 17, 12, 34, 00)

>>> datetime.datetime.strptime('20110817T123456','%Y%m%dT%H%M%S')
datetime.datetime(2011, 8, 17, 12, 34, 56)
=>CORRECT

>>> datetime.datetime.strptime('20110817T123456','%Y%m%dT%H%M')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/_strptime.py", line 328, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: 56
=>CORRECT

>>> datetime.datetime.strptime('20110817T1234','%Y%m%dT%H%M')
datetime.datetime(2011, 8, 17, 12, 34)
=> CORRECT

------------------
I have tested this with python 2.6 and 2.7
This also happens on when playing with %H%M format string and omit minutes from the input.
msg143401 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-09-02 14:50
> ERROR no seconds in input string: minute=3, second=4

If you specificy "%S" in the format string, strptime() *requires* seconds. If seconds are optional in your format, you should call strptime() twice: first without '%S', then with '%S' if the first failed.

I don't consider your examples as bugs.
msg143402 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-09-02 14:54
Example:
---
import datetime

def test(text):
    try:
        d = datetime.datetime.strptime(text, '%Y%m%dT%H%M')
    except ValueError:
        pass
    else:
        print("%s without seconds => %s" % (text, d))
        return
    d = datetime.datetime.strptime(text, '%Y%m%dT%H%M%S')
    print("%s with seconds => %s" % (text, d))

test('20110817T1234')
test('20110817T12345')
test('20110817T123456')
---

Output:

20110817T1234 without seconds => 2011-08-17 12:34:00
20110817T12345 with seconds => 2011-08-17 12:34:05
20110817T123456 with seconds => 2011-08-17 12:34:56
msg143515 - (view) Author: Heiðar Rafn Harðarson (heidar.rafn) Date: 2011-09-05 14:32
My understanding of the python documentation and the ISO 8601 standard is that the digits in a timestamp representing hours, minutes and seconds shall always be in pairs of 2 digits (hh, mm, ss), i.e. when a number is less than 10 it should be preceded by 0. 
In the example I give, the minute figure is split between minutes and seconds by the  python library function which I consider a bug: 
datetime.datetime.strptime('20110817T1234','%Y%m%dT%H%M%S') 
gives
datetime.datetime(2011, 8, 17, 12, 3, 4)
msg143517 - (view) Author: Heiðar Rafn Harðarson (heidar.rafn) Date: 2011-09-05 14:38
This issue is also discussed here: http://bugs.python.org/issue5979
msg173600 - (view) Author: Hieu Nguyen (hieu.nguyen) * Date: 2012-10-23 12:07
I have attached a patch for this issue, so that if the format of the input argument doesn't match ISO 8601 format, it will return ValueError: time data xxx does not match format xxx.
msg173644 - (view) Author: Hieu Nguyen (hieu.nguyen) * Date: 2012-10-23 22:33
Attached another patch for clearer test cases against this fix (as suggested from Ezio).
History
Date User Action Args
2022-04-11 14:57:21adminsetgithub: 57095
2020-05-31 12:37:23serhiy.storchakasetversions: + Python 3.6, Python 3.7, Python 3.8, Python 3.9, Python 3.10, - Python 2.6, Python 2.7
2012-11-18 21:54:59ezio.melottisetnosy: + belopolsky
2012-10-24 11:41:45ezio.melottisetnosy: + ezio.melotti

stage: patch review
2012-10-23 22:33:49hieu.nguyensetfiles: + issue12886.patch

messages: + msg173644
2012-10-23 12:07:34hieu.nguyensetfiles: + issue12886.patch

nosy: + hieu.nguyen
messages: + msg173600

keywords: + patch
2011-09-05 14:38:41heidar.rafnsetmessages: + msg143517
2011-09-05 14:32:08heidar.rafnsetmessages: + msg143515
2011-09-02 14:54:18vstinnersetmessages: + msg143402
2011-09-02 14:50:23vstinnersetnosy: + vstinner
messages: + msg143401
2011-09-02 14:33:26heidar.rafncreate