Skip to content

Commit

Permalink
Fix combination of star and invalid expression bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kiorky committed Feb 1, 2021
1 parent be20afd commit 84a129c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
1.0.6 (unreleased)
------------------

- Nothing changed yet.
- Fix combination of star and invalid expression bugs
[kiorky]


1.0.5 (2021-01-29)
Expand Down
25 changes: 19 additions & 6 deletions src/croniter/croniter.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def __init__(self, expr_format, start_time=None, ret_type=float,
day_or=True, max_years_between_matches=None, is_prev=False):
self._ret_type = ret_type
self._day_or = day_or
self._expr_format = expr_format

self._max_years_btw_matches_explicitly_set = (
max_years_between_matches is not None)
Expand Down Expand Up @@ -297,7 +298,9 @@ def _calc(self, now, expanded, nth_weekday_of_month, is_prev):
DAYS = self.DAYS

def proc_month(d):
if expanded[3][0] != '*':
try:
expanded[3].index('*')
except ValueError:
diff_month = nearest_diff_method(
d.month, expanded[3], self.MONTHS_IN_YEAR)
days = DAYS[month - 1]
Expand All @@ -319,7 +322,9 @@ def proc_month(d):
return False, d

def proc_day_of_month(d):
if expanded[2][0] != '*':
try:
expanded[2].index('*')
except ValueError:
days = DAYS[month - 1]
if month == 2 and self.is_leap(year) is True:
days += 1
Expand All @@ -345,7 +350,9 @@ def proc_day_of_month(d):
return False, d

def proc_day_of_week(d):
if expanded[4][0] != '*':
try:
expanded[4].index('*')
except ValueError:
diff_day_of_week = nearest_diff_method(
d.isoweekday() % 7, expanded[4], 7)
if diff_day_of_week is not None and diff_day_of_week != 0:
Expand Down Expand Up @@ -409,7 +416,9 @@ def proc_day_of_week_nth(d):
return False, d

def proc_hour(d):
if expanded[1][0] != '*':
try:
expanded[1].index('*')
except ValueError:
diff_hour = nearest_diff_method(d.hour, expanded[1], 24)
if diff_hour is not None and diff_hour != 0:
if is_prev:
Expand All @@ -421,7 +430,9 @@ def proc_hour(d):
return False, d

def proc_minute(d):
if expanded[0][0] != '*':
try:
expanded[0].index('*')
except ValueError:
diff_min = nearest_diff_method(d.minute, expanded[0], 60)
if diff_min is not None and diff_min != 0:
if is_prev:
Expand All @@ -433,7 +444,9 @@ def proc_minute(d):

def proc_second(d):
if len(expanded) == 6:
if expanded[5][0] != '*':
try:
expanded[5].index('*')
except ValueError:
diff_sec = nearest_diff_method(d.second, expanded[5], 60)
if diff_sec is not None and diff_sec != 0:
d += relativedelta(seconds=diff_sec)
Expand Down
8 changes: 8 additions & 0 deletions src/croniter/tests/test_croniter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,14 @@ def test_overflow(self):
"""."""
self.assertRaises(CroniterBadCronError, croniter , "0-10000000 * * * *", datetime.now())

def test_issue156(self):
"""."""
dt = croniter("* * * * *,0", datetime(2019, 1, 14, 11, 0, 59, 999999)).get_next()
self.assertEqual(1547463660.0, dt)
self.assertRaises(CroniterBadCronError, croniter, "* * * * *,b")
dt = croniter("0 0 * * *,sat#3", datetime(2019, 1, 14, 11, 0, 59, 999999)).get_next()
self.assertEqual(1547856000.0, dt)


if __name__ == '__main__':
unittest.main()

0 comments on commit 84a129c

Please sign in to comment.