pythoncronquartz-scheduler

Parse Quartz cron expression with Croniter


This code:

starting_point = datetime.datetime.now() - datetime.timedelta(hours=1)
cron = croniter.croniter('44 5 19 ? * WED,THU,FRI *', starting_point)
cron.get_next(datetime.datetime)

Results in:

croniter.croniter.CroniterBadCronError: Exactly 5 or 6 columns has to be specified for iterator expression.

But when using https://www.freeformatter.com/cron-expression-generator-quartz.html, the same expression results in

At 19:05:44pm, on every Wednesday, Thursday and Friday, every month

How to parse this cron expression with croniter?


Solution

  • The website you are referring to uses the Quartz Scheduler which supports the fields

    Seconds, Minutes, Hours, Day Of Month, Month, Day Of Week, Year
    

    but with croniter the Seconds field is optional and apparently is expected at the last position (I did not find this to be particularly well documented and have concluded this through experimentation):

    Minutes, Hours, Day Of Month, Day Of Week, Seconds
    

    Furthermore it does not seem to support the ? wildcard.

    So an equivalent expression seems to be

    5 19 * * WED,THU,FRI 44
    

    Demo:

    >>> import croniter
    >>> import datetime
    >>> c = croniter.croniter('5 19 * * WED,THU,FRI 44')
    >>> print(datetime.datetime.utcfromtimestamp(next(c)))
    2021-04-23 19:05:44
    >>> print(datetime.datetime.utcfromtimestamp(next(c)))
    2021-04-28 19:05:44
    >>> print(datetime.datetime.utcfromtimestamp(next(c)))
    2021-04-29 19:05:44