-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcron.go
448 lines (354 loc) · 9.76 KB
/
cron.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package chrono
import (
"errors"
"fmt"
"math"
"math/bits"
"strconv"
"strings"
"time"
)
var (
months = []string{"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}
days = []string{"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"}
)
type cronField string
const (
cronFieldNanoSecond = "NANO_SECOND"
cronFieldSecond = "SECOND"
cronFieldMinute = "MINUTE"
cronFieldHour = "HOUR"
cronFieldDayOfMonth = "DAY_OF_MONTH"
cronFieldMonth = "MONTH"
cronFieldDayOfWeek = "DAY_OF_WEEK"
)
type fieldType struct {
Field cronField
MinValue int
MaxValue int
}
var (
nanoSecond = fieldType{cronFieldNanoSecond, 0, 999999999}
second = fieldType{cronFieldSecond, 0, 59}
minute = fieldType{cronFieldMinute, 0, 59}
hour = fieldType{cronFieldHour, 0, 23}
dayOfMonth = fieldType{cronFieldDayOfMonth, 1, 31}
month = fieldType{cronFieldMonth, 1, 12}
dayOfWeek = fieldType{cronFieldDayOfWeek, 1, 7}
)
var cronFieldTypes = []fieldType{
second,
minute,
hour,
dayOfMonth,
month,
dayOfWeek,
}
type valueRange struct {
MinValue int
MaxValue int
}
func newValueRange(min int, max int) valueRange {
return valueRange{
MinValue: min,
MaxValue: max,
}
}
type cronFieldBits struct {
Typ fieldType
Bits uint64
}
func newFieldBits(typ fieldType) *cronFieldBits {
return &cronFieldBits{
Typ: typ,
}
}
const maxAttempts = 366
const mask = 0xFFFFFFFFFFFFFFFF
type CronExpression struct {
fields []*cronFieldBits
}
func newCronExpression() *CronExpression {
exp := &CronExpression{
make([]*cronFieldBits, 0),
}
nanoSecondBits := newFieldBits(nanoSecond)
nanoSecondBits.Bits = 1
exp.fields = append(exp.fields, nanoSecondBits)
return exp
}
func (expression *CronExpression) NextTime(t time.Time) time.Time {
t = t.Add(1 * time.Nanosecond)
for i := 0; i < maxAttempts; i++ {
result := expression.next(t)
if result.IsZero() || result.Equal(t) {
return result
}
t = result
}
return time.Time{}
}
func (expression *CronExpression) next(t time.Time) time.Time {
for _, field := range expression.fields {
t = expression.nextField(field, t)
if t.IsZero() {
return t
}
}
return t
}
func (expression *CronExpression) nextField(field *cronFieldBits, t time.Time) time.Time {
current := getTimeValue(t, field.Typ.Field)
next := setNextBit(field.Bits, current)
if next == -1 {
amount := getFieldMaxValue(t, field.Typ) - current + 1
t = addTime(t, field.Typ.Field, amount)
next = setNextBit(field.Bits, 0)
}
if next == current {
return t
} else {
count := 0
current := getTimeValue(t, field.Typ.Field)
for ; current != next && count < maxAttempts; count++ {
t = elapseUntil(t, field.Typ, next)
current = getTimeValue(t, field.Typ.Field)
}
if count >= maxAttempts {
return time.Time{}
}
return t
}
}
func ParseCronExpression(expression string) (*CronExpression, error) {
if len(expression) == 0 {
return nil, errors.New("cron expression must not be empty")
}
fields := strings.Fields(expression)
if len(fields) != 6 {
return nil, fmt.Errorf("cron expression must consist of 6 fields : found %d in \"%s\"", len(fields), expression)
}
cronExpression := newCronExpression()
for index, cronFieldType := range cronFieldTypes {
value, err := parseField(fields[index], cronFieldType)
if err != nil {
return nil, err
}
if cronFieldType.Field == cronFieldDayOfWeek && value.Bits&1<<0 != 0 {
value.Bits |= 1 << 7
temp := ^(1 << 0)
value.Bits &= uint64(temp)
}
cronExpression.fields = append(cronExpression.fields, value)
}
return cronExpression, nil
}
func parseField(value string, fieldType fieldType) (*cronFieldBits, error) {
if len(value) == 0 {
return nil, fmt.Errorf("value must not be empty")
}
if fieldType.Field == cronFieldMonth {
value = replaceOrdinals(value, months)
} else if fieldType.Field == cronFieldDayOfWeek {
value = replaceOrdinals(value, days)
}
cronFieldBits := newFieldBits(fieldType)
fields := strings.Split(value, ",")
for _, field := range fields {
slashPos := strings.Index(field, "/")
step := -1
var valueRange valueRange
if slashPos != -1 {
rangeStr := field[0:slashPos]
var err error
valueRange, err = parseRange(rangeStr, fieldType)
if err != nil {
return nil, err
}
if strings.Index(rangeStr, "-") == -1 {
valueRange = newValueRange(valueRange.MinValue, fieldType.MaxValue)
}
stepStr := field[slashPos+1:]
step, err = strconv.Atoi(stepStr)
if err != nil {
return nil, fmt.Errorf("step must be number : \"%s\"", stepStr)
}
if step <= 0 {
return nil, fmt.Errorf("step must be 1 or higher in \"%s\"", value)
}
} else {
var err error
valueRange, err = parseRange(field, fieldType)
if err != nil {
return nil, err
}
}
if step > 1 {
for index := valueRange.MinValue; index <= valueRange.MaxValue; index += step {
cronFieldBits.Bits |= 1 << index
}
continue
}
if valueRange.MinValue == valueRange.MaxValue {
cronFieldBits.Bits |= 1 << valueRange.MinValue
} else {
cronFieldBits.Bits |= ^(math.MaxUint64 << (valueRange.MaxValue + 1)) & (math.MaxUint64 << valueRange.MinValue)
}
}
return cronFieldBits, nil
}
func parseRange(value string, fieldType fieldType) (valueRange, error) {
if value == "*" {
return newValueRange(fieldType.MinValue, fieldType.MaxValue), nil
} else {
hyphenPos := strings.Index(value, "-")
if hyphenPos == -1 {
result, err := checkValidValue(value, fieldType)
if err != nil {
return valueRange{}, err
}
return newValueRange(result, result), nil
} else {
maxStr := value[hyphenPos+1:]
minStr := value[0:hyphenPos]
min, err := checkValidValue(minStr, fieldType)
if err != nil {
return valueRange{}, err
}
var max int
max, err = checkValidValue(maxStr, fieldType)
if err != nil {
return valueRange{}, err
}
if fieldType.Field == cronFieldDayOfWeek && min == 7 {
min = 0
}
return newValueRange(min, max), nil
}
}
}
func replaceOrdinals(value string, list []string) string {
value = strings.ToUpper(value)
for index := 0; index < len(list); index++ {
replacement := strconv.Itoa(index + 1)
value = strings.ReplaceAll(value, list[index], replacement)
}
return value
}
func checkValidValue(value string, fieldType fieldType) (int, error) {
result, err := strconv.Atoi(value)
if err != nil {
return 0, fmt.Errorf("the value in field %s must be number : %s", fieldType.Field, value)
}
if fieldType.Field == cronFieldDayOfWeek && result == 0 {
return result, nil
}
if result >= fieldType.MinValue && result <= fieldType.MaxValue {
return result, nil
}
return 0, fmt.Errorf("the value in field %s must be between %d and %d", fieldType.Field, fieldType.MinValue, fieldType.MaxValue)
}
func getTimeValue(t time.Time, field cronField) int {
switch field {
case cronFieldNanoSecond:
return t.Nanosecond()
case cronFieldSecond:
return t.Second()
case cronFieldMinute:
return t.Minute()
case cronFieldHour:
return t.Hour()
case cronFieldDayOfMonth:
return t.Day()
case cronFieldMonth:
return int(t.Month())
case cronFieldDayOfWeek:
if t.Weekday() == 0 {
return 7
}
return int(t.Weekday())
}
panic("unreachable code!")
}
func addTime(t time.Time, field cronField, value int) time.Time {
switch field {
case cronFieldNanoSecond:
return t.Add(time.Duration(value) * time.Nanosecond)
case cronFieldSecond:
return t.Add(time.Duration(value) * time.Second)
case cronFieldMinute:
return t.Add(time.Duration(value) * time.Minute)
case cronFieldHour:
return t.Add(time.Duration(value) * time.Hour)
case cronFieldDayOfMonth:
return t.AddDate(0, 0, value)
case cronFieldMonth:
return t.AddDate(0, value, 0)
case cronFieldDayOfWeek:
return t.AddDate(0, 0, value)
}
panic("unreachable code!")
}
func setNextBit(bitsValue uint64, index int) int {
result := bitsValue & (mask << index)
if result != 0 {
return bits.TrailingZeros64(result)
}
return -1
}
func elapseUntil(t time.Time, fieldType fieldType, value int) time.Time {
current := getTimeValue(t, fieldType.Field)
maxValue := getFieldMaxValue(t, fieldType)
if current >= value {
amount := value + maxValue - current + 1 - fieldType.MinValue
return addTime(t, fieldType.Field, amount)
}
if value >= fieldType.MinValue && value <= maxValue {
return with(t, fieldType.Field, value)
}
return addTime(t, fieldType.Field, value-current)
}
func with(t time.Time, field cronField, value int) time.Time {
switch field {
case cronFieldNanoSecond:
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), value, time.Local)
case cronFieldSecond:
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), value, t.Nanosecond(), time.Local)
case cronFieldMinute:
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), value, t.Second(), t.Nanosecond(), time.Local)
case cronFieldHour:
return time.Date(t.Year(), t.Month(), t.Day(), value, t.Minute(), t.Second(), t.Nanosecond(), time.Local)
case cronFieldDayOfMonth:
return time.Date(t.Year(), t.Month(), value, t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.Local)
case cronFieldMonth:
return time.Date(t.Year(), time.Month(value), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.Local)
case cronFieldDayOfWeek:
return t.AddDate(0, 0, value-int(t.Weekday()))
}
panic("unreachable code!")
}
func getFieldMaxValue(t time.Time, fieldType fieldType) int {
if cronFieldDayOfMonth == fieldType.Field {
switch int(t.Month()) {
case 2:
if isLeapYear(t.Year()) {
return 29
}
return 28
case 4:
return 30
case 6:
return 30
case 9:
return 30
case 11:
return 30
default:
return 31
}
}
return fieldType.MaxValue
}
func isLeapYear(year int) bool {
return year%400 == 0 || year%100 != 0 && year%4 == 0
}