diff --git a/nux/ntp.go b/nux/ntp.go index e2b5cbf..cc63e0e 100644 --- a/nux/ntp.go +++ b/nux/ntp.go @@ -134,21 +134,30 @@ func getTime(host string, version byte, timeout int64) (time.Time, error) { return t, nil } +func getTimeout(timeout []int64) int64 { + // If the timeout parameter is not provided or the timeout parameter is greater than 0 + if len(timeout) != 1 { + return 5 + } else { + return timeout[0] + } +} + // TimeV returns the "receive time" from the remote NTP server // specifed as host. Use the NTP client mode with the requested // version number (2, 3, or 4). -func NtpTimeV(host string, version byte, timeout int64) (time.Time, error) { - return getTime(host, version, timeout) +func NtpTimeV(host string, version byte, timeout ...int64) (time.Time, error) { + return getTime(host, version, getTimeout(timeout)) } // Time returns the "receive time" from the remote NTP server // specifed as host. NTP client mode version 4 is used. -func NtpTime(host string, timeout int64) (time.Time, error) { - return getTime(host, 4, timeout) +func NtpTime(host string, timeout ...int64) (time.Time, error) { + return getTime(host, 4, getTimeout(timeout)) } // Time returns the "receive time" from the remote NTP server // specifed as host. NTP client mode version 4 is used. -func NtpTwoTime(host string, timeout int64) (time.Time, time.Time, error) { - return getTwoTime(host, 4, timeout) +func NtpTwoTime(host string, timeout ...int64) (time.Time, time.Time, error) { + return getTwoTime(host, 4, getTimeout(timeout)) } diff --git a/nux/ntp_test.go b/nux/ntp_test.go new file mode 100644 index 0000000..a137a13 --- /dev/null +++ b/nux/ntp_test.go @@ -0,0 +1,24 @@ +package nux + +import ( + "log" + "testing" + "time" +) + +func TestNtpTime(t *testing.T) { + orgTime := time.Now() + log.Println("Begin") + serverReciveTime, serverTransmitTime, err := NtpTwoTime("ntp1.aliyun.com", 20) + if err != nil { + log.Println(err) + return + } + dstTime := time.Now() + + // https://en.wikipedia.org/wiki/Network_Time_Protocol + duration := ((serverReciveTime.UnixNano() - orgTime.UnixNano()) + (serverTransmitTime.UnixNano() - dstTime.UnixNano())) / 2 + + delta := duration / 1e6 // convert to ms + log.Println(delta) +}