-
Notifications
You must be signed in to change notification settings - Fork 6
/
cron.go
633 lines (570 loc) · 17 KB
/
cron.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
package cron
import (
"fmt"
"regexp"
"strconv"
"strings"
)
var (
specialChars = []rune{'/', '-', ',', '*'}
weekdaysNumberRegex = regexp.MustCompile(`(\d{1,2}w)|(w\d{1,2})`)
lastDayOffsetRegex = regexp.MustCompile(`l-(\d{1,2})`)
)
type (
// ExpressionDescriptor represents the CRON expression descriptor.
ExpressionDescriptor struct {
isVerbose bool
isDOWStartsAtOne bool
is24HourTimeFormat bool
logger Logger
parser Parser
locales map[LocaleType]Locale
}
// Logger is the logging interface for expression descriptor.
Logger interface {
Printf(format string, v ...interface{})
}
// Option allows to configure expression descriptor.
Option func(exprDesc *ExpressionDescriptor)
)
// NewDescriptor returns a new CRON expression descriptor based on the list of options.
// If no options provided, a default CRON expression descriptor will be returned instead.
// By default, English (Locale_en) will always be loaded.
func NewDescriptor(options ...Option) (exprDesc *ExpressionDescriptor, err error) {
exprDesc = &ExpressionDescriptor{}
for _, option := range options {
option(exprDesc)
}
// Init defaults
if exprDesc.parser == nil {
exprDesc.parser = &cronParser{
isDOWStartsAtOne: exprDesc.isDOWStartsAtOne,
}
}
// Always load EN locale so we can fallback to it
if exprDesc.locales == nil {
exprDesc.locales = make(map[LocaleType]Locale)
}
if _, ok := exprDesc.locales[Locale_en]; !ok {
localeLoader, err := NewLocaleLoaders(Locale_en)
if err != nil {
return nil, fmt.Errorf("failed to init default locale EN: %w", err)
}
exprDesc.locales[Locale_en] = localeLoader[0]
}
return exprDesc, nil
}
// ToDescription converts the CRON expression to the human readable string in specified locale.
// If the specified locale had not been loaded by the CRON expression descriptor, the result will be
// returned in English (Locale_en) by default.
//
// To configure supported locales of the CRON expression descriptor, please see the SetLocales() option.
func (e *ExpressionDescriptor) ToDescription(expr string, loc LocaleType) (desc string, err error) {
var exprParts []string
if exprParts, err = e.parser.Parse(expr); err != nil {
return "", fmt.Errorf("failed to parse CRON expression: %w", err)
}
locale := e.getLocale(loc)
var timeSegment = e.getTimeOfDayDescription(exprParts, locale)
var dayOfMonthDesc = e.getDayOfMonthDescription(exprParts, locale)
var monthDesc = e.getMonthDescription(exprParts, locale)
var dayOfWeekDesc = e.getDayOfWeekDescription(exprParts, locale)
var yearDesc = e.getYearDescription(exprParts, locale)
desc = timeSegment + dayOfMonthDesc + dayOfWeekDesc + monthDesc + yearDesc
desc = transformVerbosity(desc, locale, e.isVerbose)
desc = strings.Join(strings.Fields(desc), " ")
desc = strings.Replace(desc, " ,", ",", -1)
runes := []rune(desc)
runes[0] = []rune(strings.ToUpper(string(runes[0])))[0]
return string(runes), nil
}
func (e *ExpressionDescriptor) log(format string, v ...interface{}) {
if e.logger == nil {
return
}
e.logger.Printf(format, v...)
}
func (e *ExpressionDescriptor) verbose(format string, v ...interface{}) {
if !e.isVerbose || e.logger == nil {
return
}
e.logger.Printf(format, v...)
}
func (e *ExpressionDescriptor) getTimeOfDayDescription(exprParts []string, locale Locale) string {
second := exprParts[0]
minute := exprParts[1]
hour := exprParts[2]
var desc string
if !containsAny(second, specialChars) && !containsAny(minute, specialChars) && !containsAny(hour, specialChars) {
// specific time of day (i.e. 10:14:00)
desc += locale.GetString(atSpace) + formatTime(hour, minute, second, locale, e.is24HourTimeFormat)
} else if second == "" &&
strings.Index(minute, "-") > -1 &&
!(strings.Index(minute, ",") > -1) &&
!(strings.Index(minute, "/") > -1) &&
!containsAny(hour, specialChars) {
// minute range in single hour (i.e. 0-10 11)
idx := strings.Index(minute, "-")
desc += sprintf(locale.GetString(everyMinuteBetweenX0AndX1),
formatTime(hour, minute[:idx], "", locale, e.is24HourTimeFormat),
formatTime(hour, minute[idx+1:], "", locale, e.is24HourTimeFormat))
} else if second == "" &&
strings.Index(hour, ",") > -1 &&
strings.Index(hour, "-") == -1 &&
strings.Index(hour, "/") == -1 &&
!containsAny(minute, specialChars) {
// hours list with single minute (i.e. 30 6,14,16)
hourParts := strings.Split(hour, ",")
desc += locale.GetString(at)
for i, p := range hourParts {
desc += " "
desc += formatTime(p, minute, "", locale, e.is24HourTimeFormat)
if i < len(hourParts)-2 {
desc += ", "
}
if i == len(hourParts)-2 {
desc += locale.GetString(spaceAnd)
}
}
} else {
// default time description
secondDesc := e.getSecondsDescription(exprParts, locale)
minuteDesc := e.getMinutesDescription(exprParts, locale)
hourDesc := e.getHoursDescription(exprParts, locale)
desc += secondDesc
if desc != "" && minuteDesc != "" {
desc += ", "
}
desc += minuteDesc
if desc != "" && hourDesc != "" {
desc += ", "
}
desc += hourDesc
}
return desc
}
func (e *ExpressionDescriptor) getSecondsDescription(exprParts []string, locale Locale) string {
desc := getSegmentDescription(
exprParts[0],
locale.GetString(everySecond),
func(s string) string {
return s
},
func(s string) string {
return sprintf(locale.GetString(everyX0Seconds), s)
},
func(s string) string {
return locale.GetString(secondsX0ThroughX1PastTheMinute)
},
func(s string) string {
if s == "" {
return ""
}
sInt, _ := strconv.Atoi(s)
if sInt < 20 {
return locale.GetString(atX0SecondsPastTheMinute)
}
if msg := locale.GetString(atX0SecondsPastTheMinuteGt20); msg != "" {
return msg
}
return locale.GetString(atX0SecondsPastTheMinute)
},
locale,
)
return desc
}
func (e *ExpressionDescriptor) getDayOfMonthDescription(exprParts []string, locale Locale) string {
desc := ""
dom := exprParts[3]
switch dom {
case "l":
desc = locale.GetString(commaOnTheLastDayOfTheMonth)
case "wl":
fallthrough
case "lw":
desc = locale.GetString(commaOnTheLastWeekdayOfTheMonth)
default:
weekdaysNumberMatches := weekdaysNumberRegex.FindAllString(dom, -1)
if len(weekdaysNumberMatches) > 0 {
dayNumber, _ := strconv.Atoi(strings.Replace(weekdaysNumberMatches[0], "w", "", -1))
dayStr := ""
if dayNumber == 1 {
dayStr = locale.GetString(firstWeekday)
} else {
dayStr = sprintf(locale.GetString(weekdayNearestDayX0), strconv.Itoa(dayNumber))
}
desc = sprintf(locale.GetString(commaOnTheX0OfTheMonth), dayStr)
break
}
// Handle "last day offset" (i.e. L-5: "5 days before the last day of the month")
lastDayOffsetMatches := lastDayOffsetRegex.FindAllStringSubmatch(dom, -1)
if len(lastDayOffsetMatches) > 0 {
desc = sprintf(locale.GetString(commaDaysBeforeTheLastDayOfTheMonth), lastDayOffsetMatches[0][1])
break
}
// * dayOfMonth and dayOfWeek specified so use dayOfWeek verbiage instead
if dom == "*" && exprParts[5] != "*" {
return ""
}
desc = getSegmentDescription(
dom,
locale.GetString(commaEveryDay),
func(s string) string {
if s == "l" {
return locale.GetString(lastDay)
}
if msg := locale.GetString(dayX0); msg != "" {
return sprintf(msg, s)
}
return s
},
func(s string) string {
if s == "1" {
return locale.GetString(commaEveryDay)
}
return locale.GetString(commaEveryX0Days)
},
func(s string) string {
return locale.GetString(commaBetweenDayX0AndX1OfTheMonth)
},
func(s string) string {
return locale.GetString(commaOnDayX0OfTheMonth)
},
locale,
)
break
}
return desc
}
func (e *ExpressionDescriptor) getMonthDescription(exprParts []string, locale Locale) string {
monthNames := locale.GetSlice(monthsOfTheYear)
desc := getSegmentDescription(
exprParts[4],
"",
func(s string) string {
sInt, _ := strconv.Atoi(s)
return monthNames[sInt-1]
},
func(s string) string {
sInt, _ := strconv.Atoi(s)
if sInt == 1 {
return "" // rather than "every 1 months" just return empty string
}
return sprintf(locale.GetString(commaEveryX0Months), s)
},
func(s string) string {
if msg := locale.GetString(commaMonthX0ThroughMonthX1); msg != "" {
return msg
}
return locale.GetString(commaX0ThroughX1)
},
func(s string) string {
if msg := locale.GetString(commaOnlyInMonthX0); msg != "" {
return msg
}
return locale.GetString(commaOnlyInX0)
},
locale,
)
return desc
}
func (e *ExpressionDescriptor) getDayOfWeekDescription(exprParts []string, locale Locale) string {
daysOfWeekNames := locale.GetSlice(daysOfTheWeek)
if exprParts[5] == "*" {
// DOW is specified as * so we will not generate a description and defer to DOM part.
// Otherwise, we could get a contradiction like "on day 1 of the month, every day"
// or a dupe description like "every day, every day".
return ""
}
desc := getSegmentDescription(
exprParts[5],
locale.GetString(commaEveryDay),
func(s string) string {
exp := s
if idx := strings.Index(s, "#"); idx > -1 {
exp = s[:idx]
} else if strings.Index(s, "l") > -1 {
exp = strings.Replace(exp, "l", "", -1)
}
expInt, _ := strconv.Atoi(exp)
return daysOfWeekNames[expInt]
},
func(s string) string {
sInt, _ := strconv.Atoi(s)
if sInt == 1 {
return "" // rather than "every 1 days" just return empty string
}
return sprintf(locale.GetString(commaEveryX0DaysOfTheWeek), s)
},
func(s string) string {
return locale.GetString(commaX0ThroughX1)
},
func(s string) string {
format := ""
if idx := strings.Index(s, "#"); idx > -1 {
dowOfMonthNum := s[idx+1:]
dowOfMonthDesc := ""
switch dowOfMonthNum {
case "1":
dowOfMonthDesc = locale.GetString(first)
case "2":
dowOfMonthDesc = locale.GetString(second)
case "3":
dowOfMonthDesc = locale.GetString(third)
case "4":
dowOfMonthDesc = locale.GetString(fourth)
case "5":
dowOfMonthDesc = locale.GetString(fifth)
}
format = locale.GetString(commaOnThe) + dowOfMonthDesc + locale.GetString(spaceX0OfTheMonth)
} else if strings.Index(s, "l") > -1 {
format = locale.GetString(commaOnTheLastX0OfTheMonth)
} else {
// If both DOM and DOW are specified, the cron will execute at either time.
format = locale.GetString(commaAndOnX0)
if exprParts[3] == "*" {
format = locale.GetString(commaOnlyOnX0)
}
}
return format
},
locale,
)
return desc
}
func (e *ExpressionDescriptor) getYearDescription(exprParts []string, locale Locale) string {
desc := getSegmentDescription(
exprParts[6],
"",
func(s string) string {
return s // Note: Not handle the cases when year is not in full, e.g.: 93, 99
},
func(s string) string {
return sprintf(locale.GetString(commaEveryX0Years), s)
},
func(s string) string {
if msg := locale.GetString(commaYearX0ThroughYearX1); msg != "" {
return msg
}
return locale.GetString(commaX0ThroughX1)
},
func(s string) string {
if msg := locale.GetString(commaOnlyInYearX0); msg != "" {
return msg
}
return locale.GetString(commaOnlyInX0)
},
locale,
)
return desc
}
func (e *ExpressionDescriptor) getLocale(loc LocaleType) Locale {
v, ok := e.locales[loc]
if !ok {
return e.locales[Locale_en] // Fall back to default
}
return v
}
func containsAny(s string, matches []rune) bool {
runes := []rune(s)
for _, r := range runes {
for _, c := range matches {
if r == c {
return true
}
}
}
return false
}
func formatTime(hour, minute, second string, locale Locale, isUse24HourTimeFormat bool) string {
hourInt, _ := strconv.Atoi(hour)
minuteInt, _ := strconv.Atoi(minute)
period := ""
isPeriodBeforeTime := false
if !isUse24HourTimeFormat {
isPeriodBeforeTime = locale.GetBool(confSetPeriodBeforeTime)
period = getPeriod(hourInt, locale)
if !isPeriodBeforeTime {
period = " " + period
}
if hourInt > 12 {
hourInt -= 12
}
if hourInt == 0 {
hourInt = 12
}
}
hour = fmt.Sprintf("%02d", hourInt)
minute = fmt.Sprintf("%02d", minuteInt)
ret := ""
if isPeriodBeforeTime {
ret += period + " "
}
ret += hour + ":" + minute
if second != "" {
secondInt, _ := strconv.Atoi(second)
second = fmt.Sprintf("%02d", secondInt)
ret += ":" + second
}
if !isPeriodBeforeTime {
ret += period
}
return ret
}
func getPeriod(hour int, locale Locale) string {
if hour >= 12 {
period := locale.GetString(pm)
if period == "" {
return "PM"
}
return period
}
period := locale.GetString(am)
if period == "" {
return "AM"
}
return period
}
type getStringFunc func(string) string
func getSegmentDescription(expr, allDesc string,
getSingleItemDescription,
getIntervalDescriptionFormat,
getBetweenDescriptionFormat,
getDescriptionFormat getStringFunc,
locale Locale) string {
desc := ""
if expr == "" {
desc = ""
} else if expr == "*" {
desc = allDesc
} else if !containsAny(expr, []rune{'/', '-', ','}) {
desc = sprintf(getDescriptionFormat(expr), getSingleItemDescription(expr))
} else if strings.Index(expr, "/") > -1 {
segments := strings.Split(expr, "/")
desc = sprintf(getIntervalDescriptionFormat(segments[1]), segments[1])
// interval contains 'between' piece (i.e. 2-59/3 )
if strings.Index(segments[0], "-") > -1 {
betweenDesc := generateBetweenSegmentDescription(segments[0], getBetweenDescriptionFormat, getSingleItemDescription)
if strings.Index(betweenDesc, ", ") != 0 {
desc += ", "
}
desc += betweenDesc
} else if !containsAny(segments[0], []rune{'*', ','}) {
rangeDesc := sprintf(getDescriptionFormat(segments[0]), getSingleItemDescription(segments[0]))
rangeDesc = strings.Replace(rangeDesc, ", ", "", 1)
desc += sprintf(locale.GetString(commaStartingX0), rangeDesc)
}
} else if strings.Index(expr, ",") > -1 {
segments := strings.Split(expr, ",")
contentDesc := ""
for i, seg := range segments {
if i > 0 && len(segments) > 2 {
contentDesc += ","
if i < len(segments)-1 {
contentDesc += " "
}
}
if i > 0 && len(segments) > 1 && (i == len(segments)-1 || len(segments) == 2) {
contentDesc += locale.GetString(spaceAnd) + " "
}
getBetweenFmtFunc := func(s string) string { return locale.GetString(commaX0ThroughX1) }
if strings.Index(seg, "-") > -1 {
betweenDesc := generateBetweenSegmentDescription(
seg,
getBetweenFmtFunc,
getSingleItemDescription,
)
betweenDesc = strings.Replace(betweenDesc, ", ", "", 1)
contentDesc += betweenDesc
} else {
contentDesc += getSingleItemDescription(seg)
}
}
desc += sprintf(getDescriptionFormat(expr), contentDesc)
} else if strings.Index(expr, "-") > -1 {
desc = generateBetweenSegmentDescription(
expr,
getBetweenDescriptionFormat,
getSingleItemDescription,
)
}
return desc
}
func generateBetweenSegmentDescription(betweenDesc string, getBetweenDescriptionFormat, getSingleItemDescription getStringFunc) string {
desc := ""
betweenSegments := strings.Split(betweenDesc, "-")
seg1 := getSingleItemDescription(betweenSegments[0])
seg2 := getSingleItemDescription(betweenSegments[1])
seg2 = strings.Replace(seg2, ":00", ":59", 1)
desc += sprintf(getBetweenDescriptionFormat(betweenDesc), seg1, seg2)
return desc
}
func (e *ExpressionDescriptor) getMinutesDescription(exprParts []string, locale Locale) string {
second := exprParts[0]
hour := exprParts[2]
desc := getSegmentDescription(
exprParts[1],
locale.GetString(everyMinute),
func(s string) string {
return s
},
func(s string) string {
return sprintf(locale.GetString(everyX0Minutes), s)
},
func(s string) string {
return locale.GetString(minutesX0ThroughX1PastTheHour)
},
func(s string) string {
if s == "0" && strings.Index(hour, "/") == -1 && second == "" {
return locale.GetString(everyHour)
}
sInt, _ := strconv.Atoi(s)
if sInt < 20 {
return locale.GetString(atX0MinutesPastTheHour)
}
if msg := locale.GetString(atX0MinutesPastTheHourGt20); msg != "" {
return msg
}
return locale.GetString(atX0MinutesPastTheHour)
},
locale)
return desc
}
func (e *ExpressionDescriptor) getHoursDescription(exprParts []string, locale Locale) string {
desc := getSegmentDescription(
exprParts[2],
locale.GetString(everyHour),
func(s string) string {
return formatTime(s, "0", "", locale, e.is24HourTimeFormat)
},
func(s string) string {
return sprintf(locale.GetString(everyX0Hours), s)
},
func(s string) string {
return locale.GetString(betweenX0AndX1)
},
func(s string) string {
return locale.GetString(atX0)
},
locale,
)
return desc
}
func transformVerbosity(desc string, locale Locale, isVerbose bool) string {
if isVerbose {
return desc
}
eMinute := locale.GetString(everyMinute)
eHour := locale.GetString(everyHour)
eDay := locale.GetString(commaEveryDay)
desc = strings.Replace(desc, ", "+eMinute, "", -1)
desc = strings.Replace(desc, ", "+eHour, "", -1)
desc = strings.Replace(desc, eDay, "", -1)
return desc
}
func sprintf(tmpl string, values ...string) string {
for _, v := range values {
tmpl = strings.Replace(tmpl, "%s", v, 1)
}
return tmpl
}