cronexpr

package
v0.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package cronexpr parses standard 5-field cron expressions ("minute hour day-of-month month day-of-week") and computes the next matching time, in UTC or in a given time zone. It only evaluates schedules; it does not run anything.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Due added in v0.9.1

func Due(next, now time.Time) bool

Due reports whether a schedule's next run has arrived; an unknown (zero) next run is never due, so a schedule that cannot fire cannot hot loop.

func NextInLocation

func NextInLocation(schedule *Schedule, after time.Time, loc *time.Location) time.Time

NextInLocation returns the next time schedule matches strictly after after, evaluated on loc's wall clock, or the zero time when there is none. In a repeated DST hour it picks the first instant after "after"; a wall time inside a spring-forward gap runs once, shifted past the gap.

Example
package main

import (
	"fmt"
	"time"

	"github.com/GLINCKER/levelrail/kit/cronexpr"
)

func main() {
	s, err := cronexpr.Parse("30 9 * * 1-5")
	if err != nil {
		panic(err)
	}
	loc := time.FixedZone("UTC+2", 2*60*60)
	after := time.Date(2026, 1, 9, 12, 0, 0, 0, time.UTC)
	fmt.Println(cronexpr.NextInLocation(s, after, loc).Format(time.RFC3339))
}
Output:
2026-01-12T09:30:00+02:00

func Validate added in v0.9.1

func Validate(expr string, loc *time.Location, now time.Time) error

Validate parses expr and fails when it has no occurrence from now within the search horizon, so an impossible date is rejected at save time.

Types

type Schedule

type Schedule struct {
	// contains filtered or unexported fields
}

Schedule is a parsed, immutable 5-field cron expression, safe for concurrent use by multiple goroutines (every method only reads its own fields, computed once at Parse time).

func Parse

func Parse(expr string) (*Schedule, error)

Parse parses a standard 5-field cron expression. Fields are separated by whitespace; each field is one of "*", a single integer, an "a-b" range, or any of those with a "/step" suffix (e.g. "*/15", "10-20/5"), optionally comma-separating multiple such terms in one field (e.g. "1,15,30"). day-of-week accepts 7 as an alias for 0 (Sunday), the common convention several real cron implementations also accept, though this parser does not accept month or day names (JAN, MON, ...): app.yaml's own schedule field is documented purely as a cron expression, and every worked example in this codebase uses numeric fields only, so name support would be untested surface with no current caller.

func (*Schedule) Next

func (s *Schedule) Next(t time.Time) time.Time

Next returns the earliest minute-aligned UTC time strictly after t that this Schedule matches, or the zero time.Time if no match exists within the search horizon. Cron fields have no timezone of their own, so t is converted to UTC here; NextInLocation handles other zones.

Example
package main

import (
	"fmt"
	"time"

	"github.com/GLINCKER/levelrail/kit/cronexpr"
)

func main() {
	s, err := cronexpr.Parse("0 3 * * *")
	if err != nil {
		panic(err)
	}
	after := time.Date(2026, 1, 10, 12, 0, 0, 0, time.UTC)
	fmt.Println(s.Next(after).Format(time.RFC3339))
}
Output:
2026-01-11T03:00:00Z

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL