forked from gorhill/cronexpr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cronexpr_quartz.go
77 lines (69 loc) · 2.13 KB
/
cronexpr_quartz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cronexpr
import (
"fmt"
)
var (
quartzDowMin = 0 // minimum value of quartzDowTokens
quartzDowMax = 6 // maximum value of quartzDowTokens
quartzDowTokens = map[string]int{
`1`: 0, `sun`: 0, `sunday`: 0,
`2`: 1, `mon`: 1, `monday`: 1,
`3`: 2, `tue`: 2, `tuesday`: 2,
`4`: 3, `wed`: 3, `wednesday`: 3,
`5`: 4, `thu`: 4, `thursday`: 4,
`6`: 5, `fri`: 5, `friday`: 5,
`7`: 6, `sat`: 6, `saturday`: 6,
}
quartzDowDescriptor = fieldDescriptor{
name: "day-of-week",
min: 0,
max: 6,
hashmax: 6,
defaultList: genericDefaultList[1:7],
valuePattern: `0?[1-7]|sun|mon|tue|wed|thu|fri|sat|sunday|monday|tuesday|wednesday|thursday|friday|saturday`,
atoi: func(s string) int {
return quartzDowTokens[s]
},
}
)
// quartzExpression implements custom parsing for the Quartz scheduler format.
type quartzExpression struct {
*Expression
}
// dowFieldHandler overrides the default day of week parsing.
// Day of week uses 1-7 for SUN-SAT, instead of 0-6 on standard implementations.
func (expr *quartzExpression) dowFieldHandler(s string) error {
expr.daysOfWeekRestricted = true
expr.daysOfWeek = make(map[int]bool)
expr.lastWeekDaysOfWeek = make(map[int]bool)
expr.specificWeekDaysOfWeek = make(map[int]bool)
// Use custom descriptor
directives, err := genericFieldParse(s, quartzDowDescriptor, expr.hash)
if err != nil {
return err
}
for _, directive := range directives {
sdirective := s[directive.sbeg:directive.send]
switch directive.kind {
case none:
// not implemented.
return fmt.Errorf("syntax error in day-of-week field: '%s'", sdirective)
case one:
populateOne(expr.daysOfWeek, directive.first)
case span:
// To properly handle spans that end in 7 (Sunday)
if directive.last == 0 {
directive.last = 6
}
// validate directive
if err := directive.IsValid(quartzDowMin, quartzDowMax); err != nil {
return err
}
populateMany(expr.daysOfWeek, directive.first, directive.last, directive.step)
case all:
populateMany(expr.daysOfWeek, directive.first, directive.last, directive.step)
expr.daysOfWeekRestricted = false
}
}
return nil
}