From f5cc5150e40e50696a628f5d3d1f2a59965aa810 Mon Sep 17 00:00:00 2001 From: Daniel Rocha Date: Fri, 29 Apr 2022 00:35:11 +0200 Subject: [PATCH] refactor: use syscall to set time on Linux and macOS --- go.mod | 1 + go.sum | 2 ++ pkg/htp/service.go | 34 ---------------------------------- pkg/htp/service_darwin.go | 14 ++++++++++++++ pkg/htp/service_linux_386.go | 14 ++++++++++++++ pkg/htp/service_linux_x64.go | 14 ++++++++++++++ pkg/htp/service_unknown.go | 12 ++++++++++++ pkg/htp/service_windows.go | 13 +++++++++++++ 8 files changed, 70 insertions(+), 34 deletions(-) create mode 100644 pkg/htp/service_darwin.go create mode 100644 pkg/htp/service_linux_386.go create mode 100644 pkg/htp/service_linux_x64.go create mode 100644 pkg/htp/service_unknown.go create mode 100644 pkg/htp/service_windows.go diff --git a/go.mod b/go.mod index 1a2cad3..cce9d8c 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( github.com/spf13/cobra v1.4.0 golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd + golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 ) require ( diff --git a/go.sum b/go.sum index d7357a7..02200da 100644 --- a/go.sum +++ b/go.sum @@ -8,5 +8,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd h1:zVFyTKZN/Q7mNRWSs1GOYnHM9NiFSJ54YVRsD0rNWT4= golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= +golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc= +golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/pkg/htp/service.go b/pkg/htp/service.go index 833b1e0..7b8f464 100644 --- a/pkg/htp/service.go +++ b/pkg/htp/service.go @@ -1,18 +1,5 @@ package htp -import ( - "fmt" - "os/exec" - "runtime" - "time" -) - -const ( - // isoFormat = "2006-01-02T15:04:05.000Z07:00" - // unixFormat = "Mon Jan _2 15:04:05.000 MST 2006" - macosFormat = "0102150406.05" -) - type SyncTrace struct { Before func(i int) bool After func(i int, round *SyncRound) bool @@ -47,24 +34,3 @@ func Sync(client *SyncClient, model *SyncModel, trace *SyncTrace) error { return nil } - -func SyncSystem(model *SyncModel) error { - switch runtime.GOOS { - case "windows": - arg := fmt.Sprintf("Set-Date -Adjust $([TimeSpan]::FromSeconds(%+.3f))", -model.Offset().Sec()) - return exec.Command("powershell", "-Command", arg).Run() - - case "linux": - arg := fmt.Sprintf("%+.3f seconds", -model.Offset().Sec()) - return exec.Command("date", "-s", arg).Run() - - case "darwin": - arg := model.Now().Add(time.Second).Format(macosFormat) - sleep := time.Duration(int(time.Second) - model.Now().Nanosecond()) - time.Sleep(sleep) - return exec.Command("date", arg).Run() - - default: - return fmt.Errorf("system not supported: %s", runtime.GOOS) - } -} diff --git a/pkg/htp/service_darwin.go b/pkg/htp/service_darwin.go new file mode 100644 index 0000000..5b196b8 --- /dev/null +++ b/pkg/htp/service_darwin.go @@ -0,0 +1,14 @@ +//go:build darwin + +package htp + +import "golang.org/x/sys/unix" + +func SyncSystem(model *SyncModel) error { + now := model.Now() + tv := &unix.Timeval{ + Sec: now.Unix(), + Usec: int32(now.UnixMicro() % 1_000_000), + } + return unix.Settimeofday(tv) +} diff --git a/pkg/htp/service_linux_386.go b/pkg/htp/service_linux_386.go new file mode 100644 index 0000000..174694e --- /dev/null +++ b/pkg/htp/service_linux_386.go @@ -0,0 +1,14 @@ +//go:build linux && 386 + +package htp + +import "golang.org/x/sys/unix" + +func SyncSystem(model *SyncModel) error { + now := model.Now() + tv := &unix.Timeval{ + Sec: int32(now.Unix()), + Usec: int32(now.UnixMicro() % 1_000_000), + } + return unix.Settimeofday(tv) +} diff --git a/pkg/htp/service_linux_x64.go b/pkg/htp/service_linux_x64.go new file mode 100644 index 0000000..c57242e --- /dev/null +++ b/pkg/htp/service_linux_x64.go @@ -0,0 +1,14 @@ +//go:build linux && (amd64 || arm64) + +package htp + +import "golang.org/x/sys/unix" + +func SyncSystem(model *SyncModel) error { + now := model.Now() + tv := &unix.Timeval{ + Sec: now.Unix(), + Usec: now.UnixMicro() % 1_000_000, + } + return unix.Settimeofday(tv) +} diff --git a/pkg/htp/service_unknown.go b/pkg/htp/service_unknown.go new file mode 100644 index 0000000..139f1ad --- /dev/null +++ b/pkg/htp/service_unknown.go @@ -0,0 +1,12 @@ +//go:build !(darwin || (linux && (amd64 || arm64 || 386)) || windows) + +package htp + +import ( + "fmt" + "runtime" +) + +func SyncSystem(model *SyncModel) error { + return fmt.Errorf("system not supported: %s", runtime.GOOS) +} diff --git a/pkg/htp/service_windows.go b/pkg/htp/service_windows.go new file mode 100644 index 0000000..b1ae66f --- /dev/null +++ b/pkg/htp/service_windows.go @@ -0,0 +1,13 @@ +//go:build windows + +package htp + +import ( + "fmt" + "os/exec" +) + +func SyncSystem(model *SyncModel) error { + arg := fmt.Sprintf("Set-Date -Adjust $([TimeSpan]::FromSeconds(%+.3f))", -model.Offset().Sec()) + return exec.Command("powershell", "-Command", arg).Run() +}