22

What's an efficient way to calculate the next run time of an event given the current time and a cron spec?

I'm looking for something other than "loop through every minute checking if it matches spec".

Examples of specs might be:

  • Every month, on the 1st and 15 at 15:01
  • At 10,20,30,40,50 mins past the hour every hour

Python code would be lovely but psuedo code or high level description would also be appreciated.

[Update] Assume the spec is already parsed and is in some reasonable format.

3
  • I've done something like this, and found it tricky to get right (and my possible "specs" are a subset of yours). I just did it with some datetime calcs and functions for each type of spec. I'm interested if someone has a better answer (solution will probably be use library: x)
    – Gerrat
    Jan 6, 2011 at 1:23
  • Can you give an example of the spec format? Jan 6, 2011 at 1:41
  • Later.js is a javascript library that does just this. It is able to parse a Cron expression and then calculate future occurrences of the schedule. It's algorithm isn't very fancy, but it does the job. Might be worth a look.
    – Bill
    Feb 9, 2012 at 18:36

1 Answer 1

32

Just looking at it, I think you need to:

  • parse the chron spec to five arrays containing acceptable values for each field;
  • parse 'now' to a value for each field;
  • in order of minute, hour, {day-of-month OR day-of-week}, month-of year: find the lowest array value that matches or exceeds the current value, correcting for carry.

I don't know how to handle day-of-week and day-of-month simultaneously; I am sure there is a way, but on the other hand I don't think I've ever seen a spec that actually specified both. I think it would be sufficient to write a handler for either and throw an error if you receive both.

Edit: apparently if day-of-week and day-of-month are both specified, it is supposed to fire on both - ie if the rule is '15th, Wednesday' it will fire on every 15th and every Wednesday.

The croniter package does what you want:

import croniter
import datetime

now = datetime.datetime.now()
sched = '1 15 1,15 * *'    # at 3:01pm on the 1st and 15th of every month
cron = croniter.croniter(sched, now)

for i in range(4):
    nextdate = cron.get_next(datetime.datetime)
    print nextdate

prints

2011-01-15 15:01:00
2011-02-01 15:01:00
2011-02-15 15:01:00
2011-03-01 15:01:00

although it would be nice if it were written as an actual iterator. Maybe I've got my next project ;-)

3
  • Nice find with croniter, much thanks. I'll leave the question open for a bit longer to see if anyone drops a fantastic algorithm on us.
    – Parand
    Jan 6, 2011 at 19:54
  • Thanks for the answer. I used your code to make it work, but I needed to run it as an EXE file so I compiled it into one using py2exe. For anyone wanting to use it you can download it from here: toughtomato.com/cron2NextDate. Usage example: cron2NextDate.exe "0 15 1 * *"
    – Martin
    Mar 18, 2011 at 12:55
  • Unfortunately croniter only evaluates in UTC time. I have to adjust my expressions to UTC time
    – ospider
    Dec 14, 2018 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.