-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec.go
More file actions
400 lines (322 loc) · 9.92 KB
/
spec.go
File metadata and controls
400 lines (322 loc) · 9.92 KB
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
package cron
import (
"strconv"
"strings"
"time"
)
// SpecSchedule specifies a duty cycle (to the second granularity), based on a
// traditional crontab specification. It is computed initially and stored as bit sets.
type SpecSchedule struct {
Second, Minute, Hour, Dom, Month, Dow uint64
// Override location for this schedule.
Location *time.Location
}
// bounds provides a range of acceptable values (plus a map of name to value).
type bounds struct {
min, max uint
names map[string]uint
}
const (
// Set the top bit if a star was included in the expression.
starBit = 1 << 63
middayHour = 12
hoursPerDay = 24
yearSearchSpan = 5
minSecondValue uint = 0
maxSecondValue uint = 59
minMinuteValue uint = 0
maxMinuteValue uint = 59
minHourValue uint = 0
maxHourValue uint = 23
minDayOfMonthValue uint = 1
maxDayOfMonthValue uint = 31
minMonthValue uint = 1
maxMonthValue uint = 12
minDayOfWeekValue uint = 0
maxDayOfWeekValue uint = 6
monthJanuaryValue uint = 1
monthFebruaryValue uint = 2
monthMarchValue uint = 3
monthAprilValue uint = 4
monthMayValue uint = 5
monthJuneValue uint = 6
monthJulyValue uint = 7
monthAugustValue uint = 8
monthSeptemberValue uint = 9
monthOctoberValue uint = 10
monthNovemberValue uint = 11
monthDecemberValue uint = 12
weekdaySundayValue uint = 0
weekdayMondayValue uint = 1
weekdayTuesdayValue uint = 2
weekdayWednesdayValue uint = 3
weekdayThursdayValue uint = 4
weekdayFridayValue uint = 5
weekdaySaturdayValue uint = 6
)
func secondBounds() bounds {
return bounds{minSecondValue, maxSecondValue, nil}
}
func minuteBounds() bounds {
return bounds{minMinuteValue, maxMinuteValue, nil}
}
func hourBounds() bounds {
return bounds{minHourValue, maxHourValue, nil}
}
func dayOfMonthBounds() bounds {
return bounds{minDayOfMonthValue, maxDayOfMonthValue, nil}
}
func monthBounds() bounds {
return bounds{minMonthValue, maxMonthValue, map[string]uint{
"jan": monthJanuaryValue,
"feb": monthFebruaryValue,
"mar": monthMarchValue,
"apr": monthAprilValue,
"may": monthMayValue,
"jun": monthJuneValue,
"jul": monthJulyValue,
"aug": monthAugustValue,
"sep": monthSeptemberValue,
"oct": monthOctoberValue,
"nov": monthNovemberValue,
"dec": monthDecemberValue,
}}
}
func dayOfWeekBounds() bounds {
return bounds{minDayOfWeekValue, maxDayOfWeekValue, map[string]uint{
"sun": weekdaySundayValue,
"mon": weekdayMondayValue,
"tue": weekdayTuesdayValue,
"wed": weekdayWednesdayValue,
"thu": weekdayThursdayValue,
"fri": weekdayFridayValue,
"sat": weekdaySaturdayValue,
}}
}
// Next returns the next time this schedule is activated, greater than the given
// time. If no time can be found to satisfy the schedule, return the zero time.
func (s *SpecSchedule) Next(candidate time.Time) time.Time {
origLocation := candidate.Location()
nextActivation, ok := s.nextActivation(candidate)
if !ok {
return time.Time{}
}
return nextActivation.In(origLocation)
}
// String reconstructs a 6-field cron expression (second minute hour dom month
// dow) from the schedule's bit fields. If a field matches all values in its
// range the output is "*". The returned expression can be re-parsed by a
// [SpecParser] configured with [Second].
func (s *SpecSchedule) String() string {
return strings.Join([]string{
fieldString(s.Second, secondBounds()),
fieldString(s.Minute, minuteBounds()),
fieldString(s.Hour, hourBounds()),
fieldString(s.Dom, dayOfMonthBounds()),
fieldString(s.Month, monthBounds()),
fieldString(s.Dow, dayOfWeekBounds()),
}, " ")
}
type nextActivationState struct {
schedule *SpecSchedule
added bool
location *time.Location
yearLimit int
}
func (s *SpecSchedule) nextActivation(candidate time.Time) (time.Time, bool) {
candidate, location := s.prepareNext(candidate)
state := nextActivationState{
schedule: s,
location: location,
yearLimit: candidate.Year() + yearSearchSpan,
}
return state.find(candidate)
}
func (state *nextActivationState) find(candidate time.Time) (time.Time, bool) {
for candidate.Year() <= state.yearLimit {
var wrapped bool
candidate, wrapped = state.advance(candidate)
if !wrapped {
return candidate, true
}
}
return time.Time{}, false
}
func (state *nextActivationState) advance(candidate time.Time) (time.Time, bool) {
steps := [...]func(time.Time) (time.Time, bool){
func(candidate time.Time) (time.Time, bool) {
return state.schedule.advanceMonth(candidate, &state.added, state.location)
},
func(candidate time.Time) (time.Time, bool) {
return state.schedule.advanceDay(candidate, &state.added, state.location)
},
func(candidate time.Time) (time.Time, bool) {
return state.schedule.advanceHour(candidate, &state.added, state.location)
},
func(candidate time.Time) (time.Time, bool) {
return state.schedule.advanceMinute(candidate, &state.added)
},
func(candidate time.Time) (time.Time, bool) {
return state.schedule.advanceSecond(candidate, &state.added)
},
}
for _, step := range steps {
var wrapped bool
candidate, wrapped = step(candidate)
if wrapped {
return candidate, true
}
}
return candidate, false
}
func (s *SpecSchedule) prepareNext(candidate time.Time) (time.Time, *time.Location) {
loc := s.Location
if loc == time.Local {
loc = candidate.Location()
}
if s.Location != time.Local {
candidate = candidate.In(s.Location)
}
return candidate.Add(time.Second - time.Duration(candidate.Nanosecond())*time.Nanosecond), loc
}
func (s *SpecSchedule) advanceMonth(candidate time.Time, added *bool, loc *time.Location) (time.Time, bool) {
for !s.monthMatches(candidate) {
candidate = resetMonth(candidate, added, loc)
candidate = candidate.AddDate(0, 1, 0)
if candidate.Month() == time.January {
return candidate, true
}
}
return candidate, false
}
func (s *SpecSchedule) advanceDay(candidate time.Time, added *bool, loc *time.Location) (time.Time, bool) {
for !dayMatches(s, candidate) {
candidate = resetDay(candidate, added, loc)
candidate = normalizeMidnight(candidate.AddDate(0, 0, 1))
if candidate.Day() == 1 {
return candidate, true
}
}
return candidate, false
}
func (s *SpecSchedule) advanceHour(candidate time.Time, added *bool, loc *time.Location) (time.Time, bool) {
for !s.hourMatches(candidate) {
candidate = resetHour(candidate, added, loc)
candidate = candidate.Add(time.Hour)
if candidate.Hour() == 0 {
return candidate, true
}
}
return candidate, false
}
func (s *SpecSchedule) advanceMinute(candidate time.Time, added *bool) (time.Time, bool) {
for !s.minuteMatches(candidate) {
candidate = truncateMinute(candidate, added)
candidate = candidate.Add(time.Minute)
if candidate.Minute() == 0 {
return candidate, true
}
}
return candidate, false
}
func (s *SpecSchedule) advanceSecond(candidate time.Time, added *bool) (time.Time, bool) {
for !s.secondMatches(candidate) {
candidate = truncateSecond(candidate, added)
candidate = candidate.Add(time.Second)
if candidate.Second() == 0 {
return candidate, true
}
}
return candidate, false
}
func (s *SpecSchedule) monthMatches(candidate time.Time) bool {
return hasBit(s.Month, int(candidate.Month()))
}
func (s *SpecSchedule) hourMatches(candidate time.Time) bool {
return hasBit(s.Hour, candidate.Hour())
}
func (s *SpecSchedule) minuteMatches(candidate time.Time) bool {
return hasBit(s.Minute, candidate.Minute())
}
func (s *SpecSchedule) secondMatches(candidate time.Time) bool {
return hasBit(s.Second, candidate.Second())
}
func resetMonth(candidate time.Time, added *bool, loc *time.Location) time.Time {
if *added {
return candidate
}
*added = true
return time.Date(candidate.Year(), candidate.Month(), 1, 0, 0, 0, 0, loc)
}
func resetDay(candidate time.Time, added *bool, loc *time.Location) time.Time {
if *added {
return candidate
}
*added = true
return time.Date(candidate.Year(), candidate.Month(), candidate.Day(), 0, 0, 0, 0, loc)
}
func resetHour(candidate time.Time, added *bool, loc *time.Location) time.Time {
if *added {
return candidate
}
*added = true
return time.Date(candidate.Year(), candidate.Month(), candidate.Day(), candidate.Hour(), 0, 0, 0, loc)
}
func truncateMinute(candidate time.Time, added *bool) time.Time {
if *added {
return candidate
}
*added = true
return candidate.Truncate(time.Minute)
}
func truncateSecond(candidate time.Time, added *bool) time.Time {
if *added {
return candidate
}
*added = true
return candidate.Truncate(time.Second)
}
// normalizeMidnight handles DST regimes where local midnight does not exist.
func normalizeMidnight(candidate time.Time) time.Time {
if candidate.Hour() == 0 {
return candidate
}
if candidate.Hour() > middayHour {
return candidate.Add(time.Duration(hoursPerDay-candidate.Hour()) * time.Hour)
}
return candidate.Add(time.Duration(-candidate.Hour()) * time.Hour)
}
// dayMatches returns true if the schedule's day-of-week and day-of-month
// restrictions are satisfied by the given time.
func dayMatches(s *SpecSchedule, candidate time.Time) bool {
var (
domMatch = hasBit(s.Dom, candidate.Day())
dowMatch = hasBit(s.Dow, int(candidate.Weekday()))
)
if s.Dom&starBit > 0 || s.Dow&starBit > 0 {
return domMatch && dowMatch
}
return domMatch || dowMatch
}
func hasBit(bitset uint64, position int) bool {
return uint64(1)<<position&bitset > 0
}
// fieldString renders a single cron field from its bitset representation.
func fieldString(bitset uint64, bnd bounds) string {
allBits := getBits(bnd.min, bnd.max, 1)
if bitset&^starBit == allBits {
return "*"
}
return enumerateBits(bitset, bnd)
}
// enumerateBits returns a comma-separated list of the set bit positions within
// the given bounds.
func enumerateBits(bitset uint64, bnd bounds) string {
var parts []string
for idx := bnd.min; idx <= bnd.max; idx++ {
if bitset&(uint64(1)<<idx) != 0 {
parts = append(parts, strconv.FormatUint(uint64(idx), 10))
}
}
return strings.Join(parts, ",")
}