Skip to content

Commit

Permalink
Merge pull request #19 from Meepoljdx/master
Browse files Browse the repository at this point in the history
add time out param to ntp function
  • Loading branch information
UlricQin authored Nov 28, 2023
2 parents 91da537 + 5bfc859 commit 819b4a1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
30 changes: 19 additions & 11 deletions nux/ntp.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (m *msg) SetMode(md mode) {

// Time returns the "receive time" from the remote NTP server
// specifed as host. NTP client mode is used.
func getTwoTime(host string, version byte) (time.Time, time.Time, error) {
func getTwoTime(host string, version byte, timeout int64) (time.Time, time.Time, error) {
if version < 2 || version > 4 {
panic("ntp: invalid version number")
}
Expand All @@ -79,7 +79,7 @@ func getTwoTime(host string, version byte) (time.Time, time.Time, error) {
return time.Now(), time.Now(), err
}
defer con.Close()
con.SetDeadline(time.Now().Add(5 * time.Second))
con.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))

m := new(msg)
m.SetMode(client)
Expand All @@ -100,7 +100,7 @@ func getTwoTime(host string, version byte) (time.Time, time.Time, error) {
return t, transmitTime, nil
}

func getTime(host string, version byte) (time.Time, error) {
func getTime(host string, version byte, timeout int64) (time.Time, error) {
if version < 2 || version > 4 {
panic("ntp: invalid version number")
}
Expand All @@ -115,8 +115,7 @@ func getTime(host string, version byte) (time.Time, error) {
return time.Now(), err
}
defer con.Close()
con.SetDeadline(time.Now().Add(5 * time.Second))

con.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
m := new(msg)
m.SetMode(client)
m.SetVersion(version)
Expand All @@ -135,21 +134,30 @@ func getTime(host string, version byte) (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) (time.Time, error) {
return getTime(host, version)
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) (time.Time, error) {
return getTime(host, 4)
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) (time.Time, time.Time, error) {
return getTwoTime(host, 4)
func NtpTwoTime(host string, timeout ...int64) (time.Time, time.Time, error) {
return getTwoTime(host, 4, getTimeout(timeout))
}
24 changes: 24 additions & 0 deletions nux/ntp_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 819b4a1

Please # to comment.