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); when either field starts with * (a bare *, or * with a step), a day must match both. 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.

Contents

NameWhat it is
matchesReport whether a time fires the schedule (minute granularity - seconds are ignored).
nextFind the next time at or after after that fires the schedule.
parseParse a five-field cron expression into a Schedule.
Schedulestruct: A parsed cron schedule: the allowed values per field.

Functions

matches

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

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

schedule
the schedule
t
the instant to test
Returns bool
true if the schedule fires at t

next

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

Find the next time at or after after that fires the schedule. Skips non-matching days and hours whole, then searches minute by minute, up to a nine-year horizon (wide enough for a Feb-29-only schedule).

schedule
the schedule
after
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

parse

jennifer
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.

expr
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

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.

jennifer
def struct Schedule {
    minutes as list of int,
    hours as list of int,
    daysOfMonth as list of int,
    months as list of int,
    weekdays as list of int,
    domStar as bool,
    dowStar as bool,
    reboot as bool
};
minutes
allowed minutes (0-59)
hours
allowed hours (0-23)
daysOfMonth
allowed days of the month (1-31)
months
allowed months (1-12)
weekdays
allowed ISO weekdays (1-7, Monday = 1)
domStar
whether the day-of-month field starts with * (a bare *, or * with a step)
dowStar
whether the day-of-week field starts with *
reboot
true only for the @reboot macro (a startup-only schedule that never fires a time: matches is always false and next throws)