Update vendoring for github.com/beevik/ntp

This commit is contained in:
Ben Kochie 2016-12-16 12:54:21 +01:00
parent f1def04193
commit 6dd85969ef
2 changed files with 70 additions and 36 deletions

84
vendor/github.com/beevik/ntp/ntp.go generated vendored
View File

@ -16,7 +16,7 @@ import (
"time" "time"
) )
type mode byte type mode uint8
const ( const (
reserved mode = 0 + iota reserved mode = 0 + iota
@ -39,40 +39,53 @@ var (
ntpEpoch = time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC) ntpEpoch = time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
) )
type ntpTime struct { // An ntpTime is a 64-bit fixed-point (Q32.32) representation of the number of
Seconds uint32 // seconds elapsed since the NTP epoch.
Fraction uint32 type ntpTime uint64
// Duration interprets the fixed-point ntpTime as a number of elapsed seconds
// and returns the corresponding time.Duration value.
func (t ntpTime) Duration() time.Duration {
sec := (t >> 32) * nanoPerSec
frac := (t & 0xffffffff) * nanoPerSec >> 32
return time.Duration(sec + frac)
} }
// Time interprets the fixed-point ntpTime as a an absolute time and returns
// the corresponding time.Time value.
func (t ntpTime) Time() time.Time { func (t ntpTime) Time() time.Time {
return ntpEpoch.Add(t.sinceEpoch()) return ntpEpoch.Add(t.Duration())
} }
// sinceEpoch converts the ntpTime record t into a duration since the NTP // toNtpTime converts the time.Time value t into its 64-bit fixed-point
// epoch time (Jan 1, 1900). // ntpTime representation.
func (t ntpTime) sinceEpoch() time.Duration {
sec := time.Duration(t.Seconds) * time.Second
frac := time.Duration(uint64(t.Fraction) * nanoPerSec >> 32)
return sec + frac
}
// toNtpTime converts the time value t into an ntpTime representation.
func toNtpTime(t time.Time) ntpTime { func toNtpTime(t time.Time) ntpTime {
nsec := uint64(t.Sub(ntpEpoch)) nsec := uint64(t.Sub(ntpEpoch))
return ntpTime{ sec := nsec / nanoPerSec
Seconds: uint32(nsec / nanoPerSec), frac := (nsec - sec*nanoPerSec) << 32 / nanoPerSec
Fraction: uint32((nsec % nanoPerSec) << 32 / nanoPerSec), return ntpTime(sec<<32 | frac)
} }
// An ntpTimeShort is a 32-bit fixed-point (Q16.16) representation of the
// number of seconds elapsed since the NTP epoch.
type ntpTimeShort uint32
// Duration interprets the fixed-point ntpTimeShort as a number of elapsed
// seconds and returns the corresponding time.Duration value.
func (t ntpTimeShort) Duration() time.Duration {
sec := (t >> 16) * nanoPerSec
frac := (t & 0xffff) * nanoPerSec >> 16
return time.Duration(sec + frac)
} }
// msg is an internal representation of an NTP packet. // msg is an internal representation of an NTP packet.
type msg struct { type msg struct {
LiVnMode byte // Leap Indicator (2) + Version (3) + Mode (3) LiVnMode uint8 // Leap Indicator (2) + Version (3) + Mode (3)
Stratum byte Stratum uint8
Poll byte Poll int8
Precision byte Precision int8
RootDelay uint32 RootDelay ntpTimeShort
RootDispersion uint32 RootDispersion ntpTimeShort
ReferenceID uint32 ReferenceID uint32
ReferenceTime ntpTime ReferenceTime ntpTime
OriginTime ntpTime OriginTime ntpTime
@ -87,7 +100,7 @@ func (m *msg) setVersion(v int) {
// setMode sets the NTP protocol mode on the message. // setMode sets the NTP protocol mode on the message.
func (m *msg) setMode(md mode) { func (m *msg) setMode(md mode) {
m.LiVnMode = (m.LiVnMode & 0xf8) | byte(md) m.LiVnMode = (m.LiVnMode & 0xf8) | uint8(md)
} }
// A Response contains time data, some of which is returned by the NTP server // A Response contains time data, some of which is returned by the NTP server
@ -96,7 +109,12 @@ type Response struct {
Time time.Time // receive time reported by the server Time time.Time // receive time reported by the server
RTT time.Duration // round-trip time between client and server RTT time.Duration // round-trip time between client and server
ClockOffset time.Duration // local clock offset relative to server ClockOffset time.Duration // local clock offset relative to server
Poll time.Duration // maximum polling interval
Precision time.Duration // precision of server's system clock
Stratum uint8 // stratum level of NTP server's clock Stratum uint8 // stratum level of NTP server's clock
ReferenceID uint32 // server's reference ID
RootDelay time.Duration // server's RTT to the reference clock
RootDispersion time.Duration // server's dispersion to the reference clock
} }
// Query returns information from the remote NTP server specifed as host. NTP // Query returns information from the remote NTP server specifed as host. NTP
@ -112,7 +130,12 @@ func Query(host string, version int) (*Response, error) {
Time: m.ReceiveTime.Time(), Time: m.ReceiveTime.Time(),
RTT: rtt(m.OriginTime, m.ReceiveTime, m.TransmitTime, now), RTT: rtt(m.OriginTime, m.ReceiveTime, m.TransmitTime, now),
ClockOffset: offset(m.OriginTime, m.ReceiveTime, m.TransmitTime, now), ClockOffset: offset(m.OriginTime, m.ReceiveTime, m.TransmitTime, now),
Poll: toInterval(m.Poll),
Precision: toInterval(m.Precision),
Stratum: m.Stratum, Stratum: m.Stratum,
ReferenceID: m.ReferenceID,
RootDelay: m.RootDelay.Duration(),
RootDispersion: m.RootDispersion.Duration(),
} }
// https://tools.ietf.org/html/rfc5905#section-7.3 // https://tools.ietf.org/html/rfc5905#section-7.3
@ -204,3 +227,14 @@ func offset(t1, t2, t3, t4 ntpTime) time.Duration {
b := t3.Time().Sub(t4.Time()) b := t3.Time().Sub(t4.Time())
return (a + b) / time.Duration(2) return (a + b) / time.Duration(2)
} }
func toInterval(t int8) time.Duration {
switch {
case t > 0:
return time.Duration(uint64(time.Second) << uint(t))
case t < 0:
return time.Duration(uint64(time.Second) >> uint(-t))
default:
return time.Second
}
}

6
vendor/vendor.json vendored
View File

@ -9,10 +9,10 @@
"revisionTime": "2016-11-08T19:08:11Z" "revisionTime": "2016-11-08T19:08:11Z"
}, },
{ {
"checksumSHA1": "JhaYHdVIj52Fpdcb7DuDjr/gk0Q=", "checksumSHA1": "1cZ+PIsN4xjMDTOTb5a0KDa0c6c=",
"path": "github.com/beevik/ntp", "path": "github.com/beevik/ntp",
"revision": "f0545e6f2c3cb0d0a2ed115b88c539d8e5247ef3", "revision": "8ed1e691d326c0ab1169cacbc1e8b5c988089c04",
"revisionTime": "2016-05-31T23:09:58Z" "revisionTime": "2016-06-05T02:18:02Z"
}, },
{ {
"checksumSHA1": "spyv5/YFBjYyZLZa1U2LBfDR8PM=", "checksumSHA1": "spyv5/YFBjYyZLZa1U2LBfDR8PM=",