-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add datetime.time.strptime and datetime.date.strptime #41431
Comments
Alllow creating new datetime objects by parsing date datetime already has strftime, so adding strptime is The new constructor is equivalent to Patch includes documentation and unit test. |
Logged In: YES This patch will be welcomed by all of that have had to write I don't understand the C API well enough to check if Documentation looks good and the test passes on my machine. Two suggestions:
|
Logged In: YES Regarding support by datetime.time and datetime.date, if a |
Logged In: YES The first patch has been applied, now just the second needs That adds support for date and time as well as datetime, as |
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. |
Patch needs updating. |
Here is an updated patch, with tests. The only thing that bugs me is the name of the method: date.strptime() |
I am +1 for adding these features and I have only one comment on the code: It is documented in time.strptime() documentation that 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. |
BTW, it does not bother me that "date.strptime() |
Does this need to be brought up on python-dev for acceptance? |
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.) |
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 bpo-1100942.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. >>> 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." |
Is anyone still interested in moving this forward? |
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. |
Bumping priority to get this in before beta. |
I have updated the patches since they were not applying cleanly and included a pure Python implementation that was missing. It has the same issues that were mentioned on msg107402. Do you have any suggestions? I'm planning to block the formats that are not allowed and raise Exceptions like suggested before: >>> date.strptime('01', '%M')
...
"'%M' is not valid in date format specification." |
I've updated my patch with the tests for datetime.time.strptime that were missing and also its pure Python implementation. The previous diff also had some issues that I've fixed now: duplicated datetime.strptime method definition in c and the pure python docstring state that date.strpdate was a method and not a constructor. |
Sorry. I updated my patch again to fix the exception message for time.strptime in the pure Python version. |
I've updated the patch based on ezio.melotti and berkerpeksag reviews (thanks). It's still missing the modifications proposed on msg107402. |
Could someone please review this with a view to getting the patch into the 3.4 code base, thanks. |
Juarez, Are you planning to implement format validation as you described in msg165882? Without that, date.strptime() is not very useful because it is almost equivalent to datetime.strptime().date(). |
New patch attached. Changes:
|
I've tried to test this but v4 doesn't apply cleanly after pure2 is applied, and v4 doesn't include enough to test it (applying v4 only causes test failures). I reviewed v4 and it looks fine in general. I do see that there are changes in it unrelated to this issue, but they are PEP-8 changes so I'm not objecting, but ideally that would be split out into a separate patch. I think that this code will incorrectly detect something like '%s %%wall clock' as a date spec because it contains '%w', but strptime would consider that '%' followed by the string 'wall'. A subtle edge case, but worth considering. Maybe it needs to strip out %% first then look for the % sequences? Or perhaps just do the conversion and if the Y/M/D fields are set in then decide that it included a date spec, or if the HMS are set then say that it has the time spec included? |
Berker per your comment updated patch changing those two new methods (namely date.strptime and time.strptime) to be classmethod and not staticmethods. |
Here is an updated version (for 3.6) of the patch of maciej.szulik. I have executed all the tests. Please, could you review this patch. Thank you |
This does not look right: +.. classmethod:: time.strptime(date_string, format) |
belopolsky, could you tell me what it is wrong with the doc about time.strptime ? |
Shouldn't "time part" underlined in my previous note be "date part" instead? Also, does non-zero mean non-empty? |
Never mind the second question. |
The patch is broken against Python 3.7. I'll try working on it. |
Also, may I move this issue to a GitHub PR? |
I have updated the patch for 3.8, create a PR and fixed the documentation of _strptime._strptime, because this function returns a 3-tuple and not a 2-tuple as indicated in its comment. Thank you |
Hello, just a small reminder for this issue and the PR ;-) when you have time |
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. |
@matrixise, I don't see a PR linked to this issue. |
Found it: GH-5578. |
…)` (#120752) * Python implementation * C implementation * Test `date.strptime` * Test `time.strptime` * 📜🤖 Added by blurb_it. * Update whatsnew * Update documentation * Add leap year note * Update 2024-06-19-19-53-42.gh-issue-41431.gnkUc5.rst * Apply suggestions from code review Co-authored-by: Bénédikt Tran <[email protected]> * Remove parentheses * Use helper function * Remove bad return * Link to github issue * Fix directive * Apply suggestions from code review Co-authored-by: Paul Ganssle <[email protected]> * Fix test cases --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Paul Ganssle <[email protected]>
* main: (69 commits) Add "annotate" SET_FUNCTION_ATTRIBUTE bit to dis. (python#124566) pythongh-124412: Add helpers for converting annotations to source format (python#124551) pythongh-119180: Disallow instantiation of ConstEvaluator objects (python#124561) For-else deserves its own section in the tutorial (python#123946) Add 3.13 as a version option to the crash issue template (python#124560) pythongh-123242: Note that type.__annotations__ may not exist (python#124557) pythongh-119180: Make FORWARDREF format look at __annotations__ first (python#124479) pythonGH-58058: Add quick reference for `ArgumentParser` to argparse docs (pythongh-124227) pythongh-41431: Add `datetime.time.strptime()` and `datetime.date.strptime()` (python#120752) pythongh-102450: Add ISO-8601 alternative for midnight to `fromisoformat()` calls. (python#105856) pythongh-124370: Add "howto" for free-threaded Python (python#124371) pythongh-121277: Allow `.. versionadded:: next` in docs (pythonGH-121278) pythongh-119400: make_ssl_certs: update reference test data automatically, pass in expiration dates as parameters python#119400 (pythonGH-119401) pythongh-119180: Avoid going through AST and eval() when possible in annotationlib (python#124337) pythongh-124448: Update Windows builds to use Tcl/Tk 8.6.15 (pythonGH-124449) pythongh-123884 Tee of tee was not producing n independent iterators (pythongh-124490) pythongh-124378: Update test_ttk for Tcl/Tk 8.6.15 (pythonGH-124542) pythongh-124513: Check args in framelocalsproxy_new() (python#124515) pythongh-101100: Add a table of class attributes to the "Custom classes" section of the data model docs (python#124480) Doc: Use ``major.minor`` for documentation distribution archive filenames (python#124489) ...
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:
bugs.python.org fields:
Linked PRs
datetime.time.strptime()
anddatetime.date.strptime()
#120752The text was updated successfully, but these errors were encountered: