@@ -34,12 +34,12 @@ var defaultParser = cron.NewParser(parserFormat)
34
34
// us_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 1-5")
35
35
// fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))
36
36
// // Output: 2022-04-04T14:30:00Z
37
- func Weekly (spec string ) (* Schedule ,error ) {
38
- if err := validateWeeklySpec (spec );err != nil {
37
+ func Weekly (raw string ) (* Schedule ,error ) {
38
+ if err := validateWeeklySpec (raw );err != nil {
39
39
return nil ,xerrors .Errorf ("validate weekly schedule: %w" ,err )
40
40
}
41
41
42
- specSched ,err := defaultParser .Parse (spec )
42
+ specSched ,err := defaultParser .Parse (raw )
43
43
if err != nil {
44
44
return nil ,xerrors .Errorf ("parse schedule: %w" ,err )
45
45
}
@@ -49,9 +49,18 @@ func Weekly(spec string) (*Schedule, error) {
49
49
return nil ,xerrors .Errorf ("expected *cron.SpecSchedule but got %T" ,specSched )
50
50
}
51
51
52
+ tz := "UTC"
53
+ cron := raw
54
+ if strings .HasPrefix (raw ,"CRON_TZ=" ) {
55
+ tz = strings .TrimPrefix (strings .Fields (raw )[0 ],"CRON_TZ=" )
56
+ cron = strings .Join (strings .Fields (raw )[1 :]," " )
57
+ }
58
+
52
59
cronSched := & Schedule {
53
60
sched :schedule ,
54
- spec :spec ,
61
+ raw :raw ,
62
+ tz :tz ,
63
+ cron :cron ,
55
64
}
56
65
return cronSched ,nil
57
66
}
@@ -61,12 +70,25 @@ func Weekly(spec string) (*Schedule, error) {
61
70
type Schedule struct {
62
71
sched * cron.SpecSchedule
63
72
// XXX: there isn't any nice way for robfig/cron to serialize
64
- spec string
73
+ raw string
74
+ tz string
75
+ cron string
65
76
}
66
77
67
78
// String serializes the schedule to its original human-friendly format.
68
79
func (s Schedule )String ()string {
69
- return s .spec
80
+ return s .raw
81
+ }
82
+
83
+ // Timezone returns the timezone for the schedule.
84
+ func (s Schedule )Timezone ()string {
85
+ return s .tz
86
+ }
87
+
88
+ // Cron returns the cron spec for the schedule with the leading CRON_TZ
89
+ // stripped, if present.
90
+ func (s Schedule )Cron ()string {
91
+ return s .cron
70
92
}
71
93
72
94
// Next returns the next time in the schedule relative to t.