1
- // package crontab provides utilities for parsing and deserializing
1
+ // package schedule provides utilities for parsing and deserializing
2
2
// cron-style expressions.
3
- package crontab
3
+ package schedule
4
4
5
5
import (
6
6
"time"
@@ -10,21 +10,26 @@ import (
10
10
)
11
11
12
12
// For the purposes of this library, we only need minute, hour, and
13
- //day-of-week.
14
- const parserFormat = cron .Minute | cron .Hour | cron .Dow
13
+ // day-of-week.
14
+ const parserFormatWeekly = cron .Minute | cron .Hour | cron .Dow
15
15
16
- var defaultParser = cron .NewParser (parserFormat )
16
+ var defaultParser = cron .NewParser (parserFormatWeekly )
17
17
18
- // Parse parses a WeeklySchedule from spec.
18
+ // Weekly parses a Schedule from spec scoped to a recurring weekly event.
19
+ // Spec consists of the following space-delimited fields, in the following order:
20
+ // - timezone e.g. CRON_TZ=US/Central (optional)
21
+ // - minutes of hour e.g. 30 (required)
22
+ // - hour of day e.g. 9 (required)
23
+ // - day of week e.g. 1 (required)
19
24
//
20
25
// Example Usage:
21
- // local_sched, _ := cron.Parse ("59 23 *")
26
+ // local_sched, _ := schedule.Weekly ("59 23 *")
22
27
// fmt.Println(sched.Next(time.Now().Format(time.RFC3339)))
23
28
// // Output: 2022-04-04T23:59:00Z
24
- // us_sched, _ := cron.Parse ("CRON_TZ=US/Central 30 9 1-5")
29
+ // us_sched, _ := schedule.Weekly ("CRON_TZ=US/Central 30 9 1-5")
25
30
// fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))
26
31
// // Output: 2022-04-04T14:30:00Z
27
- func Parse (spec string ) (* WeeklySchedule , error ) {
32
+ func Weekly (spec string ) (* Schedule , error ) {
28
33
specSched , err := defaultParser .Parse (spec )
29
34
if err != nil {
30
35
return nil , xerrors .Errorf ("parse schedule: %w" , err )
@@ -35,27 +40,27 @@ func Parse(spec string) (*WeeklySchedule, error) {
35
40
return nil , xerrors .Errorf ("expected *cron.SpecSchedule but got %T" , specSched )
36
41
}
37
42
38
- cronSched := & WeeklySchedule {
43
+ cronSched := & Schedule {
39
44
sched : schedule ,
40
45
spec : spec ,
41
46
}
42
47
return cronSched , nil
43
48
}
44
49
45
- // WeeklySchedule represents a weekly cron schedule.
50
+ // Schedule represents a cron schedule.
46
51
// It's essentially a thin wrapper for robfig/cron/v3 that implements Stringer.
47
- type WeeklySchedule struct {
52
+ type Schedule struct {
48
53
sched * cron.SpecSchedule
49
54
// XXX: there isn't any nice way for robfig/cron to serialize
50
55
spec string
51
56
}
52
57
53
58
// String serializes the schedule to its original human-friendly format.
54
- func (s WeeklySchedule ) String () string {
59
+ func (s Schedule ) String () string {
55
60
return s .spec
56
61
}
57
62
58
63
// Next returns the next time in the schedule relative to t.
59
- func (s WeeklySchedule ) Next (t time.Time ) time.Time {
64
+ func (s Schedule ) Next (t time.Time ) time.Time {
60
65
return s .sched .Next (t )
61
66
}
0 commit comments