cron

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cron provides cron expression parsing and next-fire-time calculation. It implements the standard 5-field cron syntax plus optional seconds and descriptors.

Supported Syntax

Standard 5-field: minute hour day-of-month month day-of-week Optional 6-field: second minute hour day-of-month month day-of-week

  • * matches any value
  • 5 literal value
  • 1,3,5 comma-separated list
  • 1-5 range
  • */2 step (every 2 units)
  • 1-10/3 range with step
  • 5L last weekday of month (e.g. "5L" = last Friday)
  • 15W nearest weekday to day 15

Quick start

expr, err := cron.Parse("*/5 * * * *")
next := expr.Next(time.Now())

// 6-field with seconds
expr, err := cron.Parse("0 */30 * * * *")

// Descriptors
expr, err := cron.Parse("@hourly")
expr, err := cron.Parse("@every 10s")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Validate

func Validate(expr string) error

Validate checks if a cron expression is valid without fully parsing it.

Types

type Expression

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

Expression represents a parsed cron expression.

func MustParse

func MustParse(expr string) *Expression

MustParse is like Parse but panics on error.

func Parse

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

Parse parses a cron expression string. Supports 5-field, 6-field, and descriptor (@hourly, @every, etc.) syntax.

func (*Expression) EveryDuration

func (e *Expression) EveryDuration() time.Duration

EveryDuration returns the duration for @every expressions. Returns 0 for non-@every expressions.

func (*Expression) HasSeconds

func (e *Expression) HasSeconds() bool

HasSeconds returns true if the expression includes a seconds field.

func (*Expression) IsEvery

func (e *Expression) IsEvery() bool

IsEvery returns true if the expression is an @every descriptor.

func (*Expression) Next

func (e *Expression) Next(t time.Time) (time.Time, error)

Next returns the next time after t that the expression should fire. If t is zero, it uses time.Now(). Returns an error if no future fire time is found within 5 years.

func (*Expression) String

func (e *Expression) String() string

String returns the original expression text.

type Schedule

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

Schedule represents a sequence of fire times.

func NewSchedule

func NewSchedule(expr *Expression) *Schedule

NewSchedule creates a schedule from a parsed expression.

func (*Schedule) Next

func (s *Schedule) Next() (time.Time, error)

Next returns the next fire time after the last call to Next. The first call returns the next fire time after time.Now().

func (*Schedule) Reset

func (s *Schedule) Reset()

Reset resets the schedule so the next call to Next uses time.Now().

Jump to

Keyboard shortcuts

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