Skip to content

Commit 819b4a1

Browse files
authored
Merge pull request #19 from Meepoljdx/master
add time out param to ntp function
2 parents 91da537 + 5bfc859 commit 819b4a1

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

nux/ntp.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (m *msg) SetMode(md mode) {
6464

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

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

103-
func getTime(host string, version byte) (time.Time, error) {
103+
func getTime(host string, version byte, timeout int64) (time.Time, error) {
104104
if version < 2 || version > 4 {
105105
panic("ntp: invalid version number")
106106
}
@@ -115,8 +115,7 @@ func getTime(host string, version byte) (time.Time, error) {
115115
return time.Now(), err
116116
}
117117
defer con.Close()
118-
con.SetDeadline(time.Now().Add(5 * time.Second))
119-
118+
con.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
120119
m := new(msg)
121120
m.SetMode(client)
122121
m.SetVersion(version)
@@ -135,21 +134,30 @@ func getTime(host string, version byte) (time.Time, error) {
135134
return t, nil
136135
}
137136

137+
func getTimeout(timeout []int64) int64 {
138+
// If the timeout parameter is not provided or the timeout parameter is greater than 0
139+
if len(timeout) != 1 {
140+
return 5
141+
} else {
142+
return timeout[0]
143+
}
144+
}
145+
138146
// TimeV returns the "receive time" from the remote NTP server
139147
// specifed as host. Use the NTP client mode with the requested
140148
// version number (2, 3, or 4).
141-
func NtpTimeV(host string, version byte) (time.Time, error) {
142-
return getTime(host, version)
149+
func NtpTimeV(host string, version byte, timeout ...int64) (time.Time, error) {
150+
return getTime(host, version, getTimeout(timeout))
143151
}
144152

145153
// Time returns the "receive time" from the remote NTP server
146154
// specifed as host. NTP client mode version 4 is used.
147-
func NtpTime(host string) (time.Time, error) {
148-
return getTime(host, 4)
155+
func NtpTime(host string, timeout ...int64) (time.Time, error) {
156+
return getTime(host, 4, getTimeout(timeout))
149157
}
150158

151159
// Time returns the "receive time" from the remote NTP server
152160
// specifed as host. NTP client mode version 4 is used.
153-
func NtpTwoTime(host string) (time.Time, time.Time, error) {
154-
return getTwoTime(host, 4)
161+
func NtpTwoTime(host string, timeout ...int64) (time.Time, time.Time, error) {
162+
return getTwoTime(host, 4, getTimeout(timeout))
155163
}

nux/ntp_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package nux
2+
3+
import (
4+
"log"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestNtpTime(t *testing.T) {
10+
orgTime := time.Now()
11+
log.Println("Begin")
12+
serverReciveTime, serverTransmitTime, err := NtpTwoTime("ntp1.aliyun.com", 20)
13+
if err != nil {
14+
log.Println(err)
15+
return
16+
}
17+
dstTime := time.Now()
18+
19+
// https://en.wikipedia.org/wiki/Network_Time_Protocol
20+
duration := ((serverReciveTime.UnixNano() - orgTime.UnixNano()) + (serverTransmitTime.UnixNano() - dstTime.UnixNano())) / 2
21+
22+
delta := duration / 1e6 // convert to ms
23+
log.Println(delta)
24+
}

0 commit comments

Comments
 (0)