|
@@ -7,8 +7,8 @@ import (
|
|
|
"strings"
|
|
"strings"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-// Returns a new crontab schedule representing the given spec.
|
|
|
|
|
-// Panics with a descriptive error if the spec is not valid.
|
|
|
|
|
|
|
+// Parse returns a new crontab schedule representing the given spec.
|
|
|
|
|
+// It panics with a descriptive error if the spec is not valid.
|
|
|
func Parse(spec string) *Schedule {
|
|
func Parse(spec string) *Schedule {
|
|
|
if spec[0] == '@' {
|
|
if spec[0] == '@' {
|
|
|
return parseDescriptor(spec)
|
|
return parseDescriptor(spec)
|
|
@@ -38,8 +38,8 @@ func Parse(spec string) *Schedule {
|
|
|
return schedule
|
|
return schedule
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Return an Int with the bits set representing all of the times that the field represents.
|
|
|
|
|
-// A "field" is a comma-separated list of "ranges".
|
|
|
|
|
|
|
+// getField returns an Int with the bits set representing all of the times that
|
|
|
|
|
+// the field represents. A "field" is a comma-separated list of "ranges".
|
|
|
func getField(field string, r bounds) uint64 {
|
|
func getField(field string, r bounds) uint64 {
|
|
|
// list = range {"," range}
|
|
// list = range {"," range}
|
|
|
var bits uint64
|
|
var bits uint64
|
|
@@ -50,8 +50,10 @@ func getField(field string, r bounds) uint64 {
|
|
|
return bits
|
|
return bits
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// getRange returns the bits indicated by the given expression:
|
|
|
|
|
+// number | number "-" number [ "/" number ]
|
|
|
func getRange(expr string, r bounds) uint64 {
|
|
func getRange(expr string, r bounds) uint64 {
|
|
|
- // number | number "-" number [ "/" number ]
|
|
|
|
|
|
|
+
|
|
|
var (
|
|
var (
|
|
|
start, end, step uint
|
|
start, end, step uint
|
|
|
rangeAndStep = strings.Split(expr, "/")
|
|
rangeAndStep = strings.Split(expr, "/")
|
|
@@ -63,7 +65,7 @@ func getRange(expr string, r bounds) uint64 {
|
|
|
if lowAndHigh[0] == "*" || lowAndHigh[0] == "?" {
|
|
if lowAndHigh[0] == "*" || lowAndHigh[0] == "?" {
|
|
|
start = r.min
|
|
start = r.min
|
|
|
end = r.max
|
|
end = r.max
|
|
|
- extra_star = STAR_BIT
|
|
|
|
|
|
|
+ extra_star = starBit
|
|
|
} else {
|
|
} else {
|
|
|
start = parseIntOrName(lowAndHigh[0], r.names)
|
|
start = parseIntOrName(lowAndHigh[0], r.names)
|
|
|
switch len(lowAndHigh) {
|
|
switch len(lowAndHigh) {
|
|
@@ -103,6 +105,7 @@ func getRange(expr string, r bounds) uint64 {
|
|
|
return getBits(start, end, step) | extra_star
|
|
return getBits(start, end, step) | extra_star
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// parseIntOrName returns the (possibly-named) integer contained in expr.
|
|
|
func parseIntOrName(expr string, names map[string]uint) uint {
|
|
func parseIntOrName(expr string, names map[string]uint) uint {
|
|
|
if names != nil {
|
|
if names != nil {
|
|
|
if namedInt, ok := names[strings.ToLower(expr)]; ok {
|
|
if namedInt, ok := names[strings.ToLower(expr)]; ok {
|
|
@@ -112,6 +115,7 @@ func parseIntOrName(expr string, names map[string]uint) uint {
|
|
|
return mustParseInt(expr)
|
|
return mustParseInt(expr)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// mustParseInt parses the given expression as an int or panics.
|
|
|
func mustParseInt(expr string) uint {
|
|
func mustParseInt(expr string) uint {
|
|
|
num, err := strconv.Atoi(expr)
|
|
num, err := strconv.Atoi(expr)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -124,6 +128,7 @@ func mustParseInt(expr string) uint {
|
|
|
return uint(num)
|
|
return uint(num)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// getBits sets all bits in the range [min, max], modulo the given step size.
|
|
|
func getBits(min, max, step uint) uint64 {
|
|
func getBits(min, max, step uint) uint64 {
|
|
|
var bits uint64
|
|
var bits uint64
|
|
|
|
|
|
|
@@ -139,14 +144,18 @@ func getBits(min, max, step uint) uint64 {
|
|
|
return bits
|
|
return bits
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// all returns all bits within the given bounds. (plus the star bit)
|
|
|
func all(r bounds) uint64 {
|
|
func all(r bounds) uint64 {
|
|
|
- return getBits(r.min, r.max, 1) | STAR_BIT
|
|
|
|
|
|
|
+ return getBits(r.min, r.max, 1) | starBit
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// first returns bits with only the first (minimum) value set.
|
|
|
func first(r bounds) uint64 {
|
|
func first(r bounds) uint64 {
|
|
|
return getBits(r.min, r.min, 1)
|
|
return getBits(r.min, r.min, 1)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// parseDescriptor returns a pre-defined schedule for the expression, or panics
|
|
|
|
|
+// if none matches.
|
|
|
func parseDescriptor(spec string) *Schedule {
|
|
func parseDescriptor(spec string) *Schedule {
|
|
|
switch spec {
|
|
switch spec {
|
|
|
case "@yearly", "@annually":
|
|
case "@yearly", "@annually":
|