-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
201 lines (159 loc) · 3.72 KB
/
schedule.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
package cronexpr
import (
"time"
)
type (
Schedule struct {
Minute, Hour, Dom, Month, Dow bitset
Location *time.Location
}
)
// Next returns the next time matched with the expression.
func (s *Schedule) Next(t time.Time) time.Time {
loc := time.UTC
if s.Location != nil {
loc = s.Location
}
origLoc := t.Location()
t = t.In(loc)
added := false
// Start at the earliest possible time (the upcoming second).
t = t.Add(1*time.Minute - time.Duration(t.Nanosecond())*time.Nanosecond)
yearLimit := t.Year() + 5
L:
if t.Year() > yearLimit {
return time.Time{}
}
// Find the first applicable month.
// If it's this month, then do nothing.
year := t.Year()
for 1<<uint(t.Month())&s.Month == 0 {
// If we have to add a month, reset the other parts to 0.
if !added {
added = true
t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, loc)
}
t = t.AddDate(0, 1, 0)
if t.Year() != year {
goto L
}
}
// Now get a day in that month.
month := t.Month()
for !dayMatches(s, t) {
if !added {
added = true
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
}
t = t.AddDate(0, 0, 1)
if t.Month() != month {
goto L
}
}
day := t.Day()
for 1<<uint(t.Hour())&s.Hour == 0 {
if !added {
added = true
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, loc)
}
t = t.Add(1 * time.Hour)
if t.Day() != day {
goto L
}
}
hour := t.Hour()
for 1<<uint(t.Minute())&s.Minute == 0 {
if !added {
added = true
t = t.Truncate(time.Minute)
}
t = t.Add(1 * time.Minute)
if t.Hour() != hour {
goto L
}
}
return t.
Truncate(time.Minute).
In(origLoc)
}
// Next returns the previous time matched with the expression.
func (s *Schedule) Prev(t time.Time) time.Time {
loc := time.UTC
if s.Location != nil {
loc = s.Location
}
origLoc := t.Location()
t = t.In(loc)
subtracted := false
// Start at the earliest possible time (the upcoming second).
t = t.Add(-1*time.Minute + time.Duration(t.Nanosecond())*time.Nanosecond)
yearLimit := t.Year() - 5
L:
if t.Year() < yearLimit {
return time.Time{}
}
year := t.Year()
for 1<<uint(t.Month())&s.Month == 0 {
// If we have to add a month, reset with the next month before.
if !subtracted {
subtracted = true
t = time.Date(t.Year(), t.Month()+1, 0, 23, 59, 0, 0, loc)
}
// Change the time into the last day of the previous month.
// Note that AddDate(0, -1, 0) has a bug by the normalization.
// E.g) time.Date(2021, 6, 0, 23, 59, 59, 0, time.UTC).AddDate(0, -1, 0)
t = time.Date(t.Year(), t.Month(), 0, 23, 59, 0, 0, loc)
if t.Year() != year {
goto L
}
}
// Now get a day in that month.
month := t.Month()
for !dayMatches(s, t) {
if !subtracted {
subtracted = true
t = time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 0, 0, loc)
}
t = t.AddDate(0, 0, -1)
if t.Month() != month {
goto L
}
}
day := t.Day()
for 1<<uint(t.Hour())&s.Hour == 0 {
if !subtracted {
subtracted = true
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 59, 0, 0, loc)
}
t = t.Add(-1 * time.Hour)
if t.Day() != day {
goto L
}
}
hour := t.Hour()
for 1<<uint(t.Minute())&s.Minute == 0 {
if !subtracted {
subtracted = true
t = t.Truncate(-time.Minute)
}
t = t.Add(-1 * time.Minute)
if t.Hour() != hour {
goto L
}
}
return t.
Truncate(time.Minute).
In(origLoc)
}
// dayMatches returns true if the schedule's day-of-week and day-of-month
// restrictions are satisfied by the given time.
func dayMatches(s *Schedule, t time.Time) bool {
var (
domMatch bool = 1<<uint(t.Day())&s.Dom > 0
dowMatch bool = 1<<uint(t.Weekday())&s.Dow > 0
)
if s.Dom&bitsetStar > 0 || s.Dow&bitsetStar > 0 {
return domMatch && dowMatch
}
return domMatch || dowMatch
}