From a97909c17070bdee4b9ddc3654eae6a5a75c7784 Mon Sep 17 00:00:00 2001 From: Kimmo Huoman Date: Mon, 23 Jul 2018 21:33:40 +0300 Subject: [PATCH] Fix issue #14. --- pycron/__init__.py | 29 ++++++++++++++++++++--------- tests/test_minute.py | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/pycron/__init__.py b/pycron/__init__.py index 3854522..e4c804a 100644 --- a/pycron/__init__.py +++ b/pycron/__init__.py @@ -26,6 +26,26 @@ def _parse_arg(value, target): except ValueError: pass + if '-' in value: + step = 1 + if '/' in value: + # Allow divider in values, see issue #14 + try: + start, tmp = [x.strip() for x in value.split('-')] + start = int(start) + end, step = [int(x) for x in tmp.split('/')] + except ValueError: + continue + else: + try: + start, end = [int(x.strip()) for x in value.split('-')] + except ValueError: + continue + + # If target value is in the range, it matches + if target in range(start, end + 1, step): + return True + if '/' in value: v, interval = [x.strip() for x in value.split('/')] # Not sure if applicable for every situation, but just to make sure... @@ -35,15 +55,6 @@ def _parse_arg(value, target): if target % int(interval) == 0: return True - if '-' in value: - try: - start, end = [int(x.strip()) for x in value.split('-')] - except ValueError: - continue - # If target value is in the range, it matches - if target in range(start, end + 1): - return True - return False diff --git a/tests/test_minute.py b/tests/test_minute.py index 3cc3149..fc4adcc 100644 --- a/tests/test_minute.py +++ b/tests/test_minute.py @@ -28,6 +28,9 @@ def run(now): assert pycron.is_now('9,5-8 * * * *', now) assert pycron.is_now('10,20-30 * * * *', now) is False + # Issue 14 + assert pycron.is_now('1-59/2 * * * *', now) is True + now = datetime(2015, 6, 18, 0, 9) run(now) run(now.replace(tzinfo=utc)) @@ -35,3 +38,20 @@ def run(now): run(arrow.get(now)) run(udatetime.from_string(now.isoformat())) run(Delorean(datetime=now, timezone='UTC').datetime) + + +def test_last_minute(): + def run(now): + assert pycron.is_now('* * * * *', now) + assert pycron.is_now('59 * * * *', now) + assert pycron.is_now('*/1 * * * *', now) + # Issue 14 + assert pycron.is_now('1-59/2 * * * *', now) is True + + now = datetime(2015, 6, 18, 0, 59) + run(now) + run(now.replace(tzinfo=utc)) + run(pendulum.instance(now)) + run(arrow.get(now)) + run(udatetime.from_string(now.isoformat())) + run(Delorean(datetime=now, timezone='UTC').datetime)