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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions docs/user-guide/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ Else, print the total for each root project.

By default, the time spent the last 7 days is printed. This timespan
can be controlled with the `--from` and `--to` arguments. The dates
must have the format `YEAR-MONTH-DAY`, like: `2014-05-19`.
should have the format `YEAR-MONTH-DAY`, like: `2014-05-19`, but relative
formats like `two weeks ago` are also supported.

You can limit the report to a project or a tag using the `--project` and
`--tag` options. They can be specified several times each to add multiple
Expand Down Expand Up @@ -234,7 +235,8 @@ Display each recorded session during the given timespan.

By default, the sessions from the last 7 days are printed. This timespan
can be controlled with the `--from` and `--to` arguments. The dates
must have the format `YEAR-MONTH-DAY`, like: `2014-05-19`.
should have the format `YEAR-MONTH-DAY`, like: `2014-05-19`, but relative
formats like `two weeks ago` are also supported.

You can also use special shortcut options for easier timespan control:
`--day` sets the log timespan to the current day (beginning at `00:00h`)
Expand Down Expand Up @@ -457,7 +459,8 @@ Else, print the total for each root project.

By default, the time spent the last 7 days is printed. This timespan
can be controlled with the `--from` and `--to` arguments. The dates
must have the format `YEAR-MONTH-DAY`, like: `2014-05-19`.
should have the format `YEAR-MONTH-DAY`, like: `2014-05-19`, but relative
formats like `two weeks ago` are also supported.

You can also use special shortcut options for easier timespan control:
`--day` sets the report timespan to the current day (beginning at `00:00h`)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

requests
46 changes: 37 additions & 9 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# Not all ISO-8601 compliant strings are recognized by arrow.get(str)
VALID_DATES_DATA = [
('2018', '2018-01-01 00:00:00'), # years
(' 2018', '2018-01-01 00:00:00'),
('2018 ', '2018-01-01 00:00:00'),
('2018-04', '2018-04-01 00:00:00'), # calendar dates
('2018-04-10', '2018-04-10 00:00:00'),
('2018/04/10', '2018-04-10 00:00:00'),
Expand All @@ -20,9 +22,12 @@
('2018/4/10', '2018-04-10 00:00:00'),
('2018.4.10', '2018-04-10 00:00:00'),
('20180410', '2018-04-10 00:00:00'),
('18-04-10', '2018-04-10 00:00:00'),
('2018-04-10T', '2018-04-10 00:00:00'),
('2018-123', '2018-05-03 00:00:00'), # ordinal dates
('2018-04-10 12:30:43', '2018-04-10 12:30:43'),
('2018-04-10T12:30:43', '2018-04-10 12:30:43'),
('2018-04-10T12:30:43.', '2018-04-10 12:30:43'),
('2018-04-10 12:30:43Z', '2018-04-10 12:30:43'),
('2018-04-10 12:30:43.1233', '2018-04-10 12:30:43'),
('2018-04-10 12:30:43+03:00', '2018-04-10 12:30:43'),
Expand All @@ -44,27 +49,50 @@
.replace(hour=14, minute=5, second=0)
.format('YYYY-MM-DD HH:mm:ss')
),
(
'14:05:12.000',
arrow.now()
.replace(hour=14, minute=5, second=12, microsecond=0)
.format('YYYY-MM-DD HH:mm:ss')
),
(
'14.05',
arrow.now()
.replace(hour=14, minute=5, second=0)
.format('YYYY-MM-DD HH:mm:ss')
),

('2018-W08', '2018-02-19 00:00:00'), # week dates
('2018W08', '2018-02-19 00:00:00'),
('2018-W08-2', '2018-02-20 00:00:00'),
('2018W082', '2018-02-20 00:00:00'),
('yesterday 9am',
arrow.now()
.shift(days=-1)
.replace(hour=9, minute=0, second=0)
.format('YYYY-MM-DD HH:mm:ss')
),
(
'tomorrow 9am',
arrow.now()
.shift(days=1)
.replace(hour=9, minute=0, second=0)
.format('YYYY-MM-DD HH:mm:ss')
),
(
'1.5 hours ago',
arrow.now()
.shift(hours=-1.5)
.format('YYYY-MM-DD HH:mm:ss')
),
]

INVALID_DATES_DATA = [
(' 2018'),
('2018 '),
('201804'),
('18-04-10'),
('180410'), # truncated representation not allowed
('hello 2018'),
('yesterday'),
('tomorrow'),
('14:05:12.000'), # Times alone are not allowed
('140512.000'),
('140512'),
('14.05'),
('2018-04-10T'),
('2018-04-10T12:30:43.'),
]

VALID_TIMES_DATA = [
Expand Down
7 changes: 7 additions & 0 deletions watson/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import warnings

from .watson import __version__ # noqa
from .watson import Watson, WatsonError

__all__ = ['Watson', 'WatsonError']

warnings.filterwarnings(
"ignore",
message="The localize method is no longer necessary, as this time zone supports the fold attribute",
)
16 changes: 13 additions & 3 deletions watson/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import arrow
import click
from click_didyoumean import DYMGroup
import dateparser

import watson as _watson
from .autocompletion import (
Expand Down Expand Up @@ -109,6 +110,8 @@ def convert(self, value, param, ctx) -> arrow:

def _parse_multiformat(self, value) -> arrow:
date = None
if isinstance(value, str):
value = value.strip()
for fmt in (None, 'HH:mm:ss', 'HH:mm'):
try:
if fmt is None:
Expand All @@ -123,6 +126,10 @@ def _parse_multiformat(self, value) -> arrow:
break
except (ValueError, TypeError):
pass
if date is None:
date = dateparser.parse(value, settings={'DATE_ORDER': 'YMD'})
if date:
date = arrow.get(date)
return date


Expand Down Expand Up @@ -555,7 +562,8 @@ def report(watson, current, from_, to, projects, tags, ignore_projects,

By default, the time spent the last 7 days is printed. This timespan
can be controlled with the `--from` and `--to` arguments. The dates
must have the format `YEAR-MONTH-DAY`, like: `2014-05-19`.
should have the format `YEAR-MONTH-DAY`, like: `2014-05-19`, but relative
formats like `two weeks ago` are also supported.

You can also use special shortcut options for easier timespan control:
`--day` sets the report timespan to the current day (beginning at `00:00h`)
Expand Down Expand Up @@ -808,7 +816,8 @@ def aggregate(ctx, watson, current, from_, to, projects, tags, output_format,

By default, the time spent the last 7 days is printed. This timespan
can be controlled with the `--from` and `--to` arguments. The dates
must have the format `YEAR-MONTH-DAY`, like: `2014-05-19`.
should have the format `YEAR-MONTH-DAY`, like: `2014-05-19`, but relative
formats like `two weeks ago` are also supported.

You can limit the report to a project or a tag using the `--project` and
`--tag` options. They can be specified several times each to add multiple
Expand Down Expand Up @@ -978,7 +987,8 @@ def log(watson, current, reverse, from_, to, projects, tags, ignore_projects,

By default, the sessions from the last 7 days are printed. This timespan
can be controlled with the `--from` and `--to` arguments. The dates
must have the format `YEAR-MONTH-DAY`, like: `2014-05-19`.
should have the format `YEAR-MONTH-DAY`, like: `2014-05-19`, but relative
formats like `two weeks ago` are also supported.

You can also use special shortcut options for easier timespan control:
`--day` sets the log timespan to the current day (beginning at `00:00h`)
Expand Down