Skip to content
Jennifer Programming Language

cron API reference

Parse and evaluate cron expressions - the five-field schedule spec minute hour day-of-month month day-of-week. parse turns an expression into a Schedule, matches tests whether a time.Time fires it, and next finds the next fire at or after a given time. Each field takes *, single values, a-b ranges, a,b,c lists, and /n steps (a-b/n, or a wildcard with a step). Day-of-week is 0-7 (both 0 and 7 are Sunday). When both day-of-month and day-of-week are restricted, a day matching either fires (the standard cron rule). The month field also accepts the three-letter names JAN-DEC and the weekday field SUN-SAT (case-insensitive), anywhere a number works. A whole expression may be a nickname macro (@daily, @hourly, ...); @reboot parses to a Schedule that never fires (startup only).

A pure calculator over time - no clock, no sleeping. A scheduler is the caller's loop (spawn + time.sleep until cron.next). Both binaries.

Import with import "cron.j" as cron;. See the cron guide for prose and examples.

Functions

cron.matches(schedule as Schedule, t as time.Time)

Report whether a time fires the schedule (minute granularity - seconds are ignored).

Parameters

  • schedule {Schedule} - the schedule
  • t {time.Time} - the instant to test

Returns {bool} - true if the schedule fires at t

cron.next(schedule as Schedule, after as time.Time)

Find the next time at or after after that fires the schedule. Searches minute by minute (skipping non-matching days whole), up to a five-year horizon.

Parameters

  • schedule {Schedule} - the schedule
  • after {time.Time} - the earliest acceptable fire time (its zone is kept)

Returns {time.Time} - the next fire time (seconds zeroed)

Throws

  • {Error} - kind "cron" if nothing matches within the horizon

cron.parse(expr as string)

Parse a five-field cron expression into a Schedule. The whole expression may instead be a nickname macro (@yearly / @annually, @monthly, @weekly, @daily / @midnight, @hourly, @reboot). The month field accepts JAN-DEC and the weekday field SUN-SAT (case-insensitive) anywhere a number works.

Parameters

  • expr {string} - minute hour day-of-month month day-of-week, or a @macro

Returns {Schedule} - the parsed schedule

Throws

  • {Error} - kind "cron" on the wrong field count or an out-of-range value

Structs

cron.Schedule

A parsed cron schedule: the allowed values per field. weekdays are ISO weekdays (Monday = 1 ... Sunday = 7), normalized from the cron 0-7 form.

FieldTypeDescription
minuteslist of intallowed minutes (0-59)
hourslist of intallowed hours (0-23)
daysOfMonthlist of intallowed days of the month (1-31)
monthslist of intallowed months (1-12)
weekdayslist of intallowed ISO weekdays (1-7, Monday = 1)
domStarboolwhether the day-of-month field was "*"
dowStarboolwhether the day-of-week field was "*"
rebootbooltrue only for the @reboot macro (a startup-only schedule that never fires a time: matches is always false and next throws)