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: strptime %d handling of single digit day of month
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Mike Gleen, brett.cannon, docs@python, guziy.sasha, matrixise, miss-islington, p-ganssle, xtreak
Priority: normal Keywords: patch

Created on 2018-10-05 07:31 by Mike Gleen, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9747 closed python-dev, 2018-10-07 06:03
PR 14149 merged Mike Gleen, 2019-06-17 09:32
PR 14205 merged miss-islington, 2019-06-18 18:15
PR 14206 merged miss-islington, 2019-06-18 18:59
Messages (11)
msg327109 - (view) Author: Mike Gleen (Mike Gleen) * Date: 2018-10-05 07:31
strptime correctly parses single digit day-of-month values (at least in the case of "%d %b %Y") which is the behavior I want. However, the documentation seems to say that the field must be two digits with a leading zero if necessary. So I hope that this is the intended behavior rather than just an accidental artifact. If so, then my suggestion is that the documentation be updated to reflect this.
msg327110 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-10-05 07:39
Hi Mike,

Thank you for your issue, but could you add the link to the documentation, just because we have time.strptime and datetime.datetime.strptime.

Thank you,
msg327156 - (view) Author: Mike Gleen (Mike Gleen) * Date: 2018-10-05 17:31
Sorry for the omission. This refers to datetime.datetime.strptime. The documentation I referenced is: https://docs.python.org/3/library/datetime.html; I did not test 2.7.
msg327200 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-10-05 23:44
So the documentation reads that way because it was originally written for strftime and has been repurposed to represent both. If the code does the right thing then adding a note that strptime supports single digits accurately would probably be a welcome pull request!
msg327227 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-10-06 06:09
In addition to %d there are also other items that support single digit though zero padding is mentioned with strptime as below in the context of strftime : 

>>> import datetime
>>> datetime.datetime.strptime("1/1/2018 1:1:1", "%d/%m/%Y %I:%M:%S")
datetime.datetime(2018, 1, 1, 1, 1, 1)


>>> datetime.datetime.strftime(datetime.datetime(year=2018, month=1, day=1, hour=1, second=1, minute=1), "%d/%m/%Y %I:%M:%S")
'01/01/2018 01:01:01'

I couldn't find exact set of words that can be used to denote that zero padding is optional in strptime and it's returned as zero padded in strftime but if we are changing %d then I propose changing other items too with similar wording.

Thanks
msg327237 - (view) Author: Mike Gleen (Mike Gleen) * Date: 2018-10-06 08:56
Thanks for the quick response. I would be happy to write a pull request for
the doc change. I've never done this before so it'll take a little while to
get my head around the process, but the "Helping with Documentation"
chapter seems good.

Mike

On Sat, Oct 6, 2018 at 7:09 AM Karthikeyan Singaravelan <
report@bugs.python.org> wrote:

>
> Karthikeyan Singaravelan <tir.karthi@gmail.com> added the comment:
>
> In addition to %d there are also other items that support single digit
> though zero padding is mentioned with strptime as below in the context of
> strftime :
>
> >>> import datetime
> >>> datetime.datetime.strptime("1/1/2018 1:1:1", "%d/%m/%Y %I:%M:%S")
> datetime.datetime(2018, 1, 1, 1, 1, 1)
>
>
> >>> datetime.datetime.strftime(datetime.datetime(year=2018, month=1,
> day=1, hour=1, second=1, minute=1), "%d/%m/%Y %I:%M:%S")
> '01/01/2018 01:01:01'
>
> I couldn't find exact set of words that can be used to denote that zero
> padding is optional in strptime and it's returned as zero padded in
> strftime but if we are changing %d then I propose changing other items too
> with similar wording.
>
> Thanks
>
> ----------
> nosy: +xtreak
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue34903>
> _______________________________________
>
msg346000 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2019-06-18 18:15
New changeset 6b9c204ee77a0de87d6f51a3d4547a18604cef9e by Paul Ganssle (Mike Gleen) in branch 'master':
bpo-34903: Document that some strptime formats only require 1 digit (GH-14149)
https://github.com/python/cpython/commit/6b9c204ee77a0de87d6f51a3d4547a18604cef9e
msg346001 - (view) Author: miss-islington (miss-islington) Date: 2019-06-18 18:55
New changeset 452b417e34489614b3003b8d08148269096239d5 by Miss Islington (bot) in branch '3.7':
bpo-34903: Document that some strptime formats only require 1 digit (GH-14149)
https://github.com/python/cpython/commit/452b417e34489614b3003b8d08148269096239d5
msg346002 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2019-06-18 18:56
Thanks for the PR, Mike! This is now merged and backported to Python 3.7, so I believe we can close this issue.
msg346004 - (view) Author: miss-islington (miss-islington) Date: 2019-06-18 19:21
New changeset 35aa0b0ced91cfb48d9dcf80a2ca8683e61f9b3e by Miss Islington (bot) in branch '3.8':
bpo-34903: Document that some strptime formats only require 1 digit (GH-14149)
https://github.com/python/cpython/commit/35aa0b0ced91cfb48d9dcf80a2ca8683e61f9b3e
msg393688 - (view) Author: Huziy Oleksandr (guziy.sasha) Date: 2021-05-14 19:50
This is a bit unfortunate for the formats without separators, that could give wrong results silently:

```
In [26]: datetime.strptime("2020010112", "%Y%m%d%H")
Out[26]: datetime.datetime(2020, 1, 1, 12, 0)

In [27]: datetime.strptime("2020010112", "%Y%m%d%H%M")
Out[27]: datetime.datetime(2020, 1, 1, 1, 2)
```
History
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 79084
2021-11-26 10:36:29christian.heimeslinkissue45884 superseder
2021-05-14 19:50:35guziy.sashasetnosy: + guziy.sasha

messages: + msg393688
versions: + Python 3.8, - Python 3.6
2019-06-18 20:44:57p-gansslesetresolution: fixed
2019-06-18 19:21:32miss-islingtonsetmessages: + msg346004
2019-06-18 18:59:28miss-islingtonsetpull_requests: + pull_request14044
2019-06-18 18:56:50p-gansslesetstatus: open -> closed

messages: + msg346002
stage: patch review -> resolved
2019-06-18 18:55:44miss-islingtonsetnosy: + miss-islington
messages: + msg346001
2019-06-18 18:15:31miss-islingtonsetpull_requests: + pull_request14043
2019-06-18 18:15:00p-gansslesetmessages: + msg346000
2019-06-17 09:32:26Mike Gleensetpull_requests: + pull_request13990
2018-10-08 15:08:14p-gansslesetnosy: + p-ganssle
2018-10-07 06:03:54python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request9133
2018-10-06 08:56:27Mike Gleensetmessages: + msg327237
2018-10-06 06:09:25xtreaksetnosy: + xtreak
messages: + msg327227
2018-10-05 23:44:31brett.cannonsetnosy: + brett.cannon
messages: + msg327200
2018-10-05 17:31:21Mike Gleensetmessages: + msg327156
2018-10-05 07:39:52matrixisesetnosy: + matrixise
messages: + msg327110
2018-10-05 07:31:26Mike Gleencreate