Skip to content

Commit

Permalink
Be stricter on month=0
Browse files Browse the repository at this point in the history
This relates #6
  • Loading branch information
kiorky committed Dec 3, 2021
1 parent bf19b21 commit 8e5c9e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
6 changes: 3 additions & 3 deletions docs/CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Changelog
==============

1.0.16 (unreleased)
1.1.0 (unreleased)
-------------------

- Nothing changed yet.

- Enforce validation for month=1. Before this release we used to support month=0 and it was silently glided to month=1 to support having both day in month in 4th field when it came to have 6fields cron forms (second repeat). It will now raises a CroniterBadDateError. See https://github.com/kiorky/croniter/issues/6
[kiorky]

1.0.15 (2021-06-25)
-------------------
Expand Down
8 changes: 7 additions & 1 deletion src/croniter/croniter.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,13 @@ def _expand(cls, expr_format, hash_id=None):
except ValueError:
pass

if t in cls.LOWMAP[i]:
if t in cls.LOWMAP[i] and not (
# do not support 0 as a month either for classical 5 fields cron
# or 6fields second repeat form
# but still let conversion happen if day field is shifted
(i == 3 and len(expressions) == 5) or
(i == 4 and len(expressions) == 6)
):
t = cls.LOWMAP[i][t]

if (
Expand Down
25 changes: 14 additions & 11 deletions src/croniter/tests/test_croniter.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,14 @@ def testOptimizeCronExpressions(self):
wildcard = ['*']
m, h, d, mon, dow, s = range(6)
# Test each field individually
self.assertEqual(croniter('0-59 0 0 0 0').expanded[m], wildcard)
self.assertEqual(croniter('0 0-23 0 0 0').expanded[h], wildcard)
self.assertEqual(croniter('0 0 0-31 0 0').expanded[d], wildcard)
self.assertEqual(croniter('0-59 0 0 1 0').expanded[m], wildcard)
self.assertEqual(croniter('0 0-23 0 1 0').expanded[h], wildcard)
self.assertEqual(croniter('0 0 0-31 1 0').expanded[d], wildcard)
self.assertEqual(croniter('0 0 0 1-12 0').expanded[mon], wildcard)
self.assertEqual(croniter('0 0 0 0 0-6').expanded[dow], wildcard)
self.assertEqual(croniter('0 0 0 0 1-7').expanded[dow], wildcard)
self.assertEqual(croniter('0 0 0 0 1-7,sat#3').expanded[dow], wildcard)
self.assertEqual(croniter('0 0 0 0 0 0-59').expanded[s], wildcard)
self.assertEqual(croniter('0 0 0 1 0-6').expanded[dow], wildcard)
self.assertEqual(croniter('0 0 0 1 1-7').expanded[dow], wildcard)
self.assertEqual(croniter('0 0 0 1 1-7,sat#3').expanded[dow], wildcard)
self.assertEqual(croniter('0 0 0 1 0 0-59').expanded[s], wildcard)
# Real life examples
self.assertEqual(croniter('30 1-12,0,10-23 15-21 * fri').expanded[h], wildcard)
self.assertEqual(croniter('30 1-23,0 15-21 * fri').expanded[h], wildcard)
Expand Down Expand Up @@ -993,12 +993,12 @@ def test_issue_monsun_117(self):
'2019-01-17 00:00:01',
'2019-01-18 00:00:02',
'2019-01-19 00:00:03',
'2019-01-20 00:00:04',
'2019-01-23 00:00:00',
'2019-01-24 00:00:01',
'2019-01-25 00:00:02',
'2019-01-26 00:00:03',
'2019-01-27 00:00:04'])
'2019-01-30 00:00:00',
'2019-01-31 00:00:01'])

def test_mixdow(self):
base = datetime(2018, 10, 1, 0, 0)
Expand Down Expand Up @@ -1433,11 +1433,14 @@ def test_issue156(self):

def test_confirm_sort(self):
m, h, d, mon, dow, s = range(6)
self.assertListEqual(croniter('0 8,22,10,23 0 0 0').expanded[h], [8, 10, 22, 23])
self.assertListEqual(croniter('0 0 25-L 0 0').expanded[d], [25, 26, 27, 28, 29, 30, 31])
self.assertListEqual(croniter('0 8,22,10,23 0 1 0').expanded[h], [8, 10, 22, 23])
self.assertListEqual(croniter('0 0 25-L 1 0').expanded[d], [25, 26, 27, 28, 29, 30, 31])
self.assertListEqual(croniter("1 1 7,14,21,L * *").expanded[d], [7, 14, 21, "l"])
self.assertListEqual(croniter("0 0 * * *,sat#3").expanded[dow], ["*", 6])

def test_issue_k6(self):
self.assertRaises(CroniterBadCronError, croniter, '0 0 0 0 0')


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

0 comments on commit 8e5c9e0

Please sign in to comment.