-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-unit.go
278 lines (221 loc) · 7.02 KB
/
time-unit.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
package gocron
import (
"slices"
"time"
)
// secTimeUnit is a time unit implementation for the seconds field in a Cron
// expression.
type secTimeUnit []timeSet
// Next implements TimeUnit.
func (u secTimeUnit) Next(next time.Time) (time.Time, bool) {
candidate, ok := searchNextCandidate(u, next, time.Time.Second, setSeconds)
if ok {
return candidate, true
}
return setMinutes(next, next.Minute()+1), false
}
func (u secTimeUnit) Previous(before time.Time) (time.Time, bool) {
candidate, ok := searchPrevCandidate(u, before, time.Time.Second, setSeconds)
if ok {
return candidate, true
}
// Move the previous minute and set seconds to 59.
return setSeconds(before, 0).Add(-time.Second), false
}
// minTimeUnit is a time unit implementation for the minutes field in a Cron
// expression.
type minTimeUnit []timeSet
// Next implements TimeUnit.
func (u minTimeUnit) Next(next time.Time) (time.Time, bool) {
candidate, ok := searchNextCandidate(u, next, time.Time.Minute, setMinutes)
if ok {
return candidate, true
}
return setHours(next, next.Hour()+1), false
}
func (u minTimeUnit) Previous(before time.Time) (time.Time, bool) {
candidate, ok := searchPrevCandidate(u, before, time.Time.Minute, setMinutes)
if ok {
return candidate, true
}
return setMinutes(before, 0).Add(-time.Second), false
}
// hourTimeUnit is a time unit implementation for the hours field in a Cron
// expression.
type hourTimeUnit []timeSet
// Next implements TimeUnit.
func (u hourTimeUnit) Next(next time.Time) (time.Time, bool) {
candidate, ok := searchNextCandidate(u, next, time.Time.Hour, setHours)
if ok {
return candidate, true
}
return setDays(next, next.Day()+1), false
}
func (u hourTimeUnit) Previous(before time.Time) (time.Time, bool) {
candidate, ok := searchPrevCandidate(u, before, time.Time.Hour, setHours)
if ok {
return candidate, true
}
return setHours(before, 0).Add(-time.Second), false
}
// dayTimeUnit is a time unit implementation for the days field in a Cron
// expression.
type dayTimeUnit []timeSet
// Next implements TimeUnit.
func (u dayTimeUnit) Next(next time.Time) (time.Time, bool) {
if len(u) == 0 {
return next, true
}
for _, set := range u {
day, direction := set.NearestCandidate(next, next.Day(), true)
switch direction {
case hit:
return next, true
case inRange:
if next = setDays(next, day); next.Day() == day {
// Day fits inside the current month.
return next, true
}
// When the day is higher than what the current month supports (e.g.
// 30 for February).
return setMonths(next, next.Month()), false
}
}
return setMonths(next, next.Month()+1), false
}
func (u dayTimeUnit) Previous(before time.Time) (time.Time, bool) {
candidate, ok := searchPrevCandidate(u, before, time.Time.Day, setDays)
if ok {
return candidate, true
}
return setDays(before, 1).Add(-time.Second), false
}
// monthTimeUnit is a time unit implementation for the months field in a Cron
// expression.
type monthTimeUnit []timeSet
// Next implements TimeUnit.
func (u monthTimeUnit) Next(next time.Time) (time.Time, bool) {
candidate, ok := searchNextCandidate(u, next, time.Time.Month, setMonths)
if ok {
return candidate, true
}
return setYears(next, next.Year()+1), false
}
func (u monthTimeUnit) Previous(before time.Time) (time.Time, bool) {
candidate, ok := searchPrevCandidate(u, before, time.Time.Month, setMonths)
if ok {
return candidate, true
}
return setMonths(before, 1).Add(-time.Second), false
}
// weekdayTimeUnit is a time unit implementation for the week days field in a
// Cron expression.
type weekdayTimeUnit []timeSet
// Next implements TimeUnit.
func (u weekdayTimeUnit) Next(next time.Time) (time.Time, bool) {
if len(u) == 0 {
return next, true
}
for _, set := range u {
if _, direction := set.NearestCandidate(next, int(next.Weekday()), true); direction == hit {
return next, true
}
}
return setDays(next, next.Day()+1), false
}
func (u weekdayTimeUnit) Previous(before time.Time) (time.Time, bool) {
if len(u) == 0 {
return before, true
}
for i := len(u) - 1; i >= 0; i-- {
if _, direction := u[i].NearestCandidate(before, int(before.Weekday()), false); direction == hit {
return before, true
}
}
return setDays(before, before.Day()-1), false
}
type yearTimeUnit []timeSet
func (u yearTimeUnit) Next(next time.Time) (time.Time, bool) {
candidate, ok := searchNextCandidate(u, next, time.Time.Year, setYears)
if ok {
return candidate, true
}
return setYears(next, rangeMaxYear+1), false
}
func (u yearTimeUnit) Previous(before time.Time) (time.Time, bool) {
candidate, ok := searchPrevCandidate(u, before, time.Time.Year, setYears)
if ok {
return candidate, true
}
return setYears(before, rangeMinYear), false
}
type getterFunc[T int | time.Month] func(time.Time) T
type setterFunc[T int | time.Month] func(time.Time, T) time.Time
// searchNextCandidate returns the more appropriate candidate from the time unit
// if any, otherwise it returns false.
func searchNextCandidate[T int | time.Month](
in []timeSet, next time.Time,
getter getterFunc[T],
setter setterFunc[T],
) (time.Time, bool) {
return searchCandidate[T](in, next, getter, setter, true)
}
// searchPrevCandidate returns the more appropriate candidate from the time unit
// if any, otherwise it returns false.
func searchPrevCandidate[T int | time.Month](
in []timeSet,
before time.Time,
getter getterFunc[T],
setter setterFunc[T],
) (time.Time, bool) {
return searchCandidate(in, before, getter, setter, false)
}
func searchCandidate[T int | time.Month](
in []timeSet,
t time.Time,
getter getterFunc[T],
setter setterFunc[T],
forwards bool,
) (time.Time, bool) {
if len(in) == 0 {
return t, true
}
var candidates []int
for _, set := range in {
value, direction := set.NearestCandidate(t, int(getter(t)), forwards)
switch direction {
case hit:
return t, true
case inRange:
candidates = append(candidates, value)
}
}
if len(candidates) > 0 {
if forwards {
// When iterating forwards, it uses the smallest candidate.
return setter(t, T(slices.Min(candidates))), true
}
// When iterating backwards, it uses the biggest candidate annd sets all
// the lower fields too their max value.
return setter(t, T(slices.Max(candidates))+1).Add(-time.Second), true
}
return time.Time{}, false
}
func setSeconds(t time.Time, secs int) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), secs, 0, t.Location())
}
func setMinutes(t time.Time, minutes int) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), minutes, 0, 0, t.Location())
}
func setHours(t time.Time, hours int) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), hours, 0, 0, 0, t.Location())
}
func setDays(t time.Time, days int) time.Time {
return time.Date(t.Year(), t.Month(), days, 0, 0, 0, 0, t.Location())
}
func setMonths(t time.Time, months time.Month) time.Time {
return time.Date(t.Year(), months, 1, 0, 0, 0, 0, t.Location())
}
func setYears(t time.Time, years int) time.Time {
return time.Date(years, 1, 1, 0, 0, 0, 0, t.Location())
}