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

Restrict both day-of-week and day-of-month like vixie-cron #75

Open
tintamarre opened this issue Jul 8, 2024 · 2 comments
Open

Restrict both day-of-week and day-of-month like vixie-cron #75

tintamarre opened this issue Jul 8, 2024 · 2 comments

Comments

@tintamarre
Copy link

According to the cron specification, it is not possible to restrict both the day-of-week and the day-of-month simultaneously:

While normally the job is executed when the time/date specification fields all match the current time and date, there is one exception: if both "day of month" and "day of week" are restricted (not "*"), then either the "day of month" field (3) or the "day of week" field (5) must match the current day.
Source: https://en.wikipedia.org/wiki/Cron#cite_ref-posix_8-0

Although it is generally not possible to restrict both fields, there is a hacky workaround described here and working for vixie-cron.

Here is an example on cronguru restricting both day of month and day of week: https://crontab.guru/#42_5_*/32,10-17_*_Tue

When using Croniter, this workaround doesn't seem to work. Is this the intended behavior?

Thanks !
Martin

@kiorky
Copy link
Owner

kiorky commented Jul 12, 2024

Please give reproducible cases to be sure to understand your use case.

@tintamarre
Copy link
Author

tintamarre commented Jul 12, 2024

Of course. :-)

from croniter import croniter
from datetime import datetime
base = datetime(2024, 7, 12)
iter = croniter('1 1 */32,1-7 * 2', base) #First Tuesday of the month a 01:01

for _ in range(4):
    print(iter.get_next(datetime)) 

Results

2024-07-16 01:01:00
2024-07-23 01:01:00
2024-07-30 01:01:00
2024-08-01 01:01:00

Expected results (ref: Crontab.guru: https://crontab.guru/#1_1_*/32,1-7_*_2)

2024-08-06 01:01:00
2024-09-03 01:01:00
2024-10-01 01:01:00
2024-11-05 01:01:00

The proposed test

def testFirstTuesdayOfMonth(self):
    it = croniter("1 1 */32,1-7 * 2", datetime(2024, 7, 12))
    self.assertEqual(it.get_next(datetime), datetime(2024, 8, 6, 1, 1))
    self.assertEquat(it.get_next(datetime), datetime(2024, 9, 3, 1, 1))
    self.assertEqual(it.get_next(datetime), datetime(2024, 10, 1, 1, 1))
    self.assertEqual(it.get_next(datetime), datetime(2024, 11, 5, 1, 1))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants