Add support for relative dates using dateparser#484
Add support for relative dates using dateparser#484MatthewWilkes wants to merge 1 commit intojazzband:masterfrom
Conversation
If a date is entered but it cannot be parsed by arrow, try parsing it with dateparser. This enables support for a great many (but sadly not all) relative date expressions.
| click>=8.0 | ||
| click-didyoumean | ||
| colorama; sys_platform == "win32" | ||
| dateparser |
There was a problem hiding this comment.
given that arrow is already here, wouldn't hurt to rework this change with arrow.dehumanize(s).
Can't talk for the maintainers, but I'd say using existing dependencies rather than adding a new one would increase chance of this being pulled in.
There was a problem hiding this comment.
This also looks like it could replace my PR more cleanly, so if you were to ingest the tests that my PR supports and take it further to days/weeks etc, that'd be an improvement.
|
Would this support syntax like |
|
I implemented a version of this locally where the base (and only) unit was minutes, so you could do do commit ea0c189b1b0a0504638e18efcb1584ef41743af3 (HEAD -> log-length_and_master_merged)
Author: Joel Ostblom <joel.ostblom@gmail.com>
Date: Mon Feb 28 09:45:43 2022 -0800
Allow negative integers to --at for specifying "X min ago"
diff --git a/watson/cli.py b/watson/cli.py
index 580e0af..a21e08c 100644
--- a/watson/cli.py
+++ b/watson/cli.py
@@ -109,20 +109,29 @@ class DateTimeParamType(click.ParamType):
def _parse_multiformat(self, value) -> arrow:
date = None
- for fmt in (None, 'HH:mm:ss', 'HH:mm'):
+
+ # Only allow for negative times
+ # to avoid that they are mistaken for the hour of the day
+ if isinstance(value, str) and value[0] == '-':
try:
- if fmt is None:
- date = arrow.get(value)
- else:
- date = arrow.get(value, fmt)
- date = arrow.now().replace(
- hour=date.hour,
- minute=date.minute,
- second=date.second
- )
- break
+ date = arrow.now().shift(minutes=int(value))
except (ValueError, TypeError):
pass
+ else:
+ for fmt in (None, 'HH:mm:ss', 'HH:mm'):
+ try:
+ if fmt is None:
+ date = arrow.get(value)
+ else:
+ date = arrow.get(value, fmt)
+ date = arrow.now().replace(
+ hour=date.hour,
+ minute=date.minute,
+ second=date.second
+ )
+ break
+ except (ValueError, TypeError):
+ pass
return date |
Hello! I appreciate this is a long-standing community discussion, and this PR may not be super helpful, but I implemented it to scratch my own itch and thought I'd offer it up.
Following the discussion in #10 there was a proposal to move to using
dateparserfor formatting dates. This isn't a perfect library, it currently makes some silly mistakes like parsing "1.5 hours ago" as "5 hours ago" and can be somewhat slow for confusing formats, but for all other day-to-day requirements I personally have, it handles them well.This changes the
DateTimeparameter value to fall back ondateparserfor any dates previously not allowable. This means that all currently acceptable dates are parsed in the same way, but some previously invalid dates are now allowable.There's also an issue at #475 for relative dates specifically, which is implemented by PR #481. That implements a subset of the parsing directly using arrow, which I understand may well be preferable. I'm under no illusions that 1 hour of hacking is anything special, but wanted to share rather than hide it away.