-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathparser.go
More file actions
235 lines (198 loc) · 5.93 KB
/
Copy pathparser.go
File metadata and controls
235 lines (198 loc) · 5.93 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
package scheduler
import (
"slices"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
// Schedule represents a parsed cron expression.
type Schedule struct {
seconds fieldMatcher // 0-59 (optional, for 6-field format)
minutes fieldMatcher // 0-59
hours fieldMatcher // 0-23
days fieldMatcher // 1-31
months fieldMatcher // 1-12
weekdays fieldMatcher // 0-7 (0 and 7 are Sunday)
hasSecs bool
}
// fieldMatcher determines if a field value matches.
type fieldMatcher interface {
matches(value int) bool
}
// ParseCronExpression parses a cron expression and returns a Schedule.
// Supports both 5-field (minute hour day month weekday) and 6-field (second minute hour day month weekday) formats.
func ParseCronExpression(expr string) (*Schedule, error) {
if expr == "" {
return nil, errors.New("empty cron expression")
}
fields := strings.Fields(expr)
if len(fields) != 5 && len(fields) != 6 {
return nil, errors.Errorf("invalid cron expression: expected 5 or 6 fields, got %d", len(fields))
}
s := &Schedule{
hasSecs: len(fields) == 6,
}
var err error
offset := 0
// Parse seconds (if 6-field format)
if s.hasSecs {
s.seconds, err = parseField(fields[0], 0, 59)
if err != nil {
return nil, errors.Wrap(err, "invalid seconds field")
}
offset = 1
} else {
s.seconds = &exactMatcher{value: 0} // Default to 0 seconds
}
// Parse minutes
s.minutes, err = parseField(fields[offset], 0, 59)
if err != nil {
return nil, errors.Wrap(err, "invalid minutes field")
}
// Parse hours
s.hours, err = parseField(fields[offset+1], 0, 23)
if err != nil {
return nil, errors.Wrap(err, "invalid hours field")
}
// Parse days
s.days, err = parseField(fields[offset+2], 1, 31)
if err != nil {
return nil, errors.Wrap(err, "invalid days field")
}
// Parse months
s.months, err = parseField(fields[offset+3], 1, 12)
if err != nil {
return nil, errors.Wrap(err, "invalid months field")
}
// Parse weekdays (0-7, where both 0 and 7 represent Sunday)
s.weekdays, err = parseField(fields[offset+4], 0, 7)
if err != nil {
return nil, errors.Wrap(err, "invalid weekdays field")
}
return s, nil
}
// Next returns the next time the schedule should run after the given time.
func (s *Schedule) Next(from time.Time) time.Time {
// Start from the next second/minute
if s.hasSecs {
from = from.Add(1 * time.Second).Truncate(time.Second)
} else {
from = from.Add(1 * time.Minute).Truncate(time.Minute)
}
// Cap search at 4 years to prevent infinite loops
maxTime := from.AddDate(4, 0, 0)
for from.Before(maxTime) {
if s.matches(from) {
return from
}
// Advance to next potential match
if s.hasSecs {
from = from.Add(1 * time.Second)
} else {
from = from.Add(1 * time.Minute)
}
}
// Should never reach here with valid cron expressions
return time.Time{}
}
// matches checks if the given time matches the schedule.
func (s *Schedule) matches(t time.Time) bool {
return s.seconds.matches(t.Second()) &&
s.minutes.matches(t.Minute()) &&
s.hours.matches(t.Hour()) &&
s.months.matches(int(t.Month())) &&
(s.days.matches(t.Day()) || s.matchesWeekday(t.Weekday()))
}
// matchesWeekday reports whether the weekday field matches wd. Cron accepts both
// 0 and 7 as Sunday, but time.Weekday only ranges 0 (Sunday) to 6 (Saturday), so
// a Sunday must also be checked against 7 to honor expressions written that way.
func (s *Schedule) matchesWeekday(wd time.Weekday) bool {
if s.weekdays.matches(int(wd)) {
return true
}
return wd == time.Sunday && s.weekdays.matches(7)
}
// parseField parses a single cron field (supports *, ranges, lists, steps).
func parseField(field string, min, max int) (fieldMatcher, error) {
// Wildcard
if field == "*" {
return &wildcardMatcher{}, nil
}
// Step values (*/N)
if strings.HasPrefix(field, "*/") {
step, err := strconv.Atoi(field[2:])
if err != nil || step < 1 || step > max {
return nil, errors.Errorf("invalid step value: %s", field)
}
return &stepMatcher{step: step, min: min, max: max}, nil
}
// List (1,2,3)
if strings.Contains(field, ",") {
parts := strings.Split(field, ",")
values := make([]int, 0, len(parts))
for _, p := range parts {
val, err := strconv.Atoi(strings.TrimSpace(p))
if err != nil || val < min || val > max {
return nil, errors.Errorf("invalid list value: %s", p)
}
values = append(values, val)
}
return &listMatcher{values: values}, nil
}
// Range (1-5)
if strings.Contains(field, "-") {
parts := strings.Split(field, "-")
if len(parts) != 2 {
return nil, errors.Errorf("invalid range: %s", field)
}
start, err1 := strconv.Atoi(strings.TrimSpace(parts[0]))
end, err2 := strconv.Atoi(strings.TrimSpace(parts[1]))
if err1 != nil || err2 != nil || start < min || end > max || start > end {
return nil, errors.Errorf("invalid range: %s", field)
}
return &rangeMatcher{start: start, end: end}, nil
}
// Exact value
val, err := strconv.Atoi(field)
if err != nil || val < min || val > max {
return nil, errors.Errorf("invalid value: %s (must be between %d and %d)", field, min, max)
}
return &exactMatcher{value: val}, nil
}
// wildcardMatcher matches any value.
type wildcardMatcher struct{}
func (*wildcardMatcher) matches(_ int) bool {
return true
}
// exactMatcher matches a specific value.
type exactMatcher struct {
value int
}
func (m *exactMatcher) matches(value int) bool {
return value == m.value
}
// rangeMatcher matches values in a range.
type rangeMatcher struct {
start, end int
}
func (m *rangeMatcher) matches(value int) bool {
return value >= m.start && value <= m.end
}
// listMatcher matches any value in a list.
type listMatcher struct {
values []int
}
func (m *listMatcher) matches(value int) bool {
return slices.Contains(m.values, value)
}
// stepMatcher matches values at regular intervals.
type stepMatcher struct {
step, min, max int
}
func (m *stepMatcher) matches(value int) bool {
if value < m.min || value > m.max {
return false
}
return (value-m.min)%m.step == 0
}