Skip to content

Navigation Menu

Sign in
Sign up

Repository files navigation

cronplan

CI Go Reference

Overview

Cron expression parser for Amazon EventBridge.

Try with curl

$ curl cronplan.in -d '5 0 10 * ? *'
2023年10月10日 00:05:00
2023年11月10日 00:05:00
2023年12月10日 00:05:00
2024年1月10日 00:05:00
2024年2月10日 00:05:00
2024年3月10日 00:05:00
2024年4月10日 00:05:00
2024年5月10日 00:05:00
2024年6月10日 00:05:00
2024年7月10日 00:05:00

Installation

go get github.com/winebarrel/cronplan

Usage

package main
import (
	"fmt"
	"time"
	"github.com/winebarrel/cronplan/v2"
)
func main() {
	cron, err := cronplan.Parse("0 10 * * ? *")
	if err != nil {
		panic(err)
	}
	fmt.Println(
		cron.Minute.Exps[0].Number, //=> 0
		cron.Hour.Exps[0].Number, //=> 10
		cron.String(), //=> "0 10 * * ? *"
	)
	cron.Match(time.Date(2022, 11, 3, 9, 0, 0, 0, time.UTC))
	//=> false
	cron.Match(time.Date(2022, 11, 3, 10, 0, 0, 0, time.UTC))
	//=> true
	// NOTE: If you don't want to include `from`, add `1 * time.Minute`
	cron.Next(time.Date(2022, 11, 3, 10, 0, 0, 0, time.UTC))
	//=> 2022年11月03日 10:00:00 +0000 UTC
	cron.Next(time.Date(2022, 11, 3, 11, 0, 0, 0, time.UTC))
	//=> 2022年11月04日 10:00:00 +0000 UTC
	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]
	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]
	iter := cron.Iter(time.Date(2022, 11, 3, 10, 0, 0, 0, time.UTC))
	for i := range 3 {
		fmt.Println(i, iter.Next())
		//=> 0 2022年11月03日 10:00:00 +0000 UTC
		//=> 1 2022年11月04日 10:00:00 +0000 UTC
		//=> 2 2022年11月05日 10:00:00 +0000 UTC
	}
	for next := range iter.Seq() {
		fmt.Println(next)
		//=> 2022年11月06日 10:00:00 +0000 UTC
		break
	}
}

Scheduler implementation example

https://github.com/winebarrel/cronplan/blob/main/_example/cron/main.go

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 *)
2023年10月27日 00:00:00
2023年11月24日 00:00:00
2023年12月29日 00:00:00
2024年1月26日 00:00:00
2024年2月23日 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 *)
2023年10月07日 00:00:00
2023年10月14日 00:00:00
2023年10月21日 00:00:00
2023年10月28日 00:00:00
2023年11月04日 00:00:00

Behavior of "31W" in day-of-month

If you specify "31W" for day-of-month, months without a 31st day will be skipped.

(I'm not sure if this is the correct behavior)

# cron(5 0 31W * ? 2026)
2026年1月30日 00:05:00
2026年3月31日 00:05:00
2026年5月29日 00:05:00
2026年7月31日 00:05:00
2026年8月31日 00:05:00
2026年10月30日 00:05:00
2026年12月31日 00:05:00

If you want the last weekday of the month, use "LW".

# cron(5 0 LW * ? 2026)
2026年1月30日 00:05:00
2026年2月27日 00:05:00
2026年3月31日 00:05:00
2026年4月30日 00:05:00
2026年5月29日 00:05:00
2026年6月30日 00:05:00
2026年7月31日 00:05:00
2026年8月31日 00:05:00
2026年9月30日 00:05:00
2026年10月30日 00:05: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 *'
2022年10月11日 10:00:00
2022年10月11日 10:10:00
2022年10月11日 10:20:00
2022年10月11日 10:30:00
2022年10月11日 10:40:00
2022年10月11日 10:50:00
2022年10月12日 10:00:00
2022年10月12日 10:10:00
2022年10月12日 10:20:00
2022年10月12日 10:30:00
$ cronplan -h -9 '*/10 10 ? * MON-FRI *'
2022年10月11日 01:00:00
2022年10月11日 01:10:00
2022年10月11日 01:20:00
2022年10月11日 01:30:00
2022年10月11日 01:40:00
2022年10月11日 01:50:00
2022年10月12日 01:00:00
2022年10月12日 01:10:00
2022年10月12日 01:20:00
2022年10月12日 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://pkg.go.dev/github.com/araddon/dateparse#readme-extended-example

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
$ cronplan -n 5 '10 12 */5 * ? *'
2023年10月06日 12:10:00
2023年10月11日 12:10:00
2023年10月16日 12:10:00
2023年10月21日 12:10:00
2023年10月26日 12:10:00
$ cronplan -n 5 '10 12 */5 * ? *' | crongrep '* * ? * WED-FRI *'
2023年10月06日 12:10:00
2023年10月11日 12:10:00
2023年10月26日 12:10:00

cronskd CLI

CLI to show a schedule of cron expressions.

Installation

brew install winebarrel/cronplan/cronskd

Usage

Usage: cronskd [OPTION] [FILE]
 -e string
 	end date (default: end of day)
 -s string
 	start date (default: beginning of day)
 -version
 	print version and exit
$ cat exprs.txt
0 10 * * ? *
15 12 * * ? *
0 18 ? * MON-FRI *
0 8 1 * ? *
5 8-10 ? * MON-FRI *
$ cronskd -s '2024-11-11' exprs.txt
2024年11月11日 08:05:00 5 8-10 ? * MON-FRI *
2024年11月11日 09:05:00 5 8-10 ? * MON-FRI *
2024年11月11日 10:00:00 0 10 * * ? *
2024年11月11日 10:05:00 5 8-10 ? * MON-FRI *
2024年11月11日 12:15:00 15 12 * * ? *
2024年11月11日 18:00:00 0 18 ? * MON-FRI *
$ cronskd -s '2024/11/12 10:00' -e 'Nov 13, 2024, 12:00' exprs.txt
2024年11月12日 10:00:00	0 10 * * ? *
2024年11月12日 10:05:00	5 8-10 ? * MON-FRI *
2024年11月12日 12:15:00	15 12 * * ? *
2024年11月12日 18:00:00	0 18 ? * MON-FRI *
2024年11月13日 08:05:00	5 8-10 ? * MON-FRI *
2024年11月13日 09:05:00	5 8-10 ? * MON-FRI *
2024年11月13日 10:00:00	0 10 * * ? *
2024年11月13日 10:05:00	5 8-10 ? * MON-FRI *

cf. https://pkg.go.dev/github.com/araddon/dateparse#readme-extended-example

Related Links

About

Cron expression parser for Amazon EventBridge.

Topics

Resources

Stars

40 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages

AltStyle によって変換されたページ (->オリジナル) /