package fleet import ( "fmt" "strconv" "strings" "time" _ "time/tzdata" // fuseaux horaires embarques : TZ=Europe/Paris fonctionne sans tzdata dans l'image ) // Cron : planification au format crontab (5 champs : minute heure jour mois // jour-de-semaine), evaluee dans le fuseau du conteneur (variable TZ). // Acceptes : *, listes (1,15), intervalles (1-5), pas (*/15, 0-30/10), noms // anglais (jan..dec, sun..sat) et raccourcis @hourly, @daily, @weekly, // @monthly, @yearly. Comme cron, si le jour du mois ET le jour de semaine sont // restreints, l'un ou l'autre suffit. type Cron struct { min, hour, dom, month, dow uint64 // bits autorises domStar, dowStar bool } var cronMacros = map[string]string{ "@hourly": "0 * * * *", "@daily": "0 0 * * *", "@midnight": "0 0 * * *", "@weekly": "0 0 * * 0", "@monthly": "0 0 1 * *", "@yearly": "0 0 1 1 *", "@annually": "0 0 1 1 *", } var monthNames = map[string]int{"jan": 1, "feb": 2, "mar": 3, "apr": 4, "may": 5, "jun": 6, "jul": 7, "aug": 8, "sep": 9, "oct": 10, "nov": 11, "dec": 12} var dowNames = map[string]int{"sun": 0, "mon": 1, "tue": 2, "wed": 3, "thu": 4, "fri": 5, "sat": 6} // ParseCron analyse une expression crontab. func ParseCron(expr string) (*Cron, error) { expr = strings.TrimSpace(expr) if m, ok := cronMacros[strings.ToLower(expr)]; ok { expr = m } f := strings.Fields(expr) if len(f) != 5 { return nil, fmt.Errorf("%w : planification %q (5 champs attendus : minute heure jour mois jour-semaine)", ErrInvalid, expr) } c := &Cron{} var err error if c.min, _, err = cronField(f[0], 0, 59, nil); err != nil { return nil, err } if c.hour, _, err = cronField(f[1], 0, 23, nil); err != nil { return nil, err } if c.dom, c.domStar, err = cronField(f[2], 1, 31, nil); err != nil { return nil, err } if c.month, _, err = cronField(f[3], 1, 12, monthNames); err != nil { return nil, err } if c.dow, c.dowStar, err = cronField(f[4], 0, 7, dowNames); err != nil { return nil, err } if c.dow&(1<<7) != 0 { // 7 = dimanche c.dow |= 1 } return c, nil } func cronField(s string, lo, hi int, names map[string]int) (bits uint64, star bool, err error) { bad := func() (uint64, bool, error) { return 0, false, fmt.Errorf("%w : champ cron %q (valeurs %d a %d)", ErrInvalid, s, lo, hi) } num := func(v string) (int, bool) { if n, ok := names[strings.ToLower(v)]; ok { return n, true } n, e := strconv.Atoi(v) return n, e == nil && n >= lo && n <= hi } star = s == "*" || strings.HasPrefix(s, "*/") for _, part := range strings.Split(s, ",") { rng, step := part, 1 if i := strings.IndexByte(part, '/'); i >= 0 { n, e := strconv.Atoi(part[i+1:]) if e != nil || n < 1 || n > hi { return bad() } rng, step = part[:i], n } var a, b int switch { case rng == "*": a, b = lo, hi case strings.Contains(rng, "-"): x, y, _ := strings.Cut(rng, "-") var ok1, ok2 bool if a, ok1 = num(x); !ok1 { return bad() } if b, ok2 = num(y); !ok2 || b < a { return bad() } default: var ok bool if a, ok = num(rng); !ok { return bad() } b = a if step > 1 { // "5/15" : de 5 jusqu'a la fin b = hi } } for v := a; v <= b; v += step { bits |= 1 << uint(v) } } if bits == 0 { return bad() } return bits, star, nil } func (c *Cron) dayOK(t time.Time) bool { d := c.dom&(1<