niujiuru 1 день назад
Родитель
Сommit
61ab7ddb60
1 измененных файлов с 4 добавлено и 7 удалено
  1. 4 7
      netmgrd/ntp.go

+ 4 - 7
netmgrd/ntp.go

@@ -5,10 +5,10 @@ import (
 	"math"
 	"os/exec"
 	"sync"
-	"syscall"
 	"time"
 
 	"github.com/beevik/ntp"
+	"golang.org/x/sys/unix"
 )
 
 var servers = []string{
@@ -86,18 +86,15 @@ func getAccurateTime(servers []string, timeout time.Duration) (time.Time, error)
 	return bestTime, nil
 }
 
-var setTimeMu sync.Mutex // 保证调用 SetSystemTime() 函数的线程安全
+var setTimeMu sync.Mutex // 调用SetSystemTime()函数的并发互斥
 
 func SetSystemTime(t time.Time) error {
 	setTimeMu.Lock()
 	defer setTimeMu.Unlock()
 
-	tv := syscall.Timeval{
-		Sec:  t.Unix(),
-		Usec: int64(t.Nanosecond() / 1000),
-	}
+	ts := unix.NsecToTimespec(t.UnixNano())
 
-	if err := syscall.Settimeofday(&tv); err != nil {
+	if err := unix.ClockSettime(unix.CLOCK_REALTIME, &ts); err != nil {
 		return err
 	}