Skip to content

Commit 5f7cf87

Browse files
committed
schedule: add methods to fetch tz and cron string separately
1 parent 50ad2f8 commit 5f7cf87

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

coderd/autobuild/schedule/schedule.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ var defaultParser = cron.NewParser(parserFormat)
3434
// us_sched, _ := schedule.Weekly("CRON_TZ=US/Central 30 9 1-5")
3535
// fmt.Println(sched.Next(time.Now()).Format(time.RFC3339))
3636
// // 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 {
3939
return nil, xerrors.Errorf("validate weekly schedule: %w", err)
4040
}
4141

42-
specSched, err := defaultParser.Parse(spec)
42+
specSched, err := defaultParser.Parse(raw)
4343
if err != nil {
4444
return nil, xerrors.Errorf("parse schedule: %w", err)
4545
}
@@ -49,9 +49,18 @@ func Weekly(spec string) (*Schedule, error) {
4949
return nil, xerrors.Errorf("expected *cron.SpecSchedule but got %T", specSched)
5050
}
5151

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+
5259
cronSched := &Schedule{
5360
sched: schedule,
54-
spec: spec,
61+
raw: raw,
62+
tz: tz,
63+
cron: cron,
5564
}
5665
return cronSched, nil
5766
}
@@ -61,12 +70,25 @@ func Weekly(spec string) (*Schedule, error) {
6170
type Schedule struct {
6271
sched *cron.SpecSchedule
6372
// XXX: there isn't any nice way for robfig/cron to serialize
64-
spec string
73+
raw string
74+
tz string
75+
cron string
6576
}
6677

6778
// String serializes the schedule to its original human-friendly format.
6879
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
7092
}
7193

7294
// Next returns the next time in the schedule relative to t.

coderd/autobuild/schedule/schedule_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,26 @@ func Test_Weekly(t *testing.T) {
1717
at time.Time
1818
expectedNext time.Time
1919
expectedError string
20+
expectedCron string
21+
expectedTz string
2022
}{
2123
{
2224
name: "with timezone",
2325
spec: "CRON_TZ=US/Central 30 9 * * 1-5",
2426
at: time.Date(2022, 4, 1, 14, 29, 0, 0, time.UTC),
2527
expectedNext: time.Date(2022, 4, 1, 14, 30, 0, 0, time.UTC),
2628
expectedError: "",
29+
expectedCron: "30 9 * * 1-5",
30+
expectedTz: "US/Central",
2731
},
2832
{
2933
name: "without timezone",
3034
spec: "30 9 * * 1-5",
3135
at: time.Date(2022, 4, 1, 9, 29, 0, 0, time.Local),
3236
expectedNext: time.Date(2022, 4, 1, 9, 30, 0, 0, time.Local),
3337
expectedError: "",
38+
expectedCron: "30 9 * * 1-5",
39+
expectedTz: "UTC",
3440
},
3541
{
3642
name: "invalid schedule",
@@ -86,6 +92,8 @@ func Test_Weekly(t *testing.T) {
8692
require.NoError(t, err)
8793
require.Equal(t, testCase.expectedNext, nextTime)
8894
require.Equal(t, testCase.spec, actual.String())
95+
require.Equal(t, testCase.expectedCron, actual.Cron())
96+
require.Equal(t, testCase.expectedTz, actual.Timezone())
8997
} else {
9098
require.EqualError(t, err, testCase.expectedError)
9199
require.Nil(t, actual)

0 commit comments

Comments
 (0)