Skip to content

Commit

Permalink
Merge pull request kipe#15 from kipe/issue_14
Browse files Browse the repository at this point in the history
Allow range to define a divider.
  • Loading branch information
kipe authored Sep 28, 2018
2 parents 38478b7 + 5b429c9 commit ad3470e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
29 changes: 20 additions & 9 deletions pycron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand All @@ -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


Expand Down
40 changes: 40 additions & 0 deletions tests/test_minute.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,50 @@ 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))
run(pendulum.instance(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)


def test_minute_ranges():
for i in range(1, 59, 2):
now = datetime(2015, 6, 18, 0, i)
assert pycron.is_now('1-59/2 * * * *', now)
assert pycron.is_now('1-59/2 * * * *', now.replace(tzinfo=utc))
assert pycron.is_now('1-59/2 * * * *', pendulum.instance(now))
assert pycron.is_now('1-59/2 * * * *', arrow.get(now))
assert pycron.is_now('1-59/2 * * * *', udatetime.from_string(now.isoformat()))
assert pycron.is_now('1-59/2 * * * *', Delorean(datetime=now, timezone='UTC').datetime)

for i in range(0, 59, 2):
now = datetime(2015, 6, 18, 0, i)
assert pycron.is_now('1-59/2 * * * *', now) is False
assert pycron.is_now('1-59/2 * * * *', now.replace(tzinfo=utc)) is False
assert pycron.is_now('1-59/2 * * * *', pendulum.instance(now)) is False
assert pycron.is_now('1-59/2 * * * *', arrow.get(now)) is False
assert pycron.is_now('1-59/2 * * * *', udatetime.from_string(now.isoformat())) is False
assert pycron.is_now('1-59/2 * * * *', Delorean(datetime=now, timezone='UTC').datetime) is False

0 comments on commit ad3470e

Please sign in to comment.