mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
fix: Parse 24h time format from schedule cron in CLI (#2586)
* fix: parse 24h time format from schedule cron in cli * add unit test
This commit is contained in:
@ -144,7 +144,7 @@ func (s Schedule) Time() string {
|
|||||||
minute := strings.Fields(s.cronStr)[0]
|
minute := strings.Fields(s.cronStr)[0]
|
||||||
hour := strings.Fields(s.cronStr)[1]
|
hour := strings.Fields(s.cronStr)[1]
|
||||||
maybeTime := fmt.Sprintf("%s:%s", hour, minute)
|
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 {
|
if err != nil {
|
||||||
// return the original cronspec for minute and hour, who knows what's in there!
|
// return the original cronspec for minute and hour, who knows what's in there!
|
||||||
return fmt.Sprintf("cron(%s %s)", minute, hour)
|
return fmt.Sprintf("cron(%s %s)", minute, hour)
|
||||||
|
@ -50,6 +50,19 @@ func Test_Weekly(t *testing.T) {
|
|||||||
expectedString: "CRON_TZ=UTC 30 9 * * 1-5",
|
expectedString: "CRON_TZ=UTC 30 9 * * 1-5",
|
||||||
expectedTime: "9:30AM",
|
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",
|
name: "convoluted with timezone",
|
||||||
spec: "CRON_TZ=US/Central */5 12-18 * * 1,3,6",
|
spec: "CRON_TZ=US/Central */5 12-18 * * 1,3,6",
|
||||||
@ -141,6 +154,7 @@ func Test_Weekly(t *testing.T) {
|
|||||||
require.Equal(t, testCase.expectedString, actual.String())
|
require.Equal(t, testCase.expectedString, actual.String())
|
||||||
require.Equal(t, testCase.expectedMin, actual.Min())
|
require.Equal(t, testCase.expectedMin, actual.Min())
|
||||||
require.Equal(t, testCase.expectedDaysOfWeek, actual.DaysOfWeek())
|
require.Equal(t, testCase.expectedDaysOfWeek, actual.DaysOfWeek())
|
||||||
|
require.Equal(t, testCase.expectedTime, actual.Time())
|
||||||
} else {
|
} else {
|
||||||
require.EqualError(t, err, testCase.expectedError)
|
require.EqualError(t, err, testCase.expectedError)
|
||||||
require.Nil(t, actual)
|
require.Nil(t, actual)
|
||||||
|
Reference in New Issue
Block a user