cron

package
v12.41.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package cron provides methods for working with cron expressions

Index

Examples

Constants

View Source
const (
	YEARLY   = "0 0 1 1 *"
	ANNUALLY = "0 0 1 1 *"
	MONTHLY  = "0 0 1 * *"
	WEEKLY   = "0 0 * * 0"
	DAILY    = "0 0 * * *"
	HOURLY   = "0 * * * *"
)

Aliases

Variables

View Source
var (
	// ErrMalformedExpression is returned by the Parse method if given cron expression has
	// wrong or unsupported format
	ErrMalformedExpression = errors.New("Expression must have 5 tokens")

	// ErrZeroInterval is returned if interval part of expression is empty
	ErrZeroInterval = errors.New("Interval can't be less or equals 0")
)

Functions

This section is empty.

Types

type Expr

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

Expr cron expression struct

func Parse

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

Parse parse cron expression https://en.wikipedia.org/wiki/Cron

Example
expr, err := Parse("0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri")

if err != nil {
	return
}

m1 := time.Date(2020, 1, 1, 18, 15, 0, 0, time.Local)
m2 := time.Date(2020, 1, 1, 18, 20, 0, 0, time.Local)

fmt.Printf("Execute1: %t\n", expr.IsDue(m1))
fmt.Printf("Execute2: %t\n", expr.IsDue(m2))
Output:

Execute1: true
Execute2: false

func (*Expr) IsDue

func (expr *Expr) IsDue(args ...time.Time) bool

IsDue check if current moment is match for expression

func (*Expr) Next

func (expr *Expr) Next(args ...time.Time) time.Time

Next get time of next matched moment

Example
expr, err := Parse("0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri")

if err != nil {
	return
}

m := time.Date(2020, 1, 1, 18, 15, 0, 0, time.UTC)

fmt.Printf("%v\n", expr.Next(m))
Output:

2020-01-01 18:30:00 +0000 UTC

func (*Expr) Prev

func (expr *Expr) Prev(args ...time.Time) time.Time

Prev get time of prev matched moment

Example
expr, err := Parse("0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri")

if err != nil {
	return
}

m := time.Date(2020, 1, 1, 18, 15, 0, 0, time.UTC)

fmt.Printf("%v\n", expr.Prev(m))
Output:

2020-01-01 18:00:00 +0000 UTC

func (*Expr) String

func (expr *Expr) String() string

String return raw expression

Example
expr, err := Parse("0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri")

if err != nil {
	return
}

fmt.Printf("%s\n", expr.String())
Output:

0,15,30,45 0,6,12,18 1-10,15,31 * Mon-Fri

Jump to

Keyboard shortcuts

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