Skip to content

Commit

Permalink
refactor: use syscall to set time on Linux and macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc committed Apr 29, 2022
1 parent 76da039 commit f5cc515
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 34 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
34 changes: 0 additions & 34 deletions pkg/htp/service.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
14 changes: 14 additions & 0 deletions pkg/htp/service_darwin.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 14 additions & 0 deletions pkg/htp/service_linux_386.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 14 additions & 0 deletions pkg/htp/service_linux_x64.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 12 additions & 0 deletions pkg/htp/service_unknown.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 13 additions & 0 deletions pkg/htp/service_windows.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit f5cc515

Please # to comment.