-
-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for relative dates using dateparser #484
base: master
Are you sure you want to change the base?
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.
@@ -2,4 +2,5 @@ arrow>=1.0.0 | |||
click>=8.0 | |||
click-didyoumean | |||
colorama; sys_platform == "win32" | |||
dateparser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
dateparser
for 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
DateTime
parameter value to fall back ondateparser
for 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.