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