Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

WIP: bpo-1100942: Add datetime.time.strptime and datetime.date.strptime#5578

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
matrixise wants to merge16 commits intopython:mainfrommatrixise:bpo-1100942

Conversation

@matrixise
Copy link
Member

@matrixisematrixise commentedFeb 7, 2018
edited by bedevere-bot
Loading

Add datetime.date.strptime and datetime.time.strptime.

Fix the documentation of _strptime._strptime, the documentation was
wrong, return a 3-tuple and not a 2-tuple

Co-authored-by: Alexander Belopolskyalexander.belopolsky@gmail.com
Co-authored-by: Amaury Forgeot d'Arcamauryfa@gmail.com
Co-authored-by: Berker Peksagberker.peksag@gmail.com
Co-authored-by: Josh-sfjosh-sf@users.sourceforge.net
Co-authored-by: Juarez Bochijbochi@gmail.com
Co-authored-by: Maciej Szuliksoltysh@gmail.com
Co-authored-by: Stéphane Wirtelstephane@wirtel.be
Co-authored-by: Matheus Vieira Portelamatheus.v.portela@gmail.com

https://bugs.python.org/issue1100942

@matrixise
Copy link
MemberAuthor

This PR is for a very long issue, since 2005. We have a PR in 2018 👍

holdenweb and davidawad reacted with thumbs up emoji

@Mariatta
Copy link
Member

I restarted the travis job. It still did not do the full CPython test suite. So please rebase :)

@matrixise
Copy link
MemberAuthor

Thanks, I didn't see your message, works on this issue today.

@matrixise
Copy link
MemberAuthor

@Mariatta rebased and the tests pass on the CIs

"""Return a3-tuple consisting of a time struct and an int containing
the number of microseconds based on the input string and the
format string."""
format string, and the GMT offset."""
Copy link
Member

@pgansslepganssleMay 15, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

We should probably consistently use "UTC offset", though I suppose it doesn't matter much since it's not a public-facing docstring.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@pganssle I am not really confident with the offsets and the datetime. Do you think we could keep it like that and propose an other bpo ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I just meant use UTC instead of GMT everywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yeah, just replace "GMT" with "UTC". datetime.tzinfo has a utcoffset() method, so "UTC" is preferred in datetime.

(And there are some subtle differences between GMT and UTC that I forgot.)

hour,minute,second,
weekday,julian,tz,tzname,gmtoff),fraction,gmtoff_fraction

date_specs= ('%a','%A','%b','%B','%c','%d','%j','%m','%U',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think this is missing%G,%u and%V, the ISO 8601 week calendar directives.


date_specs= ('%a','%A','%b','%B','%c','%d','%j','%m','%U',
'%w','%W','%x','%y','%Y',)
time_specs= ('%T','%R','%H','%I','%M','%S','%f','%i','%s',)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't see any references to%T,%R,%i or%s in the docs or elsewhere in the code. What do these represent?

_time=_strptime_datetime(datetime_datetime,data_string,format)
return_time.time()

def_check_invalid_datetime_specs(fmt,specs,msg):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

If I'm understanding this correctly, this function seems to have the inverted sense of what I would expect. It seems that it checks iffmt is avalid spec.

From the names of the variables and functions, I was thinking that this would be a whitelist not a blacklist. Does it make sense to switch to a whitelist approach? If not, can we maybe changespecs to beblacklist_specs or something?


date_specs= ('%a','%A','%b','%B','%c','%d','%j','%m','%U',
'%w','%W','%x','%y','%Y',)
time_specs= ('%T','%R','%H','%I','%M','%S','%f','%i','%s',)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't have strong opinions about this, but my intuition is that these should beset orfrozenset rather thantuple. Are these tuples for performance reasons (I am not sure I know when exactly it's faster to use a set for "lookup membership" rather than a tuple or list).

def_strptime_datetime_date(data_string,format):
"""Return a date based on the input string and the format string."""
ifnotformat:
raiseValueError("Date format is not valid.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This line and its equivalent in_time are not being hit. If I understand correctly this branch is only hit ifformat is empty?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

useless, thanks

tests= [('2004-12-01 13:02:47.197',
'%Y-%m-%d %H:%M:%S.%f'),
('2004-12-01','%Y-%m-%d'),]
fordate_string,date_formatintests:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think it would be good to usewith self.subTest for these parametrized tests.

Also, per the other comment I guess you need to add something like('12:30:15', '')` to get full coverage.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think you need two more test cases:

    ('1900-01-01 12:30','%Y-%m-%d %H:%M'),    ('12:30:15',''),

date.strptime has similarly missing tests.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

the test with('1900-01-01 12:30', '%Y-%m-%d %H:%M') does not raise an exception but returnsdatetime.time(12, 30)

For the other test, yep, there is an issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The fact that it doesn't raise an exceptionis an issue. I'm a bit surprised that it doesn't raise an exception on pure Python, that's a bug, because I'm pretty sure that:

datetime.time.strptime("1901-01-01 12:30", "%Y-%m-%d %H:%M")does raise an exception.

*/
if (emptyDatetime==NULL) {
PyObject*emptyStringPair=Py_BuildValue("ss","","");
if (emptyStringPair==NULL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

If I'm interpretingPEP 7 correctly, theseif statements need curly braces. The relevant section isCode layout.

@matrixise
Copy link
MemberAuthor

Hi@pganssle

thank you for your review, I am going to fix it asap but I am not the author of the code, just the author of the PR. so, maybe I would need your help. Thanks

@matrixise
Copy link
MemberAuthor

@pganssle I just rebased my branch with master. I am going to work on this PR. Do you want to help me because you are mister dateutil ;-)

@pganssle
Copy link
Member

@matrixise Sorry this is on my list but probably can't get to it until the end of the month. 😟

@matrixise
Copy link
MemberAuthor

@pganssle ok, in this case, I will try to fix all the issues alone ;-) but I am not worried ;-)

@matrixise
Copy link
MemberAuthor

Hi, I just updated this PR with master.

@matrixise
Copy link
MemberAuthor

@pganssle when you have time, could you review this PR, we started together, just comment when you find a mistake, thanks

@matrixise
Copy link
MemberAuthor

ping@pganssle ;-)

Copy link
Member

@pgansslepganssle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I haven't had a chance to look at the PR thoroughly, but I have a first-pass review of things that should be changed.

I also haven't checked exactly how you are doing it, but I believe we may want to / be able to refactor the tests a bit to take advantage of the existing test suite fordatetime.strptime by separating out thedate-only andtime-only formats and reusing the tests withdate,time anddatetime.

Additionally, I should note that it's unfortunate that if this is merged, we'll havetime.strptime anddatetime.time.strptime, the first of which returns a timetuple (which is actually more like a datetime), and the second returning adatetime.time object. I don't see any way around this, but it will add confusion. :(

Copy link
Member

@pgansslepganssleNov 4, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Here and below, you havenonzero instead ofnon-zero. If we keep this wording, the hyphen needs to be added.

However, this PR does not parse to atimetuple or something and check if certain components were zero, it checks to see if theformat string containstime components, and fails in that case (Edit: I was looking at just the pure python implementation - I now realize that this is precisely what the C implementation is doing, but I think it's the wrong thing to do anyway). The way the docs are currently worded, you would expect this to work:

fromdatetimeimportdatedate.strptime("2018-01-01 00:00","%Y-%m-%d %H:%M")

But it will fail (rightly so, I think).

I believe you can change the last part of the last sentence as such:

-     parsed by `time.strptime`, or if it returns a value where the time part is-     nonzero.+     parsed by `time.strptime`, or if time components are present in the format string.

Also, I think it needs to be

:meth:`time.strptime`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

One other note on the documentation, thedatetime.strptime documentation links to

:ref:`strftime-strptime-behavior`.

I think these should too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't know why thisPyObject *Py_UNUSED(ignored) is here, so perChesterton's Fence, I'm not comfortable removing it. Any insight as to why it is here and what the consequences will be in removing it?

Possibly it will be a breaking change in the C ABI?

Copy link
Member

@pgansslepganssleNov 4, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

According togit blame,this is to silence a warning ingcc8. Is this still relevant@siddhesh@serhiy-storchaka?

Edit: Looking closer, the merged PR is from April, so I'm guessing it is, but now I'm wondering if this will break the C ABI theother way.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes,datetime_gettime should have two arguments, the second is ignored.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Hm, if I understand correctly, this is inconsistent with the pure Python version. The pure Python version checks if there are any time components in theformat string, whereas the C version parses to a datetime and then checks to see if there are anytime components.

I think checking the format string is the better way to do this, as I mention in another comment, this approach seems to indicate thatdate.strptime("2018-01-01 00:00", "%Y-%m-%d %H:%M") wouldsucceed, which is not the right thing to do. I will comment on the test suite to add a test for this.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

ok, I will check the Python implementation, but in this case, we could migrate the Python implementation to the C layer?

pganssle reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yeah, port the Python implementation to the C layer.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

lol ;-) I am not an expert with the C-API, but I could try, it's a good exercise for my comprehension of the C-API of Python

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yeah, you have to reimplement _check_invalid_datetime_specs() in C.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why does it have to be implemented in C? There are already calls out to the _strptime Python-language module here.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

not sure, but because there is an other test in C for the date, and maybe we could put the verification process in only one function.@vstinner do you confirm?

tests= [('2004-12-01 13:02:47.197',
'%Y-%m-%d %H:%M:%S.%f'),
('2004-12-01','%Y-%m-%d'),]
fordate_string,date_formatintests:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think you need two more test cases:

    ('1900-01-01 12:30','%Y-%m-%d %H:%M'),    ('12:30:15',''),

date.strptime has similarly missing tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This needs at least two more test cases:

    ('2018-01-01 00:00','%Y-%m-%d %H:%M'),    ('2018-01-01',''),

Both should fail.

@matrixise
Copy link
MemberAuthor

@pganssle thanks for your review, I am going to update this PR asap.

pganssle reacted with thumbs up emoji

@matrixise
Copy link
MemberAuthor

ok, rebased with the last master.

@matrixise
Copy link
MemberAuthor

matrixise commentedNov 11, 2018 via email

ok, I will check and try to fix that, but because I don't know this partand the main patch was not mine, that will be difficult.but okay for the fix. thanks for the review.

@matrixise
Copy link
MemberAuthor

@pganssle Are you ready for a new review of this PR? I will continue my PR ;-)

Copy link
Member

@vadmiumvadmium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I suggest you check the discussion about the previous patches. I noticed I repeated some old comments and ideas.

Return a:class:`date` corresponding to *date_string*, parsed according to
*format*.:exc:`ValueError` is raised if the date string and format can't be
parsed by:meth:`time.strptime`, or if time components are present in the
format string. For a complete list of formatting directives, see
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Perhaps clarify “time.strptime” does not refer to thestrptime function in thetime module. The reader may not see themeth RST code, HTML links, etc.

Return a:class:`time` corresponding to *date_string, parsed according to
*format*.:exc:`ValueError` is raised if the date string and format can't be
parsed by:meth:`time.strptime`, if it returns a value which isn't a time
tuple, or if the date part is nonzero. For a complete list of formatting
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

“Bytime.strptime” is redundant, unless you mean to refer to thestrptimefunction in thetime module.

What does “the date part is nonzero” mean? I would expect thetime class to work without specifying a date, zero or otherwise.

is substituted fortheyear, and ``1``forthe monthandday.
The:meth:`date.strptime` class method creates a:class:`date` object from a
string representing a date and a corresponding format string.:exc:`ValueError`
raised iftheformat codesforhours, minutes, seconds,andmicroseconds are used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

ValueErroris raised . . .

And microseconds should beor microseconds, unless you must combine all four codes to get the error.

But wouldn’t these details be better placed directly under thedate andtime classes, not in this common section about format codes in general?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I have fixed the grammar issues, but for the details I don't know.

corresponding format string. ``datetime.strptime(date_string, format)`` is
equivalent to ``datetime(*(time.strptime(date_string, format)[0:6]))``, except
when the format includes sub-second components or timezone offset information,
which are supported in ``datetime.strptime`` but are discarded by ``time.strptime``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Need to clarifytime.strptime refers to thetime module, not your newstrptime method.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

👍


..classmethod::time.strptime(date_string, format)

Return a:class:`time` corresponding to *date_string, parsed according to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why`time` here, but`.time` below (with a dot)?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

👍 and there was an other erorr with*date_string*

datetime
--------

Added:func:`~datetime.date.strptime` and:func:`~datetime.time.strptime`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I expect you lose the reference todate andtime, so all you are saying is you added two functions with the same name repeated.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@vadmium yep, here, we just add the class methodsdatetime.date.strptime anddatetime.time.strptime

returnNULL;
}

assert(PyTuple_CheckExact(specs)||PyList_CheckExact(specs));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why would it be a list? This seems like unused code.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

the first version was only for the tuples. After, I wanted to accept the list for my tests. but I am not against to only accept the tuples.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why does it have to be implemented in C? There are already calls out to the _strptime Python-language module here.

"""Return a date based on the input string and the format string."""
msg="'{!s}' {} not valid in date format specification."
from_datetimeimport_check_invalid_datetime_specs
if_check_invalid_datetime_specs(format,time_specs,msg):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why not move themsg string into thecheck function, and just pass the bit that varies ('date' or 'time') as an argument? It would make the code easier to read.

Looks like thecheck function either raises an exception or returns True. It would be clearer to not use anif statement here.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

at the beginning, this PR was a submitted patch by other contributors, I just wanted to convert it to a PR. and now, I try to fix all the issues.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

but for this point, the first step was the conversion of_check_invalid_datetime_specs to C, and after start to improve the code with the other recommendations. (from@vstinner and@pganssle)

@matrixisematrixise changed the titlebpo-1100942: Add datetime.time.strptime and datetime.date.strptimeWIP: bpo-1100942: Add datetime.time.strptime and datetime.date.strptimeMar 24, 2019
@csabella
Copy link
Contributor

@matrixise,@pganssle There's been a lot of work done on this one. Is it something we should try to move forward on? Thanks!

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actionsgithub-actionsbot added the staleStale PR or inactive for long period of time. labelAug 15, 2022
@abalkinabalkin marked this pull request as draftFebruary 8, 2023 14:59
@abalkinabalkin self-assigned thisFeb 8, 2023
@abalkinabalkin linked an issueFeb 8, 2023 that may beclosed by this pull request
@github-actionsgithub-actionsbot removed the staleStale PR or inactive for long period of time. labelMay 1, 2023
@zitterbewegung
Copy link
Contributor

@matrixise,@pganssle,@csabella while there has been lots of work for this where are we at on this? Its around a bit over two years old for the PR but, these new features would be great!

@vstinner
Copy link
Member

where are we at on this?

No activity since 2019. Someone has to step in and restart the work (ex: create a new PR and update the PR).

@nineteendo
Copy link
Contributor

I'm going to try to resolve the merge conflicts and make the requested changes.

@nineteendo
Copy link
Contributor

Looks like I'll have to start over.

@vstinner
Copy link
Member

I close the inactive PR.

nineteendo reacted with thumbs up emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@vadmiumvadmiumvadmium left review comments

@pgansslepgansslepganssle requested changes

@serhiy-storchakaserhiy-storchakaserhiy-storchaka left review comments

@vstinnervstinnervstinner requested changes

@abalkinabalkinAwaiting requested review from abalkinabalkin will be requested when the pull request is marked ready for reviewabalkin is a code owner

@AA-TurnerAA-TurnerAwaiting requested review from AA-TurnerAA-Turner will be requested when the pull request is marked ready for reviewAA-Turner is a code owner

Assignees

@abalkinabalkin

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Add datetime.time.strptime and datetime.date.strptime

13 participants

@matrixise@Mariatta@pganssle@bedevere-bot@csabella@zitterbewegung@vstinner@nineteendo@vadmium@serhiy-storchaka@abalkin@the-knights-who-say-ni@ezio-melotti

[8]ページ先頭

©2009-2025 Movatter.jp