Repository navigation
Add datetime.time.strptime and datetime.date.strptime #41431
Copy link
Copy link
Closed
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement
Description
| BPO | 1100942 |
|---|---|
| Nosy | @amauryfa, @abalkin, @vstinner, @devdanzin, @berkerpeksag, @soltysh, @matrixise, @vajrasky, @matheusportela, @pganssle |
| PRs | |
| 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 2005-01-12.14:53:01.000>
labels = ['extension-modules', 'type-feature', '3.8']
title = 'Add datetime.time.strptime and datetime.date.strptime'
updated_at = <Date 2018-08-20.19:46:06.268>
user = 'https://bugs.python.org/josh-sf'bugs.python.org fields:
activity = <Date 2018-08-20.19:46:06.268>
actor = 'p-ganssle'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Extension Modules']
creation = <Date 2005-01-12.14:53:01.000>
creator = 'josh-sf'
dependencies = []
files = ['6427', '6428', '14351', '17599', '26446', '26452', '29117', '33240', '34778', '39345', '44044', '47032']
hgrepos = []
issue_num = 1100942
keywords = ['patch', 'needs review']
message_count = 48.0
messages = ['47516', '47517', '47518', '47519', '47520', '82109', '89650', '103731', '103732', '106805', '106807', '107402', '114247', '126013', '162732', '165882', '165905', '165906', '165929', '174743', '174746', '182336', '184577', '206495', '206498', '206500', '206519', '206717', '215472', '215474', '215491', '215537', '215559', '215843', '242569', '242803', '242809', '242917', '272149', '273568', '279331', '279333', '279334', '298841', '298842', '311783', '313948', '321550']
nosy_count = 22.0
nosy_names = ['jafo', 'guettli', 'amaury.forgeotdarc', 'belopolsky', 'sonderblade', 'alanvgreen', 'vstinner', 'ajaksu2', 'josh-sf', 'cvrebert', 'tiktuk', 'adam-collard', 'westley.martinez', 'berker.peksag', 'maciej.szulik', 'Juarez.Bochi', 'petre', 'matrixise', 'vajrasky', 'Julian.Gindi', 'matheus.v.portela', 'p-ganssle']
pr_nums = ['5578']
priority = None
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue1100942'
versions = ['Python 3.8']Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement
Projects
- StatusShow more project fieldsDone
Activity
josh-sf commentedon Jan 12, 2005
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.
alanvgreen commentedon Jan 25, 2005
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:
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.)
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?
josh-sf commentedon Feb 2, 2005
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?
josh-sf commentedon Feb 17, 2005
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.
sonderblade commentedon Jun 5, 2007
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.
devdanzin commentedon Feb 14, 2009
Patch needs updating.
[-]datetime.strptime constructor added[/-][+]Add datetime.time.strptime and datetime.date.strptime[/+]amauryfa commentedon Jun 23, 2009
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
AlexanderBelopolsky commentedon Apr 20, 2010
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.
AlexanderBelopolsky commentedon Apr 20, 2010
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}().
43 remaining items
vstinner commentedon Jul 12, 2018
I removed the easy keyword from this issue: it's open since 2005, it has 12 patches and 1 PR attached, and a lot of discussion.
abalkin commentedon Feb 8, 2023
@matrixise, I don't see a PR linked to this issue.
abalkin commentedon Feb 8, 2023
Found it: GH-5578.
gh-41431: Add `datetime.time.strptime()` and `datetime.date.strptime(…
Merge branch 'main' into fix-help-double-prompt
time parsing // strptime()