cronexpr

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2021 License: MIT Imports: 4 Imported by: 2

README

Cron expression parser

Given a cron expression, you can get the previous timestamp or the next timestamp that satisfies the cron expression.

I have used cron expression syntax to implement a new feature called deploy freeze window in Gitploy.

Install

go get github.com/gitploy-io/cronexpr

Usage

Import the package, first:

import "time"
import "github.com/gitploy-io/cronexpr"
prevTime := cronexpr.MustParse("0 0 29 * *").Prev(time.Now())
nextTime := cronexpr.MustParse("0 0 29 * *").Next(time.Now())

You can check the detail in the _example directory.

Implementation

Field name     Mandatory?   Allowed values    Allowed special characters
----------     ----------   --------------    --------------------------
Minutes        Yes          0-59              * / , -
Hours          Yes          0-23              * / , -
Day of month   Yes          1-31              * / , - 
Month          Yes          1-12 or JAN-DEC   * / , -
Day of week    Yes          0-6 or SUN-SAT    * / , - 
Asterisk ( * )

The asterisk indicates that the cron expression matches for all values of the field. E.g., using an asterisk in the 4th field (month) indicates every month.

Slash ( / )

Slashes describe increments of ranges. For example 3-59/15 in the minute field indicate the third minute of the hour and every 15 minutes thereafter (i.e. 3, 18, 33, and 48). The form */... is equivalent to the form "first-last/...", that is, an increment over the largest possible range of the field.

Comma ( , )

Commas are used to separate items of a list. For example, using MON,WED,FRI in the 5th field (day of week) means Mondays, Wednesdays and Fridays.

Hyphen ( - )

Hyphens define ranges. For example, 0-6 indicates every hours between 0 and 6 AM, inclusive.

Details

  • The return value of Next and Prev is zero if the pattern doesn't match in five years.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Schedule

type Schedule struct {
	Minute, Hour, Dom, Month, Dow bitset

	Location *time.Location
}

func MustParse

func MustParse(expr string) *Schedule

MustParse returns the same result as Parse but it panic when the syntax of expression is wrong.

func MustParseInLocation added in v0.2.0

func MustParseInLocation(expr string, locName string) *Schedule

MustParse returns the same result as Parse but it panic when something is wrong.

func Parse

func Parse(expr string) (*Schedule, error)

Parse parses the expression and returns a new schedule representing the given spec. And the default location of a schedule is "UTC". It returns an error when the syntax of expression is wrong.

func ParseInLocation added in v0.2.0

func ParseInLocation(expr string, locName string) (*Schedule, error)

ParseInLocation parse the expression in the location and returns a new schedule representing the given spec. It returns an error when loading the location is failed or the syntax of the expression is wrong.

func (*Schedule) Next

func (s *Schedule) Next(t time.Time) time.Time

Next returns the next time matched with the expression.

func (*Schedule) Prev

func (s *Schedule) Prev(t time.Time) time.Time

Next returns the previous time matched with the expression.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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