@@ -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}
0 commit comments