package time import ( "syscall" "time" "linkfog.com/public/lib/l" "github.com/beevik/ntp" ) func SynchronizeSystemTime() bool { l.Info("the current device start check time sync...") defer l.Info("sync system time ends") go func() { pool := make([]string, 0) pool = append(pool, "pool.ntp.org") pool = append(pool, "cn.pool.ntp.org") pool = append(pool, "ntp1.aliyun.com") pool = append(pool, "ntp.ntsc.ac.cn") pool = append(pool, "time1.cloud.tencent.com") pool = append(pool, "time.ustc.edu.cn") for _, v := range pool { response, err := ntp.Query(v) if err != nil { l.Warn("Failed to get time from NTP:", err) continue } ntpTime := time.Now().Add(response.ClockOffset) tv := syscall.NsecToTimeval(ntpTime.UnixNano()) err = syscall.Settimeofday(&tv) if err != nil { l.Warnf("Failed to set system time: %v", err) continue } else { l.Info("set system time successfully") break } } }() for i := 30; i > 0; i-- { ts := time.Now().Unix() //2024-04-01 00:00:00 if ts > 1711900800 { l.Info("time sync success...", ts) return true } else { l.Warn("time does not sync...", ts) } time.Sleep(10 * time.Second) } return false }