Commit 8a44760c authored by Vesa-Pekka Palmu's avatar Vesa-Pekka Palmu

BUGFIX: timezone aware /clock/time/set

parent 5b10dea1
Pipeline #67595920 passed with stages
in 8 minutes and 56 seconds
......@@ -7,6 +7,9 @@ import (
"gitlab.com/Depili/clock-8001/debug"
"log"
"math"
"os"
"os/exec"
"regexp"
"time"
)
......@@ -218,6 +221,8 @@ func (engine *Engine) listen() {
engine.displaySeconds = false
case "secondsOn":
engine.displaySeconds = true
case "setTime":
engine.setTime(message.Data)
}
// We have received a osc command, so stop the version display
engine.initialized = true
......@@ -595,3 +600,27 @@ func (engine *Engine) Resume() {
engine.paused = false
}
}
func (engine *Engine) setTime(time string) {
debug.Printf("Set time: %#v", time)
_, lookErr := exec.LookPath("date")
if lookErr != nil {
debug.Printf("Date binary not found, cannot set system date: %s\n", lookErr.Error())
return
}
// Validate the received time
match, _ := regexp.MatchString("^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])$", time)
if match {
// Set the system time
dateString := fmt.Sprintf("2019-01-01 %s", time)
tzString := fmt.Sprintf("TZ=%s", engine.timeZone.String())
debug.Printf("Setting system date to: %s\n", dateString)
args := []string{"--set", dateString}
cmd := exec.Command("date", args...)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, tzString)
cmd.Run()
} else {
debug.Printf("Invalid time provided: %v\n", time)
}
}
......@@ -17,6 +17,7 @@ var clockUnits = []struct {
// Message is a generic clock message for decoded osc data
type Message struct {
Type string
Data string
CountMessage *CountMessage
CountdownMessage *CountdownMessage
DisplayMessage *DisplayMessage
......
package clock
import (
"fmt"
"github.com/hypebeast/go-osc/osc"
"gitlab.com/Depili/clock-8001/debug"
"log"
"os/exec"
"regexp"
)
// MakeServer creates a clock.Server instance from osc.Server instance
......@@ -182,23 +179,12 @@ func (server *Server) handleTimeSet(msg *osc.Message) {
if err := message.UnmarshalOSC(msg); err != nil {
log.Printf("Unmarshal %v: %v", msg, err)
} else {
debug.Printf("Set time: %#v", message)
_, lookErr := exec.LookPath("date")
if lookErr != nil {
debug.Printf("Date binary not found, cannot set system date: %s\n", lookErr.Error())
return
}
// Validate the received time
match, _ := regexp.MatchString("^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])$", message.Time)
if match {
// Set the system time
dateString := fmt.Sprintf("2019-01-01 %s", message.Time)
debug.Printf("Setting system date to: %s\n", dateString)
args := []string{"--set", dateString}
exec.Command("date", args...).Run()
} else {
debug.Printf("Invalid time provided: %v\n", message.Time)
debug.Printf("Set time: %v\n", message.Time)
m := Message{
Type: "setTime",
Data: message.Time,
}
server.update(m)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment