Repository navigation
Closed
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
Description
| BPO | 12886 |
|---|---|
| Nosy | @abalkin, @vstinner, @ezio-melotti |
| Files |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = None
created_at = <Date 2011-09-02.14:33:26.942>
labels = ['type-bug', '3.8', '3.9', '3.10', '3.7', 'library']
title = 'datetime.strptime parses input wrong'
updated_at = <Date 2020-05-31.12:37:23.388>
user = 'https://bugs.python.org/heidarrafn'bugs.python.org fields:
activity = <Date 2020-05-31.12:37:23.388>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2011-09-02.14:33:26.942>
creator = 'heidar.rafn'
dependencies = []
files = ['27675', '27688']
hgrepos = []
issue_num = 12886
keywords = ['patch']
message_count = 7.0
messages = ['143400', '143401', '143402', '143515', '143517', '173600', '173644']
nosy_count = 5.0
nosy_names = ['belopolsky', 'vstinner', 'ezio.melotti', 'heidar.rafn', 'hieu.nguyen']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue12886'
versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']Linked PRs
Metadata
Metadata
Assignees
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
Projects
- StatusShow more project fieldsDone
- StatusShow more project fieldsTodo
Activity
heidarrafn commentedon Sep 2, 2011
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:
------------------
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.
vstinner commentedon Sep 2, 2011
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.
vstinner commentedon Sep 2, 2011
Example:
---
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
heidarrafn commentedon Sep 5, 2011
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)
heidarrafn commentedon Sep 5, 2011
This issue is also discussed here: http://bugs.python.org/issue5979
hieunguyen commentedon Oct 23, 2012
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.
hieunguyen commentedon Oct 23, 2012
Attached another patch for clearer test cases against this fix (as suggested from Ezio).
5 remaining items
gh-57095: Add note about input splitting in `datetime.*.strptime` (GH…
pythongh-57095: Add note about input splitting in `datetime.*.strptim…
pythongh-57095: Add note about input splitting in `datetime.*.strptim…
[3.14] gh-57095: Add note about input splitting in `datetime.*.strpti…
[3.13] gh-57095: Add note about input splitting in `datetime.*.strpti…
pythongh-57095: Add note about input splitting in `datetime.*.strptim…
pythongh-57095: Add note about input splitting in `datetime.*.strptim…
pythongh-57095: Add note about input splitting in `datetime.*.strptim…