GitHunt
NO

norman-abramovitz/gorhill-cronexpr

Cron expression parser in Go language (golang)

NOTE

This cronexpr package is based on Raymond Hill's cronexpr package which
has been archived on Jan 10, 2019. This package seems to be the closest Go package that attempts
to implement Java's Quartz Cron expressions which is similar to Wikipedia's cron
expanded definition
. The Go language Quartz Scheduler, for example, does not come
close to the Java version. As I evaluated other Go Language implementations, these are some
of the various features that were missing:

  1. seconds support
  2. years support
  3. oversized ranges
  4. Last day (L)
  5. Last weekday (W)
  6. Nearest Day (#)
  7. Implementing AND/Intersection instead of OR/Union between day of month and day of the week fields.
    As noted in the follwing article as is done in the Cronitor Kubernetes implementation

Guiding Principlese

  1. Try to stay true to the Open Group, Java and Wikipedia specifications.
  2. Added extensions to that keep to the spirit of those specifications.
  3. Try to be responsive when people do PRs or Issue reporting

The remaining package documentation will located the doc.go file.

Install

go get github.com/norman-abramovitz/cronexpr

Usage

Import the library:

import "github.com/gorhill/cronexpr"
import "time"

Simplest way:

nextTime := cronexpr.MustParse("0 0 29 2 *").Next(time.Now())

Assuming time.Now() is "2013-08-29 09:28:00", then nextTime will be "2016-02-29 00:00:00".

You can keep the returned Expression pointer around if you want to reuse it:

expr := cronexpr.MustParse("0 0 29 2 *")
nextTime := expr.Next(time.Now())
...
nextTime = expr.Next(nextTime)

Use time.IsZero() to find out whether a valid time was returned. For example,

cronexpr.MustParse("* * * * * 1980").Next(time.Now()).IsZero()

will return true, whereas

cronexpr.MustParse("* * * * * 2050").Next(time.Now()).IsZero()

will return false (as of 2013-08-29...)

You may also query for n next time stamps:

cronexpr.MustParse("0 0 29 2 *").NextN(time.Now(), 5)

which returns a slice of time.Time objects, containing the following time stamps (as of 2013-08-30):

2016-02-29 00:00:00
2020-02-29 00:00:00
2024-02-29 00:00:00
2028-02-29 00:00:00
2032-02-29 00:00:00

The time zone of time values returned by Next and NextN is always the
time zone of the time value passed as argument, unless a zero time value is
returned.

API

TBD

License

License: pick the one which suits you best:

TODO

  1. Use Go Modules
  2. Add 5 or 6 or 7 crontab fields
  3. Add a cli command for quick testing
  4. Add previous time support
  5. Add better timezone support

Languages

Go99.5%Makefile0.5%

Contributors

Created September 19, 2024
Updated December 11, 2024