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.datetime.strptime functionality description incorrect
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Valectar, augustogoulart, belopolsky, docs@python, fhackdroid, miss-islington, p-ganssle, taleinat, vstinner, xtreak
Priority: normal Keywords: patch

Created on 2016-08-11 18:48 by Valectar, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
datetime_docs.PNG augustogoulart, 2018-10-12 00:19
Pull Requests
URL Status Linked Edit
PR 9994 merged augustogoulart, 2018-10-20 01:34
PR 10213 merged miss-islington, 2018-10-29 11:50
PR 10214 merged miss-islington, 2018-10-29 11:50
PR 10215 merged miss-islington, 2018-10-29 11:50
Messages (13)
msg272474 - (view) Author: Nicholas Colclasure (Valectar) Date: 2016-08-11 18:48
The datetime.datetime.strptime documentation states that it is equivalent to datetime(*(time.strptime(date_string, format)[0:6])), but the time.struct_time returned by time.strptime does not include microseconds, implying that datetime's strptime would also not include microseconds, but testing of the functions shows that it does include them.

Removing the false statement of equivalence would be enough to remove this bug from the documentation.
msg321628 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-07-13 18:49
The same part repeated at https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior can also be corrected. It seems both of them use _strptime which returns microseconds but only datetime.datetime.strptime uses it with time.strptime ignoring it.

Thanks
msg321689 - (view) Author: Farhaan Bukhsh (fhackdroid) * Date: 2018-07-15 10:52
Hey I would like to remove this bug I was going through the discussion so does that mean remove the about mentioned lines from 

https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime

will fix this issue? Or should we add some more information there?
msg327555 - (view) Author: Augusto Goulart (augustogoulart) * Date: 2018-10-12 00:19
It is true that time.srtptime() does not return microseconds, but as the documentation suggests, it will return the microseconds after you wrap it up with datetime().
I have attached a walkthrough of how I see that section of the docs. Please let me know if you have anything else in mind. Thanks!
msg327574 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2018-10-12 08:52
At least with Python 3.7.0, the equivalence is not complete: datetime.strptime() is better, since it retains both microseconds and timezone data. See examples below.

>>> from datetime import datetime, timezone
>>> import time
>>> s = datetime.strftime(datetime.now(), '%a %b %d %H:%M:%S %f')
>>> s
'Fri Oct 12 11:33:32 999810'
>>> datetime.strptime(s, '%a %b %d %H:%M:%S %f')
datetime.datetime(1900, 10, 12, 11, 33, 32, 999810)
>>> datetime(*time.strptime(s, '%a %b %d %H:%M:%S %f')[:6])
datetime.datetime(1900, 10, 12, 11, 33, 32)
>>> s2 = datetime.strftime(datetime.now(timezone(timedelta(hours=1))), '%a %b %d %H:%M:%S %f%z')
>>> s2
'Fri Oct 12 09:48:40 347076+0100'
>>> datetime.strptime(s2, '%a %b %d %H:%M:%S %f%z')
datetime.datetime(1900, 10, 12, 9, 48, 40, 347076, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600)))
>>> datetime(*time.strptime(s2, '%a %b %d %H:%M:%S %f%z')[:6])
datetime.datetime(1900, 10, 12, 9, 48, 40)
msg327961 - (view) Author: Augusto Goulart (augustogoulart) * Date: 2018-10-18 10:33
As far as I can see, every time something changes on datetime.strptime(), it will invalidate that section of the documentation. On the other hand, I believe the comparison with datetime(*(time.strptime(date_string, format)[0:6])" is interesting and valuable to keep.
Given that, instead of "This is equivalent to [...]", I would suggest something like:
"This is similar to [...], but datetime.strptime() is better since it retains both microseconds and timezone data."
msg328031 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2018-10-19 06:46
I agree with Gus, such new wording would be great. Gus, would you like to make a PR?

I'd split the comparison into a second paragraph, and avoid the word "better", instead using something like "but datetime.strptime() also retains both microseconds and timezone data."

Note that such a change of the wording to "similar" rather than "equivalent" will also future-proof this wording from minor future changes to either function.
msg328039 - (view) Author: Augusto Goulart (augustogoulart) * Date: 2018-10-19 11:18
Thanks, Tal. I will open up a PR for it.
msg328806 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-10-29 11:49
New changeset c0799ec973530ad2492bb1d6c7287ffc428f0348 by Victor Stinner (Gus Goulart) in branch 'master':
bpo-27741: Better wording for datetime.strptime() (GH-9994)
https://github.com/python/cpython/commit/c0799ec973530ad2492bb1d6c7287ffc428f0348
msg328809 - (view) Author: miss-islington (miss-islington) Date: 2018-10-29 11:54
New changeset 0a53a067dc4eb86ecf94c50582b3e22359c601a9 by Miss Islington (bot) in branch '3.6':
bpo-27741: Better wording for datetime.strptime() (GH-9994)
https://github.com/python/cpython/commit/0a53a067dc4eb86ecf94c50582b3e22359c601a9
msg328811 - (view) Author: miss-islington (miss-islington) Date: 2018-10-29 11:55
New changeset a02bc719ebc496bc527e9e46de2f2e83f68bfd77 by Miss Islington (bot) in branch '3.7':
bpo-27741: Better wording for datetime.strptime() (GH-9994)
https://github.com/python/cpython/commit/a02bc719ebc496bc527e9e46de2f2e83f68bfd77
msg328812 - (view) Author: miss-islington (miss-islington) Date: 2018-10-29 11:55
New changeset 4ec427b005036dab0a380de20f31774394ca4dd6 by Miss Islington (bot) in branch '2.7':
bpo-27741: Better wording for datetime.strptime() (GH-9994)
https://github.com/python/cpython/commit/4ec427b005036dab0a380de20f31774394ca4dd6
msg328818 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-10-29 12:26
Thanks Nicholas Colclasure for the bug report and Gus Goulart for the fix.
History
Date User Action Args
2022-04-11 14:58:34adminsetgithub: 71928
2018-10-29 12:26:08vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg328818

stage: patch review -> resolved
2018-10-29 11:55:17miss-islingtonsetmessages: + msg328812
2018-10-29 11:55:10miss-islingtonsetmessages: + msg328811
2018-10-29 11:54:57miss-islingtonsetnosy: + miss-islington
messages: + msg328809
2018-10-29 11:50:57miss-islingtonsetpull_requests: + pull_request9531
2018-10-29 11:50:42miss-islingtonsetpull_requests: + pull_request9530
2018-10-29 11:50:32miss-islingtonsetpull_requests: + pull_request9529
2018-10-29 11:49:56vstinnersetnosy: + vstinner
messages: + msg328806
2018-10-20 01:34:39augustogoulartsetkeywords: + patch
stage: patch review
pull_requests: + pull_request9335
2018-10-19 11:18:24augustogoulartsetmessages: + msg328039
2018-10-19 06:46:29taleinatsetmessages: + msg328031
2018-10-18 10:33:59augustogoulartsetmessages: + msg327961
2018-10-12 08:52:17taleinatsetmessages: + msg327574
2018-10-12 01:38:01ned.deilysetnosy: + belopolsky
2018-10-12 00:20:16augustogoulartsetnosy: + taleinat
2018-10-12 00:19:23augustogoulartsetfiles: + datetime_docs.PNG
nosy: + augustogoulart
messages: + msg327555

2018-07-15 10:52:40fhackdroidsetnosy: + fhackdroid
messages: + msg321689
2018-07-13 18:49:29xtreaksetnosy: + xtreak
messages: + msg321628
2018-07-05 16:00:32p-gansslesetnosy: + p-ganssle
2016-08-11 18:48:31Valectarcreate