@@ -3,6 +3,8 @@ package cli
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "strconv"
7
+ "strings"
6
8
"time"
7
9
8
10
"github.com/spf13/cobra"
@@ -82,14 +84,31 @@ func autostartEnable() *cobra.Command {
82
84
var autostartDayOfWeek string
83
85
var autostartTimezone string
84
86
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 ),
87
90
RunE : func (cmd * cobra.Command , args []string ) error {
88
91
client , err := createClient (cmd )
89
92
if err != nil {
90
93
return err
91
94
}
92
95
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
+
93
112
spec := fmt .Sprintf ("CRON_TZ=%s %s %s * * %s" , autostartTimezone , autostartMinute , autostartHour , autostartDayOfWeek )
94
113
validSchedule , err := schedule .Weekly (spec )
95
114
if err != nil {
@@ -119,7 +138,7 @@ func autostartEnable() *cobra.Command {
119
138
cmd .Flags ().StringVar (& autostartDayOfWeek , "days" , "1-5" , "autostart day(s) of week" )
120
139
tzEnv := os .Getenv ("TZ" )
121
140
if tzEnv == "" {
122
- tzEnv = "UTC "
141
+ tzEnv = "Local "
123
142
}
124
143
cmd .Flags ().StringVar (& autostartTimezone , "tz" , tzEnv , "autostart timezone" )
125
144
return cmd
0 commit comments