-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
347 lines (282 loc) · 6.99 KB
/
parser.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
package cron
import (
"fmt"
"strconv"
"strings"
"time"
)
type LayoutField uint32
const (
Year LayoutField = 1 << iota
Month
Dom
Dow
Hour
Minute
Second
)
var DefaultLayout = []LayoutField{Year, Month, Dom, Dow, Hour, Minute, Second}
type Parser struct {
layout []LayoutField
defaultLoction *time.Location // 缺省时区,解析时未指定时区则以该参数时区解析
}
type SchedTime struct {
Year [2]uint64 // 年
Month uint64 // 月
Dom uint64 // 日
Dow uint64 // 星期,0=星期日
Hour uint64 // 时
Minute uint64 // 分
Second uint64 // 秒
location *time.Location
}
var defaultParser = NewParser()
func NewParser(opts ...ParserOption) *Parser {
p := new(Parser)
p.layout = DefaultLayout
p.defaultLoction = time.Local
for _, opt := range opts {
opt(p)
}
return p
}
// 获取域的限制范围
func (f LayoutField) bounds() (min, max int) {
switch f {
case Year:
min = 1970
max = min + 127
case Month:
min = 1
max = 12
case Dom:
min = 1
max = 31
case Dow:
min = 0
max = 6
case Hour:
min = 0
max = 23
case Minute:
min = 0
max = 59
case Second:
min = 0
max = 59
}
return
}
// 解析时间表达式
func (p *Parser) Parse(exp string) (Schedule, error) {
fields := strings.Fields(exp)
if len(fields) < len(p.layout) {
return nil, fmt.Errorf("%w: invalid number of fields", ErrInvalidExp)
}
st := new(SchedTime)
st.location = p.defaultLoction
for i := range p.layout {
bits, err := parseField(fields[i], p.layout[i])
if err != nil {
return nil, err
}
switch p.layout[i] {
case Year:
st.Year = bits
case Month:
st.Month = bits[0]
case Dom:
st.Dom = bits[0]
case Dow:
st.Dow = bits[0]
case Hour:
st.Hour = bits[0]
case Minute:
st.Minute = bits[0]
case Second:
st.Second = bits[0]
}
}
return st, nil
}
// 解析域
//
// 支持符号:, - * /
func parseField(field string, lf LayoutField) ([2]uint64, error) {
ranges := strings.Split(field, ",")
min, max := lf.bounds()
bits := [2]uint64{}
err := error(nil)
for _, exp := range ranges {
start, end, step := 0, 0, 0
// 分离范围和步进
rangeAndStep := strings.Split(exp, "/")
// 分离范围起始和结束
lowAndHigh := strings.Split(rangeAndStep[0], "-")
if lowAndHigh[0] == "*" {
if len(lowAndHigh) != 1 {
// 不允许出现类似 *-2 的表达式
return [2]uint64{}, fmt.Errorf("%w: %s", ErrInvalidExp, exp)
}
// 若为通配符,则起始和结束分别为最小值和最大值
start = min
end = max
} else {
// 首个字符不是通配符,说明表达式中至少标明了起始值,尝试转换为整型
start, err = strconv.Atoi(lowAndHigh[0])
if err != nil {
return [2]uint64{}, fmt.Errorf("%w: %s", ErrInvalidExp, err)
}
switch len(lowAndHigh) {
case 1: // 长度为1,说明表达式中没有标明结束值
end = start
case 2: // 长度为2,说明表达式中标明了结束值
end, err = strconv.Atoi(lowAndHigh[1])
if err != nil {
return [2]uint64{}, fmt.Errorf("%w: %s", ErrInvalidExp, err)
}
default: // 语法错误
return [2]uint64{}, fmt.Errorf("%w: too many hyphens: %s", ErrInvalidExp, exp)
}
}
switch len(rangeAndStep) {
case 1: // 长度为1,则说明表达式中没有标明步长,默认步长为1
step = 1
case 2: // 长度为2,则说明表达式中含有步长
step, err = strconv.Atoi(rangeAndStep[1])
if err != nil {
return [2]uint64{}, fmt.Errorf("%w: %s", ErrInvalidExp, err)
}
if step <= 0 {
return [2]uint64{}, fmt.Errorf("%w: negative or zero step is not allowed", ErrInvalidExp)
}
// 表达式中没有标明结束值,则将结束值设为最大值
if len(lowAndHigh) == 1 {
end = max
}
default:
return [2]uint64{}, fmt.Errorf("%w: too many slashes: %s", ErrInvalidExp, exp)
}
// 判断参数是否超出范围
if start < min || end > max || start > end {
return [2]uint64{}, fmt.Errorf("%w: out of range: %s", ErrInvalidExp, exp)
}
// 如果当前解析的域为年,则减去1970(以1970为起始年份)
if lf == Year {
start -= 1970
end -= 1970
}
// 为有效位置1
for i := start; i <= end; i += step {
if i < 64 {
bits[0] |= 1 << i
} else {
bits[1] |= 1 << (i - 64)
}
}
}
return bits, nil
}
// 获取下一个有效时间
func (st *SchedTime) Next(t time.Time) time.Time {
// 检查时间域是否匹配,如果匹配,则进行下一个域的匹配。
// 如果域不匹配,则增加该域的值。
// 如果指定了时区,则将给定时间转换为 SchedTime 的时区。
// 保存原始时区,以便找到时间后再转换回来。
// 请注意,未指定时区的 SchedTime 将被视为本地时区。
origLocation := t.Location()
loc := st.location
if loc == time.Local {
loc = t.Location()
}
if st.location != time.Local {
t = t.In(st.location)
}
// 匹配机制未匹配到时,将一直增加时间进行匹配,
// 此值用于限制匹配失败的上限
yearMax := t.Year() + 2
// 设定用于年份判断的位
yearBits := st.Year[0]
delta := t.Year() - 1970
if delta >= 64 {
yearBits = st.Year[1]
delta = delta - 64
}
// 防止以下情况的出现:因时间精度问题,10.001 秒的时候进入该方法,
// 如果直接进行匹配,则第 10 秒的时间就会忽略
t = t.Add(time.Second - time.Duration(t.Nanosecond())*time.Nanosecond)
added := false
LOOP:
// 超过匹配年限则返回零值时间
if t.Year() > yearMax {
return time.Time{}
}
for (1<<delta)&yearBits == 0 {
if !added {
added = true
t = time.Date(t.Year(), time.January, 1, 0, 0, 0, 0, loc)
}
t = t.AddDate(1, 0, 0)
if t.Year() > yearMax {
return time.Time{}
}
}
for (1<<t.Month())&st.Month == 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.Month() == time.January {
goto LOOP
}
}
// 不支持 DST(夏令时)
for !isDayMatch(st, 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.Day() == 1 {
goto LOOP
}
}
for (1<<t.Hour())&st.Hour == 0 {
if !added {
added = true
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, loc)
}
t = t.Add(time.Hour)
if t.Hour() == 0 {
goto LOOP
}
}
for (1<<t.Minute())&st.Minute == 0 {
if !added {
added = true
t = t.Truncate(time.Minute)
}
t = t.Add(time.Minute)
if t.Minute() == 0 {
goto LOOP
}
}
for (1<<t.Second())&st.Second == 0 {
if !added {
added = true
t = t.Truncate(time.Second)
}
t = t.Add(time.Second)
if t.Second() == 0 {
goto LOOP
}
}
return t.In(origLocation)
}
// 判断“日”是否匹配,匹配规则为:必须“日”和“星期”都匹配,则认为匹配
func isDayMatch(st *SchedTime, t time.Time) bool {
domMatch := ((1 << t.Day()) & st.Dom) != 0
dowMatch := ((1 << t.Weekday()) & st.Dow) != 0
return domMatch && dowMatch
}