Skip to content

Commit

Permalink
Update DOM and DOW parsing logic
Browse files Browse the repository at this point in the history
- Added a special case handling when both 'day' fields are set to allow either one to match. This addresses issue #29.
- Updated the parsing logic for 'dom' and 'dow' arguments, using `_parse_arg` function with appropriate default values.
  • Loading branch information
kipe committed Sep 27, 2024
1 parent 05b277c commit 47a960a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pycron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down
30 changes: 30 additions & 0 deletions tests/test_dom_dow.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 47a960a

Please sign in to comment.