cronplan

package module
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2023 License: MIT Imports: 8 Imported by: 3

README

cronplan

test Go Reference Go Report Card

Overview

Cron expression parser for Amazon EventBridge.

Try with curl
$ curl cronplan.in -d '5 0 10 * ? *'
Tue, 10 Oct 2023 00:05:00
Fri, 10 Nov 2023 00:05:00
Sun, 10 Dec 2023 00:05:00
Wed, 10 Jan 2024 00:05:00
Sat, 10 Feb 2024 00:05:00
Sun, 10 Mar 2024 00:05:00
Wed, 10 Apr 2024 00:05:00
Fri, 10 May 2024 00:05:00
Mon, 10 Jun 2024 00:05:00
Wed, 10 Jul 2024 00:05:00

Installation

go get github.com/winebarrel/cronplan

Usage

package main

import (
	"fmt"
	"time"

	"github.com/winebarrel/cronplan"
)

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

	if err != nil {
		panic(err)
	}

	fmt.Println(cron.Minute.Exps[0].Number) //=> 0
	fmt.Println(cron.Hour.Exps[0].Number)   //=> 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]

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

About the behavior of "L" in day-of-week

If you specify L for day-of-week, the last day of the week of each month is usually matched.

# cron(0 0 ? * 6L *)
Fri, 27 Oct 2023 00:00:00
Fri, 24 Nov 2023 00:00:00
Fri, 29 Dec 2023 00:00:00
Fri, 26 Jan 2024 00:00:00
Fri, 23 Feb 2024 00:00:00

However, if you do not specify the day of the week before "L", the behavior will be the same as when you specify "SAT".

# cron(0 0 ? * L *) = cron(0 0 ? * SAT *)
Sat, 07 Oct 2023 00:00:00
Sat, 14 Oct 2023 00:00:00
Sat, 21 Oct 2023 00:00:00
Sat, 28 Oct 2023 00:00:00
Sat, 04 Nov 2023 00:00:00

cronplan CLI

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

cronmatch CLI

CLI to check if datetime matches cron expression.

Installation

brew install winebarrel/cronplan/cronmatch

Usage

Usage: cronmatch [OPTION] CRON_EXPR DATE
  -h int
    	hour to add
  -no-color
    	disable color output
  -version
    	print version and exit
$ cronmatch -h -9 '0 1 * * ? *' '2022/10/20 10:00'
'0 1 * * ? *' matches '2022/10/20 10:00' (offset: -9h)

$ cronmatch '0 10 * * ? *' 'Oct 10, 2022, 10:10'
'0 10 * * ? *' does not match 'Oct 10, 2022, 10:10'

cf. https://github.com/araddon/dateparse

cronviz CLI

CLI to visualize cron schedule.

inspired by cronv, aws-cronv.

Installation

brew install winebarrel/cronplan/cronviz

Usage

Usage: cronviz [OPTION] [FILE]
  -f string
    	from date (default current date)
  -h int
    	hour to add
  -p string
    	period (default "1d")
  -version
    	print version and exit
$ cat cron.txt
batch1  0 * * * ? *
batch2  30 */2 * * ? *
batch3  15,45 */3 * * ? *

$ cronviz cron.txt > output.html
$ open output.html

cf. https://raw.githack.com/winebarrel/cronplan/main/_example/timeline.html

crongrep CLI

CLI to grep with cron expression.

Installation

brew install winebarrel/cronplan/crongrep

Usage

Usage: crongrep [OPTION] CRON_EXPR
  -version
    	print version and exit
$ for i in {1..5}; do
LANG=C date
done | crongrep '0 * * * ? *'
Sun Oct  1 21:00:00 JST 2023
Sun Oct  1 21:00:00 JST 2023
Sun Oct  1 21:00:00 JST 2023
Sun Oct  1 21:00:00 JST 2023
Sun Oct  1 21:00:00 JST 2023

Documentation

Overview

Cron expression parser for Amazon EventBridge.

Index

Constants

This section is empty.

Variables

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

Functions

This section is empty.

Types

type DayOfMonth

type DayOfMonth int

func (*DayOfMonth) Capture

func (v *DayOfMonth) Capture(values []string) error

func (*DayOfMonth) Int

func (v *DayOfMonth) Int() int

func (*DayOfMonth) Match

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

func (*DayOfMonth) String

func (v *DayOfMonth) String() string

type DayOfMonthExp

type DayOfMonthExp struct {
	NearestWeekday *NearestWeekday     `( @Number "W" )`
	Wildcard       bool                `| ( ( @"*"`
	Range          *DayOfMonthRange    `      | @@`
	Number         *DayOfMonth         `      | @Number )`
	Bottom         *int                `    ( "/" @Number )? )`
	LastWeekday    *LastWeekdayOfMonth `| ( @"L" "W" )`
	Last           *LastDayOfMonth     `| ( @"L" ( "-" @Number )? )`
}

func (*DayOfMonthExp) Match

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

func (*DayOfMonthExp) String

func (e *DayOfMonthExp) String() string

type DayOfMonthField

type DayOfMonthField struct {
	Exps []*DayOfMonthExp `( @@ ( "," @@ )* )`
	Any  bool             `| @"?"`
}

func (*DayOfMonthField) Match

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

func (*DayOfMonthField) String

func (v *DayOfMonthField) String() string

type DayOfMonthRange

type DayOfMonthRange struct {
	Start *DayOfMonth `@Number`
	End   *DayOfMonth `"-" @Number`
}

func (*DayOfMonthRange) Match

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

func (*DayOfMonthRange) String

func (v *DayOfMonthRange) String() string

type DayOfWeekExp

type DayOfWeekExp struct {
	Nth      *NthDayOfWeek  `@@`
	Last     *LastDayOfWeek `| @@`
	Wildcard bool           `| ( ( @"*"`
	Range    *WeekdayRange  `      | @@`
	Wday     *Weekday       `      | ( @Number | @Weekday ) )`
	Bottom   *int           `    ( "/" @Number )? )`
}

func (*DayOfWeekExp) Match

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

func (*DayOfWeekExp) String

func (e *DayOfWeekExp) String() string

type DayOfWeekField

type DayOfWeekField struct {
	Exps []*DayOfWeekExp `( @@ ( "," @@ )* )`
	Any  bool            `| @"?"`
}

func (*DayOfWeekField) Match

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

func (*DayOfWeekField) String

func (v *DayOfWeekField) String() string

type Expression

type Expression struct {
	Minute     *MinuteField     `@@`
	Hour       *HourField       `SP @@`
	DayOfMonth *DayOfMonthField `SP @@`
	Month      *MonthField      `SP @@`
	DayOfWeek  *DayOfWeekField  `SP @@`
	Year       *YearField       `SP @@`
}

func Parse

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

func (*Expression) Between

func (v *Expression) Between(from time.Time, to time.Time) []time.Time

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 Hour

type Hour int

func (*Hour) Capture

func (v *Hour) Capture(values []string) error

func (*Hour) Int

func (v *Hour) Int() int

func (*Hour) Match

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

func (*Hour) String

func (v *Hour) String() string

type HourExp

type HourExp struct {
	Wildcard bool       `( @"*"`
	Range    *HourRange `  | @@`
	Number   *Hour      `  | @Number )`
	Bottom   *int       `( "/" @Number )?`
}

func (*HourExp) Match

func (e *HourExp) Match(t time.Time) bool

func (*HourExp) String

func (e *HourExp) String() string

type HourField

type HourField struct {
	Exps []*HourExp `@@ ( "," @@ )*`
}

func (*HourField) Match

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

func (*HourField) String

func (v *HourField) String() string

type HourRange

type HourRange struct {
	Start *Hour `@Number`
	End   *Hour `"-" @Number`
}

func (*HourRange) Match

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

func (*HourRange) String

func (v *HourRange) String() string

type LastDayOfMonth

type LastDayOfMonth int

func (*LastDayOfMonth) Capture

func (v *LastDayOfMonth) Capture(values []string) error

func (*LastDayOfMonth) Int

func (v *LastDayOfMonth) Int() int

func (*LastDayOfMonth) Match

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

func (*LastDayOfMonth) String

func (v *LastDayOfMonth) String() string

type LastDayOfWeek

type LastDayOfWeek struct {
	Wday *Weekday `(@Number | @Weekday)? "L"`
}

func (*LastDayOfWeek) Match

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

func (*LastDayOfWeek) String

func (v *LastDayOfWeek) String() string

func (*LastDayOfWeek) Weekday

func (v *LastDayOfWeek) Weekday() time.Weekday

type LastWeekdayOfMonth added in v1.10.0

type LastWeekdayOfMonth struct{}

func (*LastWeekdayOfMonth) Capture added in v1.10.0

func (v *LastWeekdayOfMonth) Capture(values []string) error

func (*LastWeekdayOfMonth) Match added in v1.10.0

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

func (*LastWeekdayOfMonth) String added in v1.10.0

func (v *LastWeekdayOfMonth) String() string

type Minute

type Minute int

func (*Minute) Capture

func (v *Minute) Capture(values []string) error

func (*Minute) Int

func (v *Minute) Int() int

func (*Minute) Match

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

func (*Minute) String

func (v *Minute) String() string

type MinuteExp

type MinuteExp struct {
	Wildcard bool         `( @"*"`
	Range    *MinuteRange `  | @@`
	Number   *Minute      `  | @Number )`
	Bottom   *int         `( "/" @Number )?`
}

func (*MinuteExp) Match

func (e *MinuteExp) Match(t time.Time) bool

func (*MinuteExp) String

func (e *MinuteExp) String() string

type MinuteField

type MinuteField struct {
	Exps []*MinuteExp `@@ ( "," @@ )*`
}

func (*MinuteField) Match

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

func (*MinuteField) String

func (v *MinuteField) String() string

type MinuteRange

type MinuteRange struct {
	Start *Minute `@Number`
	End   *Minute `"-" @Number`
}

func (*MinuteRange) Match

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

func (*MinuteRange) String

func (v *MinuteRange) String() string

type Month

type Month int

func (*Month) Capture

func (v *Month) Capture(values []string) error

func (*Month) Int

func (v *Month) Int() int

func (*Month) Match

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

func (*Month) Month

func (v *Month) Month() time.Month

func (*Month) String

func (v *Month) String() string

type MonthExp

type MonthExp struct {
	Wildcard bool        `( @"*"`
	Range    *MonthRange `  | @@`
	Month    *Month      `  | ( @Number | @Month ) )`
	Bottom   *int        `( "/" @Number )?`
}

func (*MonthExp) Match

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

func (*MonthExp) String

func (e *MonthExp) String() string

type MonthField

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

func (*MonthField) Match

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

func (*MonthField) String

func (v *MonthField) String() string

type MonthRange

type MonthRange struct {
	Start *Month `( @Number | @Month )`
	End   *Month `"-" ( @Number | @Month )`
}

func (*MonthRange) Match

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

func (*MonthRange) String

func (v *MonthRange) String() string

type NearestWeekday

type NearestWeekday int

func (*NearestWeekday) Capture

func (v *NearestWeekday) Capture(values []string) error

func (*NearestWeekday) Int

func (v *NearestWeekday) Int() int

func (*NearestWeekday) Match

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

func (*NearestWeekday) String

func (v *NearestWeekday) String() string

type NthDayOfWeek

type NthDayOfWeek struct {
	Wday *Weekday `( @Number | @Weekday )`
	Nth  int      `"#" @Number`
}

func (*NthDayOfWeek) Match

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

func (*NthDayOfWeek) String

func (v *NthDayOfWeek) String() string

type Weekday

type Weekday time.Weekday

func (*Weekday) Capture

func (v *Weekday) Capture(values []string) error

func (*Weekday) Int

func (v *Weekday) Int() int

func (*Weekday) Match

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

func (*Weekday) String

func (v *Weekday) String() string

func (*Weekday) Weekday

func (v *Weekday) Weekday() time.Weekday

type WeekdayRange

type WeekdayRange struct {
	Start *Weekday `( @Number | @Weekday )`
	End   *Weekday `"-" ( @Number | @Weekday )`
}

func (*WeekdayRange) Match

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

func (*WeekdayRange) String

func (v *WeekdayRange) String() string

type Year

type Year int

func (*Year) Capture

func (v *Year) Capture(values []string) error

func (*Year) Int

func (v *Year) Int() int

func (*Year) Match

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

func (*Year) String

func (v *Year) String() string

type YearExp

type YearExp struct {
	Wildcard bool       `( @"*"`
	Range    *YearRange `  | @@`
	Number   *Year      `  | @Number )`
	Bottom   *int       `( "/" @Number )?`
}

func (*YearExp) Match

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

func (*YearExp) String

func (e *YearExp) String() string

type YearField

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

func (*YearField) Match

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

func (*YearField) String

func (v *YearField) String() string

type YearRange

type YearRange struct {
	Start *Year `@Number`
	End   *Year `"-" @Number`
}

func (*YearRange) Match

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

func (*YearRange) String

func (v *YearRange) String() string

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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