Skip to content

fix: Parse 24h time format from schedule cron in CLI #2586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coderd/autobuild/schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (s Schedule) Time() string {
minute := strings.Fields(s.cronStr)[0]
hour := strings.Fields(s.cronStr)[1]
maybeTime := fmt.Sprintf("%s:%s", hour, minute)
t, err := time.ParseInLocation("3:4", maybeTime, s.sched.Location)
t, err := time.ParseInLocation("15:4", maybeTime, s.sched.Location)
if err != nil {
// return the original cronspec for minute and hour, who knows what's in there!
return fmt.Sprintf("cron(%s %s)", minute, hour)
Expand Down
14 changes: 14 additions & 0 deletions coderd/autobuild/schedule/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ func Test_Weekly(t *testing.T) {
expectedString: "CRON_TZ=UTC 30 9 * * 1-5",
expectedTime: "9:30AM",
},
{
name: "24h format",
spec: "30 13 * * 1-5",
at: time.Date(2022, 4, 1, 13, 29, 0, 0, time.UTC),
expectedNext: time.Date(2022, 4, 1, 13, 30, 0, 0, time.UTC),
expectedMin: 24 * time.Hour,
expectedDaysOfWeek: "Mon-Fri",
expectedError: "",
expectedCron: "30 13 * * 1-5",
expectedLocation: time.UTC,
expectedString: "CRON_TZ=UTC 30 13 * * 1-5",
expectedTime: "1:30PM",
},
{
name: "convoluted with timezone",
spec: "CRON_TZ=US/Central */5 12-18 * * 1,3,6",
Expand Down Expand Up @@ -141,6 +154,7 @@ func Test_Weekly(t *testing.T) {
require.Equal(t, testCase.expectedString, actual.String())
require.Equal(t, testCase.expectedMin, actual.Min())
require.Equal(t, testCase.expectedDaysOfWeek, actual.DaysOfWeek())
require.Equal(t, testCase.expectedTime, actual.Time())
} else {
require.EqualError(t, err, testCase.expectedError)
require.Nil(t, actual)
Expand Down