gronx is Golang cron expression parser ported from adhocore/cron-expr.
- Zero dependency.
- Very fast because it bails early in case a segment doesn't match.
go get -u github.com/adhocore/gronx
import ( "time" "github.com/adhocore/gronx" ) gron := gronx.New() expr := "* * * * *" // check if expr is even valid, returns bool gron.IsValid(expr) // true // check if expr is due for current time, returns bool and error gron.IsDue(expr) // true|false, nil // check if expr is due for given time gron.IsDue(expr, time.Date(2021, time.April, 1, 1, 1, 0, 0, time.UTC)) // true|false, nil
Cron expression normally consists of 5 segments viz:
<minute> <hour> <day> <month> <weekday>
and sometimes there can be 6th segment for
<year> at the end.
For each segments you can have multiple choices separated by comma:
0,30 * * * *means either 0th or 30th minute.
To specify range of values you can use dash:
10-15 * * * *means 10th, 11th, 12th, 13th, 14th and 15th minute.
To specify range of step you can combine a dash and slash:
10-15/2 * * * *means every 2 minutes between 10 and 15 i.e 10th, 12th and 14th minute.
For the 3rd and 5th segment, there are additional modifiers (optional).
And if you want, you can mix them up:
5,12-20/4,55 * * * *matches if any one of
55matches the minute.
You can use real abbreviations for month and week days. eg:
Following tags are available and they are converted to real cron expressions before parsing:
- @yearly or @annually - every year
- @monthly - every month
- @daily - every day
- @weekly - every week
- @hourly - every hour
- @5minutes - every 5 minutes
- @10minutes - every 10 minutes
- @15minutes - every 15 minutes
- @30minutes - every 30 minutes
- @always - every minute
Following modifiers supported
- Day of Month / 3rd segment:
Lstands for last day of month (eg:
Lcould mean 29th for February in leap year)
Wstands for closest week day (eg:
10Wis closest week days (MON-FRI) to 10th date)
- Day of Week / 5th segment:
Lstands for last weekday of month (eg:
2Lis last monday)
#stands for nth day of week in the month (eg:
1#2is second sunday)
© MIT | 2017-2019, Jitendra Adhikari