Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

cron.gno

// Package cron parses the classic five-field cron expression and answers the
// only question a schedule exists to answer: when does it next fire.
//
// The grammar is the one every crontab uses, in the order minute, hour,
// day-of-month, month, day-of-week:
//
//	*/15 9-17 * * mon-fri
//	│    │    │ │ │
//	│    │    │ │ └── day of week, 0-7 or sun..sat (0 and 7 are both Sunday)
//	│    │    │ └──── month, 1-12 or jan..dec
//	│    │    └────── day of month, 1-31
//	│    └─────────── hour, 0-23
//	└──────────────── minute, 0-59
//
// Each field is a comma-separated list whose items are a value, a range, a
// star, or any of those followed by a step: 5, 5-30, *, */15, 5-30/5, 5/20.
// The seven @macros are accepted too (@hourly, @daily, @weekly, @monthly,
// @yearly, @annually, @midnight).
//
// A Schedule is immutable once parsed and holds no state, so it is safe to
// build one at package scope and share it. Everything is computed in UTC: a
// block timestamp has no local zone, and a schedule whose answer depended on
// one would not be the same schedule on every validating node.
package cron

import (
	"errors"
	"strconv"
	"strings"
	"time"
)

// horizonYears bounds the search in Next. A cron expression can be perfectly
// well formed and still never fire (February the 30th), so the walk needs a
// stop condition that is not "a match eventually appears". Four years clears
// the longest real gap, the leap day, with a year to spare.
const horizonYears = 5

// Schedule is a parsed cron expression.
type Schedule struct {
	expr string

	minute [60]bool
	hour   [24]bool
	dom    [32]bool // 1..31
	month  [13]bool // 1..12
	dow    [7]bool  // 0..6, Sunday is 0

	// Whether each day field was narrowed from "*". The pair decides how the
	// two are combined; see matchDay.
	domRestricted bool
	dowRestricted bool
}

var macros = map[string]string{
	"@yearly":   "0 0 1 1 *",
	"@annually": "0 0 1 1 *",
	"@monthly":  "0 0 1 * *",
	"@weekly":   "0 0 * * 0",
	"@daily":    "0 0 * * *",
	"@midnight": "0 0 * * *",
	"@hourly":   "0 * * * *",
}

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 dayNames = map[string]int{
	"sun": 0, "mon": 1, "tue": 2, "wed": 3, "thu": 4, "fri": 5, "sat": 6,
}

// Parse compiles a cron expression.
//
// It validates the grammar and the ranges, and nothing else: an expression
// that is well formed but can never fire parses successfully, because only
// Next is in a position to discover that.
func Parse(expr string) (*Schedule, error) {
	raw := strings.TrimSpace(expr)
	if raw == "" {
		return nil, errors.New("cron: empty expression")
	}

	spec := raw
	if strings.HasPrefix(spec, "@") {
		expanded, ok := macros[strings.ToLower(spec)]
		if !ok {
			return nil, errors.New("cron: unknown macro " + strconv.Quote(raw))
		}
		spec = expanded
	}

	fields := strings.Fields(spec)
	if len(fields) != 5 {
		return nil, errors.New("cron: want 5 fields, got " + strconv.Itoa(len(fields)) +
			" in " + strconv.Quote(raw))
	}

	s := &Schedule{expr: raw}

	minute, err := parseField(fields[0], 0, 59, nil)
	if err != nil {
		return nil, fieldError("minute", fields[0], err)
	}
	copy(s.minute[:], minute)

	hour, err := parseField(fields[1], 0, 23, nil)
	if err != nil {
		return nil, fieldError("hour", fields[1], err)
	}
	copy(s.hour[:], hour)

	dom, err := parseField(fields[2], 1, 31, nil)
	if err != nil {
		return nil, fieldError("day of month", fields[2], err)
	}
	copy(s.dom[:], dom)
	s.domRestricted = fields[2] != "*"

	month, err := parseField(fields[3], 1, 12, monthNames)
	if err != nil {
		return nil, fieldError("month", fields[3], err)
	}
	copy(s.month[:], month)

	// Day of week is parsed over 0..7 so that 7 and 5-7 stay legal, then
	// folded: Sunday has two spellings and one slot.
	dow, err := parseField(fields[4], 0, 7, dayNames)
	if err != nil {
		return nil, fieldError("day of week", fields[4], err)
	}
	if dow[7] {
		dow[0] = true
	}
	copy(s.dow[:], dow[:7])
	s.dowRestricted = fields[4] != "*"

	return s, nil
}

// MustParse is Parse for an expression written by the programmer rather than
// supplied by a caller. It panics instead of returning an error, so a constant
// schedule can be a package-level variable.
func MustParse(expr string) *Schedule {
	s, err := Parse(expr)
	if err != nil {
		panic(err)
	}
	return s
}

// Expr returns the expression this schedule was parsed from, unmodified. A
// macro is reported as the macro, not as its expansion.
func (s *Schedule) Expr() string { return s.expr }

// Matches reports whether the schedule fires during the minute containing t.
// Seconds and smaller are ignored: cron has no sub-minute resolution.
func (s *Schedule) Matches(t time.Time) bool {
	u := t.UTC()
	return s.month[int(u.Month())] &&
		s.matchDay(u) &&
		s.hour[u.Hour()] &&
		s.minute[u.Minute()]
}

// Next returns the first instant strictly after `after` at which the schedule
// fires, and whether there is one.
//
// The second return value is not decoration. "0 0 30 2 *" is a legal
// expression with no instant behind it, and a caller that assumed a match
// would scan forever.
func (s *Schedule) Next(after time.Time) (time.Time, bool) {
	// Strictly after, and aligned to the minute: a schedule handed its own
	// firing time must answer with the following one, not with itself.
	t := after.UTC().Truncate(time.Minute).Add(time.Minute)
	limit := t.Year() + horizonYears

	for t.Year() <= limit {
		switch {
		case !s.month[int(t.Month())]:
			// Skipping to the first of the next month relies on time.Date
			// normalising month 13 into January of the following year.
			t = time.Date(t.Year(), t.Month()+1, 1, 0, 0, 0, 0, time.UTC)
		case !s.matchDay(t):
			t = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, time.UTC)
		case !s.hour[t.Hour()]:
			t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+1, 0, 0, 0, time.UTC)
		case !s.minute[t.Minute()]:
			t = t.Add(time.Minute)
		default:
			return t, true
		}
	}
	return time.Time{}, false
}

// NextN returns the next n firing times, in order. It returns fewer, possibly
// none, when the schedule runs out before n are found.
func (s *Schedule) NextN(after time.Time, n int) []time.Time {
	if n <= 0 {
		return nil
	}
	out := make([]time.Time, 0, n)
	t := after
	for i := 0; i < n; i++ {
		next, ok := s.Next(t)
		if !ok {
			break
		}
		out = append(out, next)
		t = next
	}
	return out
}

// matchDay combines the two day fields.
//
// This is the rule the whole package is written for, and it is the one every
// reimplementation gets wrong. When BOTH day fields are narrowed the match is
// their union, not their intersection: "0 0 13 * 5" is the 13th of the month
// OR any Friday, which is why a Friday the 13th crontab is written that way
// and still fires on ordinary Fridays. When only one is narrowed the other
// matches everything, so the union and the intersection coincide and there is
// nothing to decide.
func (s *Schedule) matchDay(t time.Time) bool {
	dom := s.dom[t.Day()]
	dow := s.dow[int(t.Weekday())]
	switch {
	case s.domRestricted && s.dowRestricted:
		return dom || dow
	case s.domRestricted:
		return dom
	case s.dowRestricted:
		return dow
	default:
		return true
	}
}

// parseField expands one comma-separated field into a bitmap of size max+1.
func parseField(field string, min, max int, names map[string]int) ([]bool, error) {
	bits := make([]bool, max+1)
	for _, part := range strings.Split(field, ",") {
		if part == "" {
			return nil, errors.New("empty list item")
		}

		rangePart := part
		step := 1
		if i := strings.Index(part, "/"); i >= 0 {
			rangePart = part[:i]
			n, err := strconv.Atoi(part[i+1:])
			if err != nil {
				return nil, errors.New("step is not a number: " + strconv.Quote(part[i+1:]))
			}
			if n < 1 {
				return nil, errors.New("step must be at least 1, got " + strconv.Itoa(n))
			}
			step = n
		}

		lo, hi, err := parseRange(rangePart, min, max, names, step)
		if err != nil {
			return nil, err
		}
		for v := lo; v <= hi; v += step {
			bits[v] = true
		}
	}
	return bits, nil
}

// parseRange resolves the pre-step half of a list item into inclusive bounds.
func parseRange(s string, min, max int, names map[string]int, step int) (int, int, error) {
	if s == "*" {
		return min, max, nil
	}
	if i := strings.Index(s, "-"); i > 0 {
		lo, err := parseValue(s[:i], min, max, names)
		if err != nil {
			return 0, 0, err
		}
		hi, err := parseValue(s[i+1:], min, max, names)
		if err != nil {
			return 0, 0, err
		}
		if lo > hi {
			return 0, 0, errors.New("range runs backwards: " + strconv.Quote(s))
		}
		return lo, hi, nil
	}
	v, err := parseValue(s, min, max, names)
	if err != nil {
		return 0, 0, err
	}
	if step > 1 {
		// "5/20" is shorthand for "5-max/20": a bare value with a step means
		// start here and keep going, not fire once.
		return v, max, nil
	}
	return v, v, nil
}

// parseValue resolves a single number or three-letter name.
func parseValue(s string, min, max int, names map[string]int) (int, error) {
	if names != nil {
		if v, ok := names[strings.ToLower(s)]; ok {
			return v, nil
		}
	}
	v, err := strconv.Atoi(s)
	if err != nil {
		return 0, errors.New("not a number or a known name: " + strconv.Quote(s))
	}
	if v < min || v > max {
		return 0, errors.New("out of range " + strconv.Itoa(min) + "-" + strconv.Itoa(max) +
			": " + strconv.Itoa(v))
	}
	return v, nil
}

// fieldError names the field a parse failure came from. A bare "out of range"
// is useless when five fields all accept numbers.
func fieldError(name, field string, err error) error {
	return errors.New("cron: " + name + " field " + strconv.Quote(field) + ": " + err.Error())
}

cron_test.gno

package cron

import (
	"testing"
	"time"
)

// at is shorthand for a UTC instant, so the tables below read as dates.
func at(y int, mo time.Month, d, h, mi int) time.Time {
	return time.Date(y, mo, d, h, mi, 0, 0, time.UTC)
}

func TestParseRejects(t *testing.T) {
	cases := []struct {
		name string
		expr string
	}{
		{"empty", ""},
		{"blank", "   "},
		{"four fields", "* * * *"},
		{"six fields", "* * * * * *"},
		{"minute out of range", "60 * * * *"},
		{"hour out of range", "* 24 * * *"},
		{"day zero", "* * 0 * *"},
		{"month thirteen", "* * * 13 *"},
		{"weekday eight", "* * * * 8"},
		{"backwards range", "* * * * 5-1"},
		{"zero step", "*/0 * * * *"},
		{"negative step", "*/-2 * * * *"},
		{"empty list item", "1,,2 * * * *"},
		{"not a number", "abc * * * *"},
		{"unknown name", "* * * smarch *"},
		{"unknown macro", "@fortnightly"},
		{"macro with fields", "@daily extra"},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			if _, err := Parse(tc.expr); err == nil {
				t.Fatalf("Parse(%q): want error, got nil", tc.expr)
			}
		})
	}
}

func TestNext(t *testing.T) {
	base := at(2026, time.March, 10, 9, 17)
	cases := []struct {
		name string
		expr string
		from time.Time
		want time.Time
	}{
		{"every minute", "* * * * *", base, at(2026, time.March, 10, 9, 18)},
		{"quarter hour", "*/15 * * * *", base, at(2026, time.March, 10, 9, 30)},
		{"top of hour", "0 * * * *", base, at(2026, time.March, 10, 10, 0)},
		{"list", "5,25,45 * * * *", base, at(2026, time.March, 10, 9, 25)},
		{"step from value", "5/20 * * * *", base, at(2026, time.March, 10, 9, 25)},
		{"range with step", "0-30/10 * * * *", base, at(2026, time.March, 10, 9, 20)},
		{"workday morning", "30 8 * * 1-5", base, at(2026, time.March, 11, 8, 30)},
		{"weekend only", "0 12 * * sat,sun", base, at(2026, time.March, 14, 12, 0)},
		{"month list", "0 0 1 jan,jul *", base, at(2026, time.July, 1, 0, 0)},
		{"end of month", "0 0 31 * *", base, at(2026, time.March, 31, 0, 0)},
		{"leap day", "0 0 29 2 *", base, at(2028, time.February, 29, 0, 0)},
		{"next minute is exclusive", "17 9 * * *", base, at(2026, time.March, 11, 9, 17)},
		{"seconds are discarded", "* * * * *", base.Add(42 * time.Second), at(2026, time.March, 10, 9, 18)},
		{"year rolls over", "0 0 1 1 *", base, at(2027, time.January, 1, 0, 0)},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			s, err := Parse(tc.expr)
			if err != nil {
				t.Fatalf("Parse(%q): %v", tc.expr, err)
			}
			got, ok := s.Next(tc.from)
			if !ok {
				t.Fatalf("Next(%q): no match within the horizon", tc.expr)
			}
			if !got.Equal(tc.want) {
				t.Errorf("Next(%q) from %v = %v, want %v", tc.expr, tc.from, got, tc.want)
			}
		})
	}
}

// TestDayOrDay pins the one rule a cron parser is actually written for: when
// both day fields are restricted the match is a UNION, not an intersection.
func TestDayOrDay(t *testing.T) {
	// The 13th of March 2026 is a Friday, so a naive AND would hide the bug.
	// April 2026 separates them: Friday the 3rd comes before the 13th.
	s, err := Parse("0 0 13 * 5")
	if err != nil {
		t.Fatal(err)
	}
	got, ok := s.Next(at(2026, time.March, 20, 0, 0))
	if !ok {
		t.Fatal("no match")
	}
	if want := at(2026, time.March, 27, 0, 0); !got.Equal(want) {
		t.Errorf("first = %v, want %v (a Friday, not the 13th)", got, want)
	}
	got, ok = s.Next(at(2026, time.April, 8, 0, 0))
	if !ok {
		t.Fatal("no match")
	}
	if want := at(2026, time.April, 10, 0, 0); !got.Equal(want) {
		t.Errorf("second = %v, want %v", got, want)
	}
}

func TestDayFieldRestriction(t *testing.T) {
	cases := []struct {
		name string
		expr string
		from time.Time
		want time.Time
	}{
		// Day-of-week alone: every day-of-month qualifies, so it is an AND
		// with a field that matches everything.
		{"dow only", "0 0 * * 3", at(2026, time.March, 10, 0, 0), at(2026, time.March, 11, 0, 0)},
		// Day-of-month alone, same reasoning mirrored.
		{"dom only", "0 0 15 * *", at(2026, time.March, 10, 0, 0), at(2026, time.March, 15, 0, 0)},
		// Neither restricted: the next midnight.
		{"neither", "0 0 * * *", at(2026, time.March, 10, 0, 0), at(2026, time.March, 11, 0, 0)},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			s, err := Parse(tc.expr)
			if err != nil {
				t.Fatal(err)
			}
			got, ok := s.Next(tc.from)
			if !ok {
				t.Fatal("no match")
			}
			if !got.Equal(tc.want) {
				t.Errorf("Next(%q) = %v, want %v", tc.expr, got, tc.want)
			}
		})
	}
}

// TestSundayIsSevenToo pins the historical alias: 0 and 7 are both Sunday, and
// 5-7 therefore has to mean Friday through Sunday rather than running backwards.
func TestSundayIsSevenToo(t *testing.T) {
	from := at(2026, time.March, 10, 0, 0) // a Tuesday
	zero, err := Parse("0 0 * * 0")
	if err != nil {
		t.Fatal(err)
	}
	seven, err := Parse("0 0 * * 7")
	if err != nil {
		t.Fatal(err)
	}
	a, _ := zero.Next(from)
	b, _ := seven.Next(from)
	if !a.Equal(b) {
		t.Errorf("dow 0 gave %v, dow 7 gave %v", a, b)
	}
	if want := at(2026, time.March, 15, 0, 0); !a.Equal(want) {
		t.Errorf("Sunday = %v, want %v", a, want)
	}

	wrap, err := Parse("0 0 * * 5-7")
	if err != nil {
		t.Fatalf("5-7 must span Friday to Sunday: %v", err)
	}
	for _, d := range []int{13, 14, 15} {
		if !wrap.Matches(at(2026, time.March, d, 0, 0)) {
			t.Errorf("5-7 should match March %d", d)
		}
	}
	if wrap.Matches(at(2026, time.March, 16, 0, 0)) {
		t.Error("5-7 should not match Monday")
	}
}

func TestMacros(t *testing.T) {
	cases := []struct {
		macro string
		same  string
	}{
		{"@yearly", "0 0 1 1 *"},
		{"@annually", "0 0 1 1 *"},
		{"@monthly", "0 0 1 * *"},
		{"@weekly", "0 0 * * 0"},
		{"@daily", "0 0 * * *"},
		{"@midnight", "0 0 * * *"},
		{"@hourly", "0 * * * *"},
	}
	from := at(2026, time.March, 10, 9, 17)
	for _, tc := range cases {
		t.Run(tc.macro, func(t *testing.T) {
			m, err := Parse(tc.macro)
			if err != nil {
				t.Fatal(err)
			}
			plain, err := Parse(tc.same)
			if err != nil {
				t.Fatal(err)
			}
			a, _ := m.Next(from)
			b, _ := plain.Next(from)
			if !a.Equal(b) {
				t.Errorf("%s = %v, %s = %v", tc.macro, a, tc.same, b)
			}
			if m.Expr() != tc.macro {
				t.Errorf("Expr() = %q, want the original %q", m.Expr(), tc.macro)
			}
		})
	}
}

// TestImpossible: a schedule with no instant at all must say so rather than
// loop. February never has a 30th.
func TestImpossible(t *testing.T) {
	s, err := Parse("0 0 30 2 *")
	if err != nil {
		t.Fatalf("Parse must accept it, only Next can know: %v", err)
	}
	if got, ok := s.Next(at(2026, time.March, 10, 0, 0)); ok {
		t.Errorf("want no match, got %v", got)
	}
}

func TestMatches(t *testing.T) {
	s, err := Parse("30 8 * * 1-5")
	if err != nil {
		t.Fatal(err)
	}
	cases := []struct {
		name string
		at   time.Time
		want bool
	}{
		{"wednesday 8:30", at(2026, time.March, 11, 8, 30), true},
		{"wednesday 8:31", at(2026, time.March, 11, 8, 31), false},
		{"saturday 8:30", at(2026, time.March, 14, 8, 30), false},
		{"seconds ignored", at(2026, time.March, 11, 8, 30).Add(59 * time.Second), true},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			if got := s.Matches(tc.at); got != tc.want {
				t.Errorf("Matches(%v) = %v, want %v", tc.at, got, tc.want)
			}
		})
	}
}

func TestNextN(t *testing.T) {
	s, err := Parse("0 */6 * * *")
	if err != nil {
		t.Fatal(err)
	}
	got := s.NextN(at(2026, time.March, 10, 9, 17), 4)
	want := []time.Time{
		at(2026, time.March, 10, 12, 0),
		at(2026, time.March, 10, 18, 0),
		at(2026, time.March, 11, 0, 0),
		at(2026, time.March, 11, 6, 0),
	}
	if len(got) != len(want) {
		t.Fatalf("got %d times, want %d", len(got), len(want))
	}
	for i := range want {
		if !got[i].Equal(want[i]) {
			t.Errorf("[%d] = %v, want %v", i, got[i], want[i])
		}
	}
	if n := len(s.NextN(at(2026, time.March, 10, 9, 17), 0)); n != 0 {
		t.Errorf("NextN(_, 0) returned %d times, want none", n)
	}
	impossible, err := Parse("0 0 30 2 *")
	if err != nil {
		t.Fatal(err)
	}
	if n := len(impossible.NextN(at(2026, time.March, 10, 0, 0), 3)); n != 0 {
		t.Errorf("an impossible schedule yielded %d times", n)
	}
}

func TestMustParsePanics(t *testing.T) {
	defer func() {
		if recover() == nil {
			t.Error("MustParse must panic on a bad expression")
		}
	}()
	MustParse("nonsense")
}

gnomod.toml

module = "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/cron/v0"
gno = "0.9"

[addpkg]
  creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv"
  height = 751591
  max_deposit = "9000000ugnot"