Skip to content
This repository was archived by the owner on Jan 11, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The reference documentation for this implementation is found at
Year No 1970–2099 * / , -

#### 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.
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. The form `*/...` is equivalent to the form "first-last/...", that is, an increment over the largest possible range of the field.
Expand Down Expand Up @@ -52,7 +52,7 @@ The `W` character can also be combined with `L`, i.e. `LW` to mean "the last bus

Predefined cron expressions
---------------------------
(Copied from <https://en.wikipedia.org/wiki/Cron#Predefined_scheduling_definitions>, with text modified according to this implementation)
(Copied from <https://en.wikipedia.org/wiki/Cron#Predefined_scheduling_definitions>, with text modified according to this implementation)

Entry Description Equivalent to
@annually Run once a year at midnight in the morning of January 1 0 0 0 1 1 * *
Expand All @@ -69,6 +69,7 @@ Other details
* If only five fields are present, a `0` second field is prepended and a wildcard year field is appended, that is, `* * * * Mon` internally become `0 * * * * Mon *`.
* Domain for day-of-week field is [0-7] instead of [0-6], 7 being Sunday (like 0). This to comply with http://linux.die.net/man/5/crontab#.
* As of now, the behavior of the code is undetermined if a malformed cron expression is supplied
* By default, on a DST change, it returns the times that would have been skipped when the clock moves forward and returns only once the times that would have been repeated when the clock moves backwards. For customized behavior, see the godoc documentation.

Install
-------
Expand Down Expand Up @@ -131,4 +132,3 @@ License: pick the one which suits you best:

- GPL v3 see <https://www.gnu.org/licenses/gpl.html>
- APL v2 see <http://www.apache.org/licenses/LICENSE-2.0>

35 changes: 34 additions & 1 deletion cronexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
// <https://github.com/gorhill/cronexpr#implementation>
type Expression struct {
expression string
options Options
secondList []int
minuteList []int
hourList []int
Expand All @@ -42,6 +43,30 @@ type Expression struct {
lastWeekDaysOfWeek map[int]bool
daysOfWeekRestricted bool
yearList []int

// indicates an instance of a expression used internally
// for rounding a time
rounding bool
}

type DSTFlags uint

const (
// DSTLeapUnskip indicates the parser to not skip times that would have been
// been skipped when the clock moves forward on a DST change.
DSTLeapUnskip = 1 << iota

// DSTFallFireEarly indicates the parser to return the earliest time
// when a time is repeated due to a DST fall.
DSTFallFireEarly

// DSTFallFireLate indicates the parser to return the latest time
// when a time is repeated due to a DST fall.
DSTFallFireLate
)

type Options struct {
DSTFlags DSTFlags
}

/******************************************************************************/
Expand All @@ -67,6 +92,14 @@ func MustParse(cronLine string) *Expression {
// about what is a well-formed cron expression from this library's point of
// view.
func Parse(cronLine string) (*Expression, error) {
return ParseWithOptions(cronLine, Options{DSTFlags: DSTLeapUnskip | DSTFallFireEarly})
}

// ParseWithOptions is used to build a Expression pointer with custom options.
func ParseWithOptions(cronLine string, options Options) (*Expression, error) {
if options.DSTFlags == 0 {
return nil, fmt.Errorf("missing DST flags")
}

// Maybe one of the built-in aliases is being used
cron := cronNormalizer.Replace(cronLine)
Expand All @@ -81,7 +114,7 @@ func Parse(cronLine string) (*Expression, error) {
fieldCount = 7
}

var expr = Expression{}
var expr = Expression{options: options}
var field = 0
var err error

Expand Down
Loading