问题
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