cronparse

package module
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2022 License: MIT Imports: 7 Imported by: 0

README

cronparse

Cron expression parser for Amazon EventBridge.

Installation

go get github.com/winebarrel/cronparse@v1.5.0

Usage

package main

import (
	"fmt"

	"github.com/winebarrel/cronparse"
)

func main() {
	cron, err := cronparse.Parse("0 10 * * ? *")

	if err != nil {
		panic(err)
	}

	fmt.Println(cron.Minutes.Exps[0].Number.Value) //=> 0
	fmt.Println(cron.Hours.Exps[0].Number.Value)   //=> 10
	fmt.Println(cron.String())                     //=> "0 10 * * ? *"

	fmt.Println(cron.Match(time.Date(2022, 11, 3, 9, 0, 0, 0, time.UTC)))
	// => false
	fmt.Println(cron.Match(time.Date(2022, 11, 3, 10, 0, 0, 0, time.UTC)))
	// => true

	fmt.Println(cron.Next(time.Date(2022, 11, 3, 10, 0, 0, 0, time.UTC)))
	//=> 2022-11-03 10:00:00 +0000 UTC
	fmt.Println(cron.Next(time.Date(2022, 11, 3, 11, 0, 0, 0, time.UTC)))
	//=> 2022-11-04 10:00:00 +0000 UTC
	fmt.Println(cron.NextN(time.Date(2022, 11, 3, 10, 0, 0, 0, time.UTC), 3))
	//=> [2022-11-03 10:00:00 +0000 UTC 2022-11-04 10:00:00 +0000 UTC 2022-11-05 10:00:00 +0000 UTC]
}

cronplan

CLI to show next triggers.

Installation

brew install winebarrel/cronplan/cronplan

Usage

Usage: cronplan [OPTION] CRON_EXPR
  -h int
    	hour to add
  -n int
    	number of next triggers (default 10)
  -version
    	print version and exit
$ cronplan "*/10 10 ? * MON-FRI *"
Tue, 11 Oct 2022 10:00:00
Tue, 11 Oct 2022 10:10:00
Tue, 11 Oct 2022 10:20:00
Tue, 11 Oct 2022 10:30:00
Tue, 11 Oct 2022 10:40:00
Tue, 11 Oct 2022 10:50:00
Wed, 12 Oct 2022 10:00:00
Wed, 12 Oct 2022 10:10:00
Wed, 12 Oct 2022 10:20:00
Wed, 12 Oct 2022 10:30:00

$ cronplan -h -9 "*/10 10 ? * MON-FRI *"
Tue, 11 Oct 2022 01:00:00
Tue, 11 Oct 2022 01:10:00
Tue, 11 Oct 2022 01:20:00
Tue, 11 Oct 2022 01:30:00
Tue, 11 Oct 2022 01:40:00
Tue, 11 Oct 2022 01:50:00
Wed, 12 Oct 2022 01:00:00
Wed, 12 Oct 2022 01:10:00
Wed, 12 Oct 2022 01:20:00
Wed, 12 Oct 2022 01:30:00

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Parser = participle.MustBuild[Expression](
		participle.Lexer(cronLexer),
	)
)

Functions

This section is empty.

Types

type All

type All struct {
	Value struct{} `"*"`
}

all

func (*All) Match

func (v *All) Match(x interface{}) bool

func (*All) String

func (v *All) String() string

type Any

type Any struct {
	Value struct{} `"?"`
}

any

func (*Any) Match

func (v *Any) Match(x interface{}) bool

func (*Any) String

func (v *Any) String() string

type CommonExp

type CommonExp struct {
	Increment   *Increment   `@@`
	NumberRange *NumberRange `| @@`
	Number      *Number      `| @@`
	All         *All         `| @@`
}

func (*CommonExp) Match

func (v *CommonExp) Match(x int, base int) bool

func (*CommonExp) Present

func (v *CommonExp) Present() bool

func (*CommonExp) String

func (v *CommonExp) String() string

type DayOfMonth

type DayOfMonth struct {
	Exps []*DayOfMonthExp `@@ ( "," @@ )*`
}

func (*DayOfMonth) HasAny

func (v *DayOfMonth) HasAny() bool

func (*DayOfMonth) Match

func (v *DayOfMonth) Match(t time.Time) bool

func (*DayOfMonth) String

func (v *DayOfMonth) String() string

type DayOfMonthExp

type DayOfMonthExp struct {
	Weekday *Weekday `@@ |`
	CommonExp
	Any  *Any         `| @@`
	Last *LastOfMonth `| @@`
}

day of month

func (*DayOfMonthExp) Match

func (v *DayOfMonthExp) Match(t time.Time) bool

func (*DayOfMonthExp) String

func (v *DayOfMonthExp) String() string

type DayOfWeek

type DayOfWeek struct {
	Exps []*DayOfWeekExp `@@ ( "," @@ )*`
}

func (*DayOfWeek) HasAny

func (v *DayOfWeek) HasAny() bool

func (*DayOfWeek) Match

func (v *DayOfWeek) Match(t time.Time) bool

func (*DayOfWeek) String

func (v *DayOfWeek) String() string

type DayOfWeekExp

type DayOfWeekExp struct {
	Instance *Instance `@@ |`
	CommonExp
	NameRange *WeekRange  `| @@`
	Name      *WeekName   `| @@`
	Any       *Any        `| @@`
	Last      *LastOfWeek `| @@`
}

day of week

func (*DayOfWeekExp) Match

func (v *DayOfWeekExp) Match(t time.Time) bool

func (*DayOfWeekExp) String

func (v *DayOfWeekExp) String() string

type Expression

type Expression struct {
	Minutes    *Minutes    `@@`
	Hours      *Hours      `SP @@`
	DayOfMonth *DayOfMonth `SP @@`
	Month      *Month      `SP @@`
	DayOfWeek  *DayOfWeek  `SP @@`
	Year       *Year       `SP @@`
}

func Parse

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

func (*Expression) Match

func (v *Expression) Match(t time.Time) bool

func (*Expression) Next

func (v *Expression) Next(from time.Time) time.Time

func (*Expression) NextN

func (v *Expression) NextN(from time.Time, n int) []time.Time

func (*Expression) String

func (v *Expression) String() string

type Hours

type Hours struct {
	Exps []*HoursExp `@@ ( "," @@ )*`
}

func (*Hours) Match

func (v *Hours) Match(t time.Time) bool

func (*Hours) String

func (v *Hours) String() string

type HoursExp

type HoursExp struct {
	CommonExp
}

hours

func (*HoursExp) Match

func (v *HoursExp) Match(t time.Time) bool

func (*HoursExp) String

func (v *HoursExp) String() string

type Increment

type Increment struct {
	Wildcard bool `( @"*"`
	Top      int  `| @Number )`
	Buttom   int  `"/" @Number`
}

increment

func (*Increment) Match

func (v *Increment) Match(x int, base int) bool

func (*Increment) String

func (v *Increment) String() string

type Instance

type Instance struct {
	DayOfWeek    int `@Number`
	NthDayOfWeek int `"#" @Number`
}

instance

func (*Instance) Match

func (v *Instance) Match(t time.Time) bool

func (*Instance) String

func (v *Instance) String() string

type LastOfMonth

type LastOfMonth struct {
	Value struct{} `"L"`
}

last of month

func (*LastOfMonth) Match

func (v *LastOfMonth) Match(t time.Time) bool

func (*LastOfMonth) String

func (v *LastOfMonth) String() string

type LastOfWeek

type LastOfWeek struct {
	Value struct{} `"L"`
}

last of week

func (*LastOfWeek) Match

func (v *LastOfWeek) Match(t time.Time) bool

func (*LastOfWeek) String

func (v *LastOfWeek) String() string

type Minutes

type Minutes struct {
	Exps []*MinutesExp `@@ ( "," @@ )*`
}

func (*Minutes) Match

func (v *Minutes) Match(t time.Time) bool

func (*Minutes) String

func (v *Minutes) String() string

type MinutesExp

type MinutesExp struct {
	CommonExp
}

minutes

func (*MinutesExp) Match

func (v *MinutesExp) Match(t time.Time) bool

func (*MinutesExp) String

func (v *MinutesExp) String() string

type Month

type Month struct {
	Exps []*MonthExp `@@ ( "," @@ )*`
}

func (*Month) Match

func (v *Month) Match(t time.Time) bool

func (*Month) String

func (v *Month) String() string

type MonthExp

type MonthExp struct {
	CommonExp
	NameRange *MonthRange `| @@`
	Name      *MonthName  `| @@`
	Any       *Any        `| @@`
}

month

func (*MonthExp) Match

func (v *MonthExp) Match(t time.Time) bool

func (*MonthExp) String

func (v *MonthExp) String() string

type MonthName

type MonthName struct {
	Value string `@Month`
}

month name

func (*MonthName) Match

func (v *MonthName) Match(x time.Month) bool

func (*MonthName) String

func (v *MonthName) String() string

type MonthRange

type MonthRange struct {
	From string `@Month`
	To   string `"-" @Month`
}

month range

func (*MonthRange) Match

func (v *MonthRange) Match(x time.Month) bool

func (*MonthRange) String

func (v *MonthRange) String() string

type Number

type Number struct {
	Value int `@Number`
}

number

func (*Number) Match

func (v *Number) Match(x int) bool

func (*Number) String

func (v *Number) String() string

type NumberRange

type NumberRange struct {
	From int `@Number`
	To   int `"-" @Number`
}

number range

func (*NumberRange) Match

func (v *NumberRange) Match(x int) bool

func (*NumberRange) String

func (v *NumberRange) String() string

type WeekName

type WeekName struct {
	Value string `@Week`
}

week name

func (*WeekName) Match

func (v *WeekName) Match(x time.Weekday) bool

func (*WeekName) String

func (v *WeekName) String() string

type WeekRange

type WeekRange struct {
	From string `@Week`
	To   string `"-" @Week`
}

week range

func (*WeekRange) Match

func (v *WeekRange) Match(x time.Weekday) bool

func (*WeekRange) String

func (v *WeekRange) String() string

type Weekday

type Weekday struct {
	Value int `@Number "W"`
}

weekday

func (*Weekday) Match

func (v *Weekday) Match(base time.Time) bool

func (*Weekday) String

func (v *Weekday) String() string

type Year

type Year struct {
	Exps []*YearExp `@@ ( "," @@ )*`
}

func (*Year) Match

func (v *Year) Match(t time.Time) bool

func (*Year) String

func (v *Year) String() string

type YearExp

type YearExp struct {
	CommonExp
}

year

func (*YearExp) Match

func (v *YearExp) Match(t time.Time) bool

func (*YearExp) String

func (v *YearExp) String() string

Directories

Path Synopsis
cmd
cronplan command

Jump to

Keyboard shortcuts

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