X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/03ef0cf2e8b23ae3954a89883bde4bb557b9ccee..c781f79f3c8cb9a7577ee1574d196988da2719b3:/schedule_cron.go diff --git a/schedule_cron.go b/schedule_cron.go index 78ec90b..3255f99 100644 --- a/schedule_cron.go +++ b/schedule_cron.go @@ -53,60 +53,6 @@ var scheduleCronFields = []struct { }, } -// scheduleParseCron parses a cron expression of five fields: minute, hour, day -// of month, month, and day of week, where 0 and 7 are Sunday. A field is a -// comma list of items, and an item is *, N, or N-M, where * and N-M can have a -// step /S. Names, macros, the characters ? L W #, and N/S are errors. -// -// It refuses an expression whose runs can be less than scheduleCronGap minutes -// apart. The check takes the wall times of one day from the hour and the minute -// fields, and the gap across midnight as if the next day matches too. It also -// refuses an expression that matches no day in scheduleCronYears years, such as -// one on February 30. -func scheduleParseCron(expression string) (ScheduleCron, error) { - fields := strings.Fields(expression) - if len(fields) != len(scheduleCronFields) { - 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)) - } - - var sets [5]uint64 - for index, field := range fields { - set, err := scheduleCronField(field, scheduleCronFields[index].low, scheduleCronFields[index].high) - if err != nil { - return ScheduleCron{}, fmt.Errorf("the %s field of %q: %w", scheduleCronFields[index].name, expression, err) - } - - sets[index] = set - } - - c := ScheduleCron{ - minutes: sets[0], - hours: sets[1], - days: sets[2], - months: sets[3], - weekdays: sets[4], - anyDay: strings.HasPrefix(fields[2], "*"), - anyWeekday: strings.HasPrefix(fields[4], "*"), - description: strings.Join(fields, " "), - } - - if c.weekdays&(1<<7) != 0 { - c.weekdays |= 1 - } - - if c.gap() < scheduleCronGap { - 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) - } - - start := time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC) - _, found := c.Next(start) - if !found { - return ScheduleCron{}, fmt.Errorf("the cron expression %q matches no day in %d years", expression, scheduleCronYears) - } - - return c, nil -} - // ScheduleCron is a parsed cron expression. Each set holds a bit for each value // of its field. anyDay and anyWeekday tell that the day field or the weekday // field starts with *. When neither does, a day matches when either field @@ -146,6 +92,7 @@ func (c ScheduleCron) Next(after time.Time) (time.Time, bool) { // one day are not always in order. The first time is the // smallest one. var next time.Time + for hour := range 24 { if c.hours&(1<