classification
Title: Add datetime.time.strptime and datetime.date.strptime
Type: enhancement Stage: needs patch
Components: Extension Modules Versions: Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: belopolsky Nosy List: ajaksu2, alanvgreen, amaury.forgeotdarc, belopolsky, guettli, josh-sf, mark.dickinson, sonderblade, tiktuk
Priority: normal Keywords: easy, needs review, patch

Created on 2005-01-12 14:53 by josh-sf, last changed 2011-01-11 15:43 by belopolsky.

Files
File name Uploaded Description Edit
strptime.diff josh-sf, 2005-01-12 14:54
strptime2.diff josh-sf, 2005-02-02 22:00 time.strptime and date.strptime as well
date-strptime.patch amaury.forgeotdarc, 2009-06-23 23:18
issue1100942.diff belopolsky, 2010-06-09 15:17
Messages (14)
msg47516 - (view) Author: Josh (josh-sf) Date: 2005-01-12 14:53
Alllow creating new datetime objects by parsing date
strings.

datetime already has strftime, so adding strptime is
logical.

The new constructor is equivalent to
datetime(*(time.strptime(date_string, format)[0:6])).

Patch includes documentation and unit test.
msg47517 - (view) Author: Alan Green (alanvgreen) Date: 2005-01-25 12:05
Logged In: YES 
user_id=1174944

This patch will be welcomed by all of that have had to write
"datetime(*(time.strptime(date_string, format)[0:6]))".

I don't understand the C API well enough to check if
reference counts are handled properly, but otherwise the
implementation looks straight forward.

Documentation looks good and the test passes on my machine.

Two suggestions:

1. In the time module, the strptime() function's format
parameter is  optional. For consistency's sake, I'd expect
datetime.strptime()'s format parameter also to be optional.
(On the other hand, the default value for the format is not
very useful.)

2. Since strftime is supported by datetime.time,
datetime.date and datetime.datetime, I'd also expect
strptime to be supported by all three classes. Could you add
that now, or would it be better to do it as a separate patch?
msg47518 - (view) Author: Josh (josh-sf) Date: 2005-02-02 00:50
Logged In: YES 
user_id=1194964

Regarding support by datetime.time and datetime.date, if a
date component or a time component is specified,
respectively, do you think that we should raise an
exception? or should we just ignore it?
msg47519 - (view) Author: Josh (josh-sf) Date: 2005-02-17 17:15
Logged In: YES 
user_id=1194964

The first patch has been applied, now just the second needs
to be. (strptime2.diff).

That adds support for date and time as well as datetime, as
per alanvgreen's suggestion.
msg47520 - (view) Author: Björn Lindqvist (sonderblade) Date: 2007-06-05 20:44
The patch doesn't apply cleanly anymore, although that was easy to fix. With the patch, I also get a few implicit declaration warnings and a few conflicting type errors. Rearranging the order of the functions solve that. Fixing that makes the code compile. The two new methods seem to work correct, although there should be unit tests.
msg82109 - (view) Author: Daniel Diniz (ajaksu2) Date: 2009-02-14 19:08
Patch needs updating.
msg89650 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-06-23 23:18
Here is an updated patch, with tests.

The only thing that bugs me is the name of the method: date.strptime() 
seems a bit odd, given that it cannot accept a time part...
OTOH 'strptime' refers to the format specification: %Y-%m-%d
msg103731 - (view) Author: Alexander Belopolsky (Alexander.Belopolsky) Date: 2010-04-20 16:02
I am +1 for adding these features and I have only one comment on the code:

It is documented in time.strptime() documentation that
"""
The default values used to fill in any missing data when more accurate values cannot be inferred are (1900, 1, 1, 0, 0, 0, 0, 1, -1). 
""" http://docs.python.org/dev/py3k/library/time.html#time.strptime

and "datetime.strptime(date_string, format) is equivalent to datetime(*(time.strptime(date_string, format)[0:6]))." according to datetime module documentation.

Thus, datetime.strptime("", "") returning datetime.datetime(1900, 1, 1, 0, 0) is not an implementation detail and there is no need to compute it in time_strptime.
msg103732 - (view) Author: Alexander Belopolsky (Alexander.Belopolsky) Date: 2010-04-20 16:12
BTW, it does not bother me that "date.strptime() 
seems a bit odd, given that it cannot accept a time part."  To me "time" in strptime means time specification that may include date, time or even just month.  If parsed specification does not fit in date (includes time component), date.strptime fails.  There is nothing wrong with it.  An alternative would be to make {date,time}.strptime() promiscuous and just drop unneeded components, but that would make these functions less useful because such behavior is simply datetime.strptime(..).{date,time}().
msg106805 - (view) Author: Alexander Belopolsky (belopolsky) (Python committer) Date: 2010-05-31 19:23
Does this need to be brought up on python-dev for acceptance?
msg106807 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-05-31 19:45
This doesn't appear to be at all controversial;  I don't think it's necessary to consult python-dev.  (I haven't looked at the patch, though.)
msg107402 - (view) Author: Alexander Belopolsky (belopolsky) (Python committer) Date: 2010-06-09 15:17
I have updated Amaury's patch for py3k.  I simplified the test for default date values and fixed a documentation nit. (Time fileds are [4:7], not [4:6]).  The result is attached as issue1100942.diff.

Note that date.strptime accepts some time specs and time.strptime accepts some date specs:

>>> time.strptime('1900', '%Y')
datetime.time(0, 0)
>>> date.strptime('00', '%H')
datetime.date(1900, 1, 1)

This matches the proposed documentation, but I am not sure is desirable.  
I am about +0 for making the test more robust by scanning the format string and rejecting date format codes in time.strptime and time format codes in date.  This will also allow better diagnostic messages.  For example, instead of 

>>> date.strptime('01', '%M')
Traceback (most recent call last):
  ..
ValueError: date.strptime value cannot have a time part

we can produce "'%M' is not valid in date format specification."
msg114247 - (view) Author: Alexander Belopolsky (belopolsky) (Python committer) Date: 2010-08-18 16:39
Is anyone still interested in moving this forward?
msg126013 - (view) Author: Alexander Belopolsky (belopolsky) (Python committer) Date: 2011-01-11 15:43
New patch needed to address the issue of time.strftime() accepting %Y when year is 1900 and other similar oddities.  See msg107402 above.  Also a patch for datetime.py is needed.
History
Date User Action Args
2011-01-11 15:43:19belopolskysetnosy: guettli, amaury.forgeotdarc, mark.dickinson, belopolsky, sonderblade, alanvgreen, ajaksu2, josh-sf, tiktuk
versions: + Python 3.3, - Python 3.2
messages: + msg126013
stage: patch review -> needs patch
2010-08-18 16:39:56belopolskysetmessages: + msg114247
2010-06-09 15:17:23belopolskysetkeywords: + easy, patch
files: + issue1100942.diff
messages: + msg107402
2010-05-31 19:45:14mark.dickinsonsetmessages: + msg106807
2010-05-31 19:23:21belopolskysetversions: + Python 3.2, - Python 2.7
nosy: + mark.dickinson

messages: + msg106805

stage: test needed -> patch review
2010-05-25 23:55:45belopolskysetassignee: belopolsky
nosy: + belopolsky, - Alexander.Belopolsky
2010-04-20 16:12:34Alexander.Belopolskysetmessages: + msg103732
2010-04-20 16:02:17Alexander.Belopolskysetnosy: + Alexander.Belopolsky
messages: + msg103731
2009-12-02 13:21:19guettlisetnosy: + guettli
2009-06-23 23:18:59amaury.forgeotdarcsetfiles: + date-strptime.patch

nosy: + amaury.forgeotdarc
messages: + msg89650

keywords: + needs review, - patch
2009-06-22 15:17:34tiktuksetnosy: + tiktuk
2009-02-14 19:08:51ajaksu2setnosy: + ajaksu2
versions: + Python 2.7, - Python 2.6
stage: test needed
messages: + msg82109
title: datetime.strptime constructor added -> Add datetime.time.strptime and datetime.date.strptime
2008-01-06 12:35:14christian.heimessettype: enhancement
versions: + Python 2.6
2005-01-12 14:53:01josh-sfcreate