diff --git a/tests/test_utils.py b/tests/test_utils.py index 162c5e29..bfc53663 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -27,6 +27,10 @@ safe_save, parse_tags, PY2, + isTime, + isDateTime, + getDateTimeToday, + getMergedDateTime ) from . import mock_datetime @@ -228,3 +232,49 @@ def test_confirm_tags_reject_raises_abort(confirm): watson_tags = ['a', 'b'] with pytest.raises(Abort): confirm_project(tags, watson_tags) + + +@pytest.mark.parametrize("date_time_string, result", [ + ("2018-05-28 7:00", False), + ("2018-05.28 7:00", False), + ("7:00", True), + ("17:00", True), + ("117:00", False), + ("117", False), +]) +def test_isTime(date_time_string, result): + assert isTime(date_time_string) == result + + +@pytest.mark.parametrize("date_time_string, result", [ + ("2018-05-28 7:00", True), + ("2018-05-28 07:00", True), + ("2018-05-28 127:00", False), + ("2018-05-f 7:00", False), + ("2018-05-28 7.00", False), + ("7:00", False), +]) +def test_isDateTime(date_time_string, result): + assert isDateTime(date_time_string) == result + + +@pytest.mark.parametrize("date_time, time_string", [ + ("2014-04-01", "07:00"), + ("2014-04-01", "17:00"), +]) +def test_getMergedDateTime(date_time, time_string): + result = arrow.get("{} {}".format(date_time, time_string), 'YYYY-MM-DD HH:mm') + date = arrow.get(date_time, 'YYYY-MM-DD') + assert getMergedDateTime(date, time_string) == result + + +@pytest.mark.parametrize("time_string", [ + ("07:00"), + ("17:00"), + ("09:05"), + ("13:37"), +]) +def test_getDateTimeToday(time_string): + today = datetime.date.today().strftime("%Y-%m-%d") + result = arrow.get("{} {}".format(today, time_string), 'YYYY-MM-DD HH:mm').replace(tzinfo='local') + assert getDateTimeToday(time_string) == result diff --git a/watson/cli.py b/watson/cli.py index ad41fcdc..3d9f3f37 100644 --- a/watson/cli.py +++ b/watson/cli.py @@ -26,6 +26,10 @@ sorted_groupby, style, parse_tags, + isTime, + isDateTime, + getDateTimeToday, + getMergedDateTime ) @@ -1013,22 +1017,6 @@ def frames(watson): for frame in watson.frames: click.echo(style('short_id', frame.id)) -def isTime(date_time_string): - match = re.search("^\d{1,2}:\d{1,2}$", date_time_string) - return match != None - -def isDateTime(date_time_string): - match = re.search("^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}$", date_time_string) - return match != None - -def getDateTimeToday(time_string): - date_today = arrow.now().floor("day") - return getMergedDateTime(date_today, time_string) - -def getMergedDateTime(date_time, time_string): - hours, minutes = time_string.split(":") - return date_time.shift(hours=int(hours), minutes=int(minutes)) - @cli.command(context_settings={'ignore_unknown_options': True}) @click.argument('args', nargs=-1) @click.option('-f', '--from', 'from_', required=True, diff --git a/watson/utils.py b/watson/utils.py index 97f30ce2..7fa3253a 100644 --- a/watson/utils.py +++ b/watson/utils.py @@ -9,6 +9,7 @@ import click import arrow +import re from .fullmoon import get_last_full_moon @@ -282,3 +283,23 @@ def parse_tags(values_list): )) for i, w in enumerate(values_list) if w.startswith('+') )))) # pile of pancakes ! + + +def isTime(date_time_string): + match = re.search("^\d{1,2}:\d{1,2}$", date_time_string) + return match != None + + +def isDateTime(date_time_string): + match = re.search("^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}$", date_time_string) + return match != None + + +def getDateTimeToday(time_string): + date_today = arrow.now().floor("day") + return getMergedDateTime(date_today, time_string) + + +def getMergedDateTime(date_time, time_string): + hours, minutes = time_string.split(":") + return date_time.shift(hours=int(hours), minutes=int(minutes))