Skip to content

Commit da00128

Browse files
committed
feat: Support "Local" timezone for autostart
1 parent fff59ef commit da00128

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

cli/autostart.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cli
33
import (
44
"fmt"
55
"os"
6+
"strconv"
7+
"strings"
68
"time"
79

810
"github.com/spf13/cobra"
@@ -82,14 +84,31 @@ func autostartEnable() *cobra.Command {
8284
var autostartDayOfWeek string
8385
var autostartTimezone string
8486
cmd := &cobra.Command{
85-
Use: "enable <workspace_name> <schedule>",
86-
Args: cobra.ExactArgs(1),
87+
Use: "enable <workspace_name> <schedule>",
88+
Example: "coder autostart enable my-workspace --minute 30 --hour 9 --days 1-5 --tz Local",
89+
Args: cobra.ExactArgs(1),
8790
RunE: func(cmd *cobra.Command, args []string) error {
8891
client, err := createClient(cmd)
8992
if err != nil {
9093
return err
9194
}
9295

96+
// If the user selects "local" timezone, we need to convert this into
97+
// UTC for the api.
98+
if strings.EqualFold(autostartTimezone, "local") {
99+
// Grab current year/month/day to account for daylight savings
100+
// windows.
101+
now := time.Now()
102+
// Create the local date for the hour + minute.
103+
local := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), 0, 0, time.Local)
104+
// Convert to UTC time
105+
inUTC := local.In(time.UTC)
106+
// Convert the user's input to UTC values.
107+
autostartMinute = strconv.Itoa(inUTC.Minute())
108+
autostartHour = strconv.Itoa(inUTC.Hour())
109+
autostartTimezone = "UTC"
110+
}
111+
93112
spec := fmt.Sprintf("CRON_TZ=%s %s %s * * %s", autostartTimezone, autostartMinute, autostartHour, autostartDayOfWeek)
94113
validSchedule, err := schedule.Weekly(spec)
95114
if err != nil {
@@ -119,7 +138,7 @@ func autostartEnable() *cobra.Command {
119138
cmd.Flags().StringVar(&autostartDayOfWeek, "days", "1-5", "autostart day(s) of week")
120139
tzEnv := os.Getenv("TZ")
121140
if tzEnv == "" {
122-
tzEnv = "UTC"
141+
tzEnv = "Local"
123142
}
124143
cmd.Flags().StringVar(&autostartTimezone, "tz", tzEnv, "autostart timezone")
125144
return cmd

0 commit comments

Comments
 (0)