Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DOM and DOW parsing logic #34

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading