Open MatthewWilkes opened 2 years ago
Would this support syntax like -3h
? This is the syntax I would like the most.
I implemented a version of this locally where the base (and only) unit was minutes, so you could do do watson start --at -5
to start 5 minutes ago. I think that worked really well; here is the code in case it is helpful:
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.