issue #69: added day_or option to change behavior when day-of-month and day-of-week is given

cron defineds that day-of-month and day-of-week should be connected using OR, however, an AND
connection would be expected by some users as it is implemented in fcron. This day_or option allows
to switch between the behaviour
This commit is contained in:
Andreas Vogl 2016-12-22 09:25:45 +01:00
parent ba293797b9
commit 21cb2ac858
2 changed files with 21 additions and 2 deletions

View File

@ -63,8 +63,10 @@ class croniter(object):
bad_length = 'Exactly 5 or 6 columns has to be specified for iterator' \
'expression.'
def __init__(self, expr_format, start_time=None, ret_type=float):
def __init__(self, expr_format, start_time=None, ret_type=float, day_or=True):
self._ret_type = ret_type
self._day_or = day_or
if start_time is None:
start_time = time()
@ -240,7 +242,8 @@ class croniter(object):
raise TypeError("Invalid ret_type, only 'float' or 'datetime' "
"is acceptable.")
if expanded[2][0] != '*' and expanded[4][0] != '*':
# exception to support day of month and day of week as defined in cron
if (expanded[2][0] != '*' and expanded[4][0] != '*') and self._day_or:
bak = expanded[4]
expanded[4] = ['*']
t1 = self._calc(self.cur, expanded, is_prev)

View File

@ -145,6 +145,22 @@ class CroniterTest(base.TestCase):
self.assertEqual(n3.day, 3)
self.assertEqual(n3.year, 2010)
def testWeekDayDayAnd(self):
base = datetime(2010, 1, 25)
itr = croniter('0 0 1 * mon', base, day_or=False)
n1 = itr.get_next(datetime)
self.assertEqual(n1.month, 2)
self.assertEqual(n1.day, 1)
self.assertEqual(n1.year, 2010)
n2 = itr.get_next(datetime)
self.assertEqual(n2.month, 3)
self.assertEqual(n2.day, 1)
self.assertEqual(n2.year, 2010)
n3 = itr.get_next(datetime)
self.assertEqual(n3.month, 11)
self.assertEqual(n3.day, 1)
self.assertEqual(n3.year, 2010)
def testMonth(self):
base = datetime(2010, 1, 25)
itr = croniter('0 0 1 * *', base)