Repository navigation
Parse an hour-only time in relative dates, e.g. "tomorrow at 12" - #1450
AdrianAtZyte wants to merge 3 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1450 +/- ##
=======================================
Coverage 98.81% 98.82%
=======================================
Files 237 237
Lines 3218 3221 +3
=======================================
+ Hits 3180 3183 +3
Misses 38 38 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
serhii73
left a comment
There was a problem hiding this comment.
Thanks, tomorrow at 12 (and demain à 14, вчера в 12, morgen um 10) work now. One side effect:
since translation drops at, any bare trailing number becomes the hour. search_dates usually keeps
the count that follows a relative date in listings, so minute/hour offsets get a wrong time:
from datetime import datetime
from dateparser.search import search_dates
S = {"RELATIVE_BASE": datetime(2020, 6, 15, 9, 0)}
for s in ["Posted 5 minutes ago 3 replies", "John Doe 2 hours ago 3 replies",
"Updated 3 hours ago 20 comments", "2 hours ago 5 likes"]:
print(s, search_dates(s, settings=S))| input | master | this branch |
|---|---|---|
Posted 5 minutes ago 3 replies |
08:55 | 03:00 |
John Doe 2 hours ago 3 replies |
07:00 | 03:00 |
Updated 3 hours ago 20 comments |
06:00 | 20:00 (in the future) |
2 hours ago 5 likes |
07:00 | 05:00 |
(parse("5 minutes ago 3") gives the same 03:00.) Could the hour-only reading be limited to relative
dates without an hour/minute/second unit? A clock hour contradicts those anyway:
has_clock_units = any(
unit in ("hour", "minute", "second") for _, unit in PATTERN.findall(date_string)
)
date_string = PATTERN.sub("", date_string)
date_string = re.sub(r"\b(?:ago|in)\b", "", date_string).strip()
if not has_clock_units and re.fullmatch(r"\d{1,2}", date_string) and int(date_string) < 24:
return time(int(date_string))With that, the new tests still pass and the cases above give the same results as master.
Posted 3 days ago 15 comments would still get 15:00, but the date stays right.
Fixes #1080