12 // scheduleCronGap is the shortest time between two runs of a cron
13 // expression. Each run is a full turn, so shorter gaps use the quota
17 // scheduleCronYears is how many years the search for the next time
22 // scheduleCronFields are the fields of a cron expression, in order, with their
23 // lowest and highest values.
24 var scheduleCronFields = []struct {
56 // scheduleParseCron parses a cron expression of five fields: minute, hour, day
57 // of month, month, and day of week, where 0 and 7 are Sunday. A field is a
58 // comma list of items, and an item is *, N, or N-M, where * and N-M can have a
59 // step /S. Names, macros, the characters ? L W #, and N/S are errors.
61 // It refuses an expression whose runs can be less than scheduleCronGap minutes
62 // apart. The check takes the wall times of one day from the hour and the minute
63 // fields, and the gap across midnight as if the next day matches too. It also
64 // refuses an expression that matches no day in scheduleCronYears years, such as
65 // one on February 30.
66 func scheduleParseCron(expression string) (ScheduleCron, error) {
67 fields := strings.Fields(expression)
68 if len(fields) != len(scheduleCronFields) {
69 return ScheduleCron{}, fmt.Errorf("the cron expression %q has %d fields, not 5: minute, hour, day of month, month, day of week", expression, len(fields))
74 for index, field := range fields {
75 set, err := scheduleCronField(field, scheduleCronFields[index].low, scheduleCronFields[index].high)
77 return ScheduleCron{}, fmt.Errorf("the %s field of %q: %w", scheduleCronFields[index].name, expression, err)
89 anyDay: strings.HasPrefix(fields[2], "*"),
90 anyWeekday: strings.HasPrefix(fields[4], "*"),
91 description: strings.Join(fields, " "),
94 if c.weekdays&(1<<7) != 0 {
98 if c.gap() < scheduleCronGap {
99 return ScheduleCron{}, fmt.Errorf("the runs of %q can be less than %d minutes apart: the runs of a schedule must be at least %d minutes apart", expression, scheduleCronGap, scheduleCronGap)
102 start := time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC)
103 _, found := c.Next(start)
105 return ScheduleCron{}, fmt.Errorf("the cron expression %q matches no day in %d years", expression, scheduleCronYears)
111 // ScheduleCron is a parsed cron expression. Each set holds a bit for each value
112 // of its field. anyDay and anyWeekday tell that the day field or the weekday
113 // field starts with *. When neither does, a day matches when either field
114 // matches, as in classic cron. Otherwise both fields must match.
115 type ScheduleCron struct {
126 // String returns the expression with one space between its fields.
127 func (c ScheduleCron) String() string {
131 // Next returns the first time after after whose wall time in the location of
132 // after matches, or false when no time in scheduleCronYears years matches. A
133 // wall time goes through scheduleWallTime, so a wall time that the clock skips
134 // runs later by the length of the skip, and a wall time that the clock repeats
135 // runs once, at its first occurrence.
136 func (c ScheduleCron) Next(after time.Time) (time.Time, bool) {
137 location := after.Location()
138 year, month, day := after.Date()
139 first := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
140 last := first.AddDate(scheduleCronYears, 0, 0)
141 for date := first; date.Before(last); date = date.AddDate(0, 0, 1) {
146 // A wall time that the clock skips moves later, so the times of
147 // one day are not always in order. The first time is the
151 for hour := range 24 {
152 if c.hours&(1<<hour) == 0 {
156 for minute := range 60 {
157 if c.minutes&(1<<minute) == 0 {
161 t := scheduleWallTime(date.Year(), date.Month(), date.Day(), hour, minute, location)
162 if t.After(after) && (next.IsZero() || t.Before(next)) {
173 return time.Time{}, false
176 // day tells if the calendar date matches the month and the day fields.
177 func (c ScheduleCron) day(date time.Time) bool {
178 if c.months&(1<<int(date.Month())) == 0 {
182 day := c.days&(1<<date.Day()) != 0
183 weekday := c.weekdays&(1<<int(date.Weekday())) != 0
184 if c.anyDay || c.anyWeekday {
185 return day && weekday
188 return day || weekday
191 // gap returns the shortest time in minutes between two wall times of the hour
192 // and the minute fields, also across midnight.
193 func (c ScheduleCron) gap() int {
196 for hour := range 24 {
197 for minute := range 60 {
198 if c.hours&(1<<hour) != 0 && c.minutes&(1<<minute) != 0 {
199 times = append(times, 60*hour+minute)
204 gap := times[0] + 24*60 - times[len(times)-1]
205 for index := 1; index < len(times); index++ {
206 gap = min(gap, times[index]-times[index-1])
212 // scheduleWallTime returns the time of a wall time in location. A wall time
213 // that the clock skips, at the start of daylight saving time, is later by the
214 // length of the skip: 02:30 becomes 03:30 when the clock goes from 02:00 to
215 // 03:00. A wall time that the clock repeats, at the end of daylight saving
216 // time, is its first occurrence, as in cron.
217 func scheduleWallTime(year int, month time.Month, day, hour, minute int, location *time.Location) time.Time {
218 t := time.Date(year, month, day, hour, minute, 0, 0, location)
219 _, before := t.Add(-3 * time.Hour).Zone()
220 _, offset := t.Zone()
221 if t.Hour() != hour || t.Minute() != minute || t.Day() != day {
222 wall := time.Date(year, month, day, hour, minute, 0, 0, time.UTC)
224 return wall.Add(-time.Duration(before) * time.Second).In(location)
228 earlier := t.Add(-time.Duration(before-offset) * time.Second)
229 if earlier.Day() == day && earlier.Hour() == hour && earlier.Minute() == minute {
237 // scheduleCronField returns the set of the values of one field, from low to
239 func scheduleCronField(field string, low, high int) (uint64, error) {
242 for item := range strings.SplitSeq(field, ",") {
243 base, stepText, stepped := strings.Cut(item, "/")
248 step, err = scheduleCronNumber(stepText, 1, high)
250 return 0, fmt.Errorf("the step of %q: %w", item, err)
254 first, last := low, high
255 startText, endText, ranged := strings.Cut(base, "-")
261 first, err = scheduleCronNumber(startText, low, high)
263 last, err = scheduleCronNumber(endText, low, high)
266 if err == nil && first > last {
267 err = errors.New("the start is after the end")
271 return 0, fmt.Errorf("the range %q: %w", base, err)
274 return 0, fmt.Errorf("%q has a step after a single value: write a range N-M/S or */S", item)
278 first, err = scheduleCronNumber(base, low, high)
286 for value := first; value <= last; value += step {
294 // scheduleCronNumber returns the value of text, a decimal number from low to
296 func scheduleCronNumber(text string, low, high int) (int, error) {
297 if text == "" || strings.Trim(text, "0123456789") != "" {
298 return 0, fmt.Errorf("%q is not a number from %d to %d", text, low, high)
301 value, err := strconv.Atoi(text)
302 if err != nil || value < low || value > high {
303 return 0, fmt.Errorf("%q is not a number from %d to %d", text, low, high)