Skip to content

Parsing dates with no times, without assuming default timesΒ #937

Closed

Description

@Terrance

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.

Activity

  1. Gallaecio commented on Jul 13, 2021

    @Gallaecio
    Contributor

    This would be related to #700

  2. noviluni commented on Jul 24, 2021

    @noviluni
    Contributor

    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. πŸ‘

  3. Terrance commented on Oct 17, 2021

    @Terrance
    Author

    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:

    >>> dp = DateDataParser()
    >>> dp.get_date_data("tomorrow")
    DateData(date_obj=datetime.datetime(2021, 10, 18, 13, 22, 11, 104649), period='day', locale='en')
    >>> dp.get_date_data("tomorrow 4pm")
    DateData(date_obj=datetime.datetime(2021, 10, 18, 16, 0), period='day', locale='en')  # period should be 'time'?
    >>> dp.get_date_data("4pm")
    DateData(date_obj=datetime.datetime(2021, 10, 17, 16, 0), period='day', locale='en')  # ditto

    In fact, from a quick dig into parse_with_formats(), it seems like the only periods we'll get back are day or month?

    For what it's worth, I am +1 for the found_parts proposal in #700 as that would also cover what I need.

  4. Terrance commented on Oct 17, 2021

    @Terrance
    Author

    Apologies, just noticed I need to set RETURN_TIME_AS_PERIOD to get the right value -- seems to work now!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions