diff --git a/pycron/__init__.py b/pycron/__init__.py index 3e74887..a869e41 100644 --- a/pycron/__init__.py +++ b/pycron/__init__.py @@ -117,15 +117,25 @@ def is_now(s, dt=None): """ if dt is None: dt = datetime.now() - minute, hour, dom, month, dow = s.split(" ") + minute, hour, dom, month, dow = [x.strip() for x in s.split(" ")] weekday = dt.isoweekday() + # Special case if both of the 'day' -fields are set -> allow either one to match + # See: https://github.com/kipe/pycron/issues/29 + if "*" not in dom and "*" not in dow: + day_rule = _parse_arg(dom, dt.day) or _parse_arg( + dow, 0 if weekday == 7 else weekday, True + ) + else: + day_rule = _parse_arg(dom, dt.day) and _parse_arg( + dow, 0 if weekday == 7 else weekday, True + ) + return ( _parse_arg(minute, dt.minute) and _parse_arg(hour, dt.hour) - and _parse_arg(dom, dt.day) and _parse_arg(month, dt.month) - and _parse_arg(dow, 0 if weekday == 7 else weekday, True) + and day_rule ) diff --git a/tests/test_dom_dow.py b/tests/test_dom_dow.py new file mode 100644 index 0000000..7f649d5 --- /dev/null +++ b/tests/test_dom_dow.py @@ -0,0 +1,30 @@ +import unittest +from datetime import datetime +import pycron +from pytz import utc +import pendulum +import arrow +import udatetime +from delorean import Delorean + + +class DOMDOWTestCase(unittest.TestCase): + def test_dom(self): + def run(now): + assert pycron.is_now("* * * * *", now) + assert pycron.is_now("* * 22 * thu", now) + assert pycron.is_now("* * 23 * thu", now) + assert pycron.is_now("* * 23 * fri", now) + assert pycron.is_now("* * 23 * *", now) + assert pycron.is_now("* * * * thu", now) + assert pycron.is_now("* * * * *", now) + assert pycron.is_now("* * * * fri", now) is False + assert pycron.is_now("* * 22 * *", now) is False + + now = datetime(2022, 6, 23, 16, 44) # Thursday + 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)