-
Notifications
You must be signed in to change notification settings - Fork 0
/
cronexpr.go
executable file
·268 lines (238 loc) · 5.22 KB
/
cronexpr.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package timer
// reference: https://github.com/robfig/cron
import (
"fmt"
"math"
"strconv"
"strings"
"time"
)
// Field name | Mandatory? | Allowed values | Allowed special characters
// ---------- | ---------- | -------------- | --------------------------
// Seconds | No | 0-59 | * / , -
// Minutes | Yes | 0-59 | * / , -
// Hours | Yes | 0-23 | * / , -
// Day of month | Yes | 1-31 | * / , -
// Month | Yes | 1-12 | * / , -
// Day of week | Yes | 0-6 | * / , -
type CronExpr struct {
sec uint64
min uint64
hour uint64
dom uint64
month uint64
dow uint64
}
// goroutine safe
func NewCronExpr(expr string) (cronExpr *CronExpr, err error) {
fields := strings.Fields(expr)
if len(fields) != 5 && len(fields) != 6 {
err = fmt.Errorf("invalid expr %v: expected 5 or 6 fields, got %v", expr, len(fields))
return
}
if len(fields) == 5 {
fields = append([]string{"0"}, fields...)
}
cronExpr = new(CronExpr)
// Seconds
cronExpr.sec, err = parseCronField(fields[0], 0, 59)
if err != nil {
goto onError
}
// Minutes
cronExpr.min, err = parseCronField(fields[1], 0, 59)
if err != nil {
goto onError
}
// Hours
cronExpr.hour, err = parseCronField(fields[2], 0, 23)
if err != nil {
goto onError
}
// Day of month
cronExpr.dom, err = parseCronField(fields[3], 1, 31)
if err != nil {
goto onError
}
// Month
cronExpr.month, err = parseCronField(fields[4], 1, 12)
if err != nil {
goto onError
}
// Day of week
cronExpr.dow, err = parseCronField(fields[5], 0, 6)
if err != nil {
goto onError
}
return
onError:
err = fmt.Errorf("invalid expr %v: %v", expr, err)
return
}
// 1. *
// 2. num
// 3. num-num
// 4. */num
// 5. num/num (means num-max/num)
// 6. num-num/num
func parseCronField(field string, min int, max int) (cronField uint64, err error) {
fields := strings.Split(field, ",")
for _, field := range fields {
rangeAndIncr := strings.Split(field, "/")
if len(rangeAndIncr) > 2 {
err = fmt.Errorf("too many slashes: %v", field)
return
}
// range
startAndEnd := strings.Split(rangeAndIncr[0], "-")
if len(startAndEnd) > 2 {
err = fmt.Errorf("too many hyphens: %v", rangeAndIncr[0])
return
}
var start, end int
if startAndEnd[0] == "*" {
if len(startAndEnd) != 1 {
err = fmt.Errorf("invalid range: %v", rangeAndIncr[0])
return
}
start = min
end = max
} else {
// start
start, err = strconv.Atoi(startAndEnd[0])
if err != nil {
err = fmt.Errorf("invalid range: %v", rangeAndIncr[0])
return
}
// end
if len(startAndEnd) == 1 {
if len(rangeAndIncr) == 2 {
end = max
} else {
end = start
}
} else {
end, err = strconv.Atoi(startAndEnd[1])
if err != nil {
err = fmt.Errorf("invalid range: %v", rangeAndIncr[0])
return
}
}
}
if start > end {
err = fmt.Errorf("invalid range: %v", rangeAndIncr[0])
return
}
if start < min {
err = fmt.Errorf("out of range [%v, %v]: %v", min, max, rangeAndIncr[0])
return
}
if end > max {
err = fmt.Errorf("out of range [%v, %v]: %v", min, max, rangeAndIncr[0])
return
}
// increment
var incr int
if len(rangeAndIncr) == 1 {
incr = 1
} else {
incr, err = strconv.Atoi(rangeAndIncr[1])
if err != nil {
err = fmt.Errorf("invalid increment: %v", rangeAndIncr[1])
return
}
if incr <= 0 {
err = fmt.Errorf("invalid increment: %v", rangeAndIncr[1])
return
}
}
// cronField
if incr == 1 {
cronField |= ^(math.MaxUint64 << uint(end+1)) & (math.MaxUint64 << uint(start))
} else {
for i := start; i <= end; i += incr {
cronField |= 1 << uint(i)
}
}
}
return
}
func (e *CronExpr) matchDay(t time.Time) bool {
// day-of-month blank
if e.dom == 0xfffffffe {
return 1<<uint(t.Weekday())&e.dow != 0
}
// day-of-week blank
if e.dow == 0x7f {
return 1<<uint(t.Day())&e.dom != 0
}
return 1<<uint(t.Weekday())&e.dow != 0 ||
1<<uint(t.Day())&e.dom != 0
}
// goroutine safe
func (e *CronExpr) Next(t time.Time) time.Time {
// the upcoming second
t = t.Truncate(time.Second).Add(time.Second)
year := t.Year()
initFlag := false
retry:
// Year
if t.Year() > year+1 {
return time.Time{}
}
// Month
for 1<<uint(t.Month())&e.month == 0 {
if !initFlag {
initFlag = true
t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
}
t = t.AddDate(0, 1, 0)
if t.Month() == time.January {
goto retry
}
}
// Day
for !e.matchDay(t) {
if !initFlag {
initFlag = true
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}
t = t.AddDate(0, 0, 1)
if t.Day() == 1 {
goto retry
}
}
// Hours
for 1<<uint(t.Hour())&e.hour == 0 {
if !initFlag {
initFlag = true
t = t.Truncate(time.Hour)
}
t = t.Add(time.Hour)
if t.Hour() == 0 {
goto retry
}
}
// Minutes
for 1<<uint(t.Minute())&e.min == 0 {
if !initFlag {
initFlag = true
t = t.Truncate(time.Minute)
}
t = t.Add(time.Minute)
if t.Minute() == 0 {
goto retry
}
}
// Seconds
for 1<<uint(t.Second())&e.sec == 0 {
if !initFlag {
initFlag = true
}
t = t.Add(time.Second)
if t.Second() == 0 {
goto retry
}
}
return t
}