Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MatthewWilkes
Copy link

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 on dateparser 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.

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

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.

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.

@kuon
Copy link

kuon commented Aug 26, 2024

Would this support syntax like -3h ? This is the syntax I would like the most.

@joelostblom
Copy link
Contributor

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 <[email protected]>
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants