Skip to content

Commit cfbda57

Browse files
authored
fix: Parse 24h time format from schedule cron in CLI (#2586)
* fix: parse 24h time format from schedule cron in cli * add unit test
1 parent b7eeb43 commit cfbda57

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

coderd/autobuild/schedule/schedule.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (s Schedule) Time() string {
144144
minute := strings.Fields(s.cronStr)[0]
145145
hour := strings.Fields(s.cronStr)[1]
146146
maybeTime := fmt.Sprintf("%s:%s", hour, minute)
147-
t, err := time.ParseInLocation("3:4", maybeTime, s.sched.Location)
147+
t, err := time.ParseInLocation("15:4", maybeTime, s.sched.Location)
148148
if err != nil {
149149
// return the original cronspec for minute and hour, who knows what's in there!
150150
return fmt.Sprintf("cron(%s %s)", minute, hour)

coderd/autobuild/schedule/schedule_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ func Test_Weekly(t *testing.T) {
5050
expectedString: "CRON_TZ=UTC 30 9 * * 1-5",
5151
expectedTime: "9:30AM",
5252
},
53+
{
54+
name: "24h format",
55+
spec: "30 13 * * 1-5",
56+
at: time.Date(2022, 4, 1, 13, 29, 0, 0, time.UTC),
57+
expectedNext: time.Date(2022, 4, 1, 13, 30, 0, 0, time.UTC),
58+
expectedMin: 24 * time.Hour,
59+
expectedDaysOfWeek: "Mon-Fri",
60+
expectedError: "",
61+
expectedCron: "30 13 * * 1-5",
62+
expectedLocation: time.UTC,
63+
expectedString: "CRON_TZ=UTC 30 13 * * 1-5",
64+
expectedTime: "1:30PM",
65+
},
5366
{
5467
name: "convoluted with timezone",
5568
spec: "CRON_TZ=US/Central */5 12-18 * * 1,3,6",
@@ -141,6 +154,7 @@ func Test_Weekly(t *testing.T) {
141154
require.Equal(t, testCase.expectedString, actual.String())
142155
require.Equal(t, testCase.expectedMin, actual.Min())
143156
require.Equal(t, testCase.expectedDaysOfWeek, actual.DaysOfWeek())
157+
require.Equal(t, testCase.expectedTime, actual.Time())
144158
} else {
145159
require.EqualError(t, err, testCase.expectedError)
146160
require.Nil(t, actual)

0 commit comments

Comments
 (0)