You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Parsing dates with no times, without assuming default timesΒ #937
Is it possible to optionally get a datetime.date object out of a parser, instead of a datetime.datetime?
Essentially I'd like to differentiate between, say, "tomorrow" and "tomorrow 4pm", where the former would just give me date(2021, 6, 23), and the latter datetime(2021, 6, 23, 16, 0). I'm not sure if this exists as a feature already -- searching the docs and issues for the likes of "date without time" is tricky in a date-parsing library!
The current behaviour is for "tomorrow" to give me datetime(2021, 6, 23, 13, 0), i.e. the current time one day away, which for my use case (semi-free text calendar entry creation) isn't very intuitive. The workaround I've got currently is a first pass scan of the input, extracting variants of "all day" and setting a flag to strip the time components later, which also isn't ideal.
As a feature request
I believe we should still always return a datetime by default, so as not to break any existing compatibility. This could be put behind a new setting, such that parsing a string would result in one of:
always a datetime (the default, and current behaviour)
always a date, ignore times
prefer a datetime, unless "all day" text is detected (in which case ignore any times)
prefer date, unless time text detected (in which case ignore any "all day"s)
The third case could alternatively just be done without a setting, by checking for the presence of "all day" or variants, though callers would need to be aware that they might not get a datetime back.
Sorry for not getting back to you on this -- I had a look at DateDataParser but the period field doesn't distinguish as I'd expect it to for the original examples:
Activity
Gallaecio commentedon Jul 13, 2021
This would be related to #700
noviluni commentedon Jul 24, 2021
I explained here how to know the "smallest part" that has been parsed: #700 (comment)
However, what you say makes sense, it could be interesting to add an option to return dates instead of a datetime. π
Terrance commentedon Oct 17, 2021
Sorry for not getting back to you on this -- I had a look at
DateDataParserbut theperiodfield doesn't distinguish as I'd expect it to for the original examples:In fact, from a quick dig into
parse_with_formats(), it seems like the only periods we'll get back aredayormonth?For what it's worth, I am +1 for the
found_partsproposal in #700 as that would also cover what I need.Terrance commentedon Oct 17, 2021
Apologies, just noticed I need to set
RETURN_TIME_AS_PERIODto get the right value -- seems to work now!