How to determine whether dateparser.search.search_dates() returns dates, times, or datetimes

≡放荡痞女 提交于 2020-03-02 19:14:12

问题


Trying to answer another question I stumbled over the following problem:

dateparser.search.search_dates() returns a list of tuples, each tuple consisting of a text segment recognized as carrying date and/or time information, and the corresponding datetime (plus an optional language code, if requested).

If the segment was of date type, only the date fields of the datetime are set. The others are taken from the current time, or from a base datetime that can be supplied via the settings parameter. Vice versa if the segment was of time type. Only if the segment carried both date and time information, all the date and time fields of the datetime are set.

☞ I have found no way to tell which fields of the datetime have been set and which not. ☜

I might supply a particular base datetime, and check which field group has changed, but relative dates would be offset from this base, not to mention other problems.

What am I missing? How to tell dates, times and datetimes apart?

Example code:

for s in ("Soccer with @homies at Payne Whitney tomorrow at 2 pm to 4 pm!",
          "Soccer with @homies at Payne Whitney tomorrow starting at 2 pm to 4 pm!"):
    print()
    print(s)
    for part in search_dates(s):
        print(part)

Output:

Soccer with @homies at Payne Whitney tomorrow at 2 pm to 4 pm!
('tomorrow at 2 pm', datetime.datetime(2020, 1, 15, 14, 0))
('4 pm', datetime.datetime(2020, 1, 14, 16, 0))

Soccer with @homies at Payne Whitney tomorrow starting at 2 pm to 4 pm!
('tomorrow', datetime.datetime(2020, 1, 15, 13, 31, 40, 681832))
('at 2 pm', datetime.datetime(2020, 1, 14, 14, 0))
('4 pm', datetime.datetime(2020, 1, 14, 16, 0))

来源:https://stackoverflow.com/questions/59726449/how-to-determine-whether-dateparser-search-search-dates-returns-dates-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!