• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.travis.ymlH A D19-Mar-2020130 1511

CONTRIBUTORSH A D19-Mar-2020169 77

LICENSEH A D19-Mar-20201.3 KiB2520

README.mdH A D19-Mar-20203.7 KiB7359

RELEASE_NOTES.mdH A D19-Mar-20202.6 KiB6544

ntp.goH A D19-Mar-202016.8 KiB574331

ntp_test.goH A D19-Mar-20208.4 KiB338265

README.md

1[![Build Status](https://travis-ci.org/beevik/ntp.svg?branch=master)](https://travis-ci.org/beevik/ntp)
2[![GoDoc](https://godoc.org/github.com/beevik/ntp?status.svg)](https://godoc.org/github.com/beevik/ntp)
3
4ntp
5===
6
7The ntp package is an implementation of a Simple NTP (SNTP) client based on
8[RFC5905](https://tools.ietf.org/html/rfc5905). It allows you to connect to
9a remote NTP server and request information about the current time.
10
11
12## Querying the current time
13
14If all you care about is the current time according to a remote NTP server,
15simply use the `Time` function:
16```go
17time, err := ntp.Time("0.beevik-ntp.pool.ntp.org")
18```
19
20
21## Querying time metadata
22
23To obtain the current time as well as some additional metadata about the time,
24use the [`Query`](https://godoc.org/github.com/beevik/ntp#Query) function:
25```go
26response, err := ntp.Query("0.beevik-ntp.pool.ntp.org")
27time := time.Now().Add(response.ClockOffset)
28```
29
30Alternatively, use the [`QueryWithOptions`](https://godoc.org/github.com/beevik/ntp#QueryWithOptions)
31function if you want to change the default behavior used by the `Query`
32function:
33```go
34options := ntp.QueryOptions{ Timeout: 30*time.Second, TTL: 5 }
35response, err := ntp.QueryWithOptions("0.beevik-ntp.pool.ntp.org", options)
36time := time.Now().Add(response.ClockOffset)
37```
38
39The [`Response`](https://godoc.org/github.com/beevik/ntp#Response) structure
40returned by `Query` includes the following information:
41* `Time`: The time the server transmitted its response, according to its own clock.
42* `ClockOffset`: The estimated offset of the local system clock relative to the server's clock. For a more accurate time reading, you may add this offset to any subsequent system clock reading.
43* `RTT`: An estimate of the round-trip-time delay between the client and the server.
44* `Precision`: The precision of the server's clock reading.
45* `Stratum`: The server's stratum, which indicates the number of hops from the server to the reference clock. A stratum 1 server is directly attached to the reference clock. If the stratum is zero, the server has responded with the "kiss of death".
46* `ReferenceID`: A unique identifier for the consulted reference clock.
47* `ReferenceTime`: The time at which the server last updated its local clock setting.
48* `RootDelay`: The server's aggregate round-trip-time delay to the stratum 1 server.
49* `RootDispersion`: The server's estimated maximum measurement error relative to the reference clock.
50* `RootDistance`: An estimate of the root synchronization distance between the client and the stratum 1 server.
51* `Leap`: The leap second indicator, indicating whether a second should be added to or removed from the current month's last minute.
52* `MinError`: A lower bound on the clock error between the client and the server.
53* `KissCode`: A 4-character string describing the reason for a "kiss of death" response (stratum=0).
54* `Poll`: The maximum polling interval between successive messages to the server.
55
56The `Response` structure's [`Validate`](https://godoc.org/github.com/beevik/ntp#Response.Validate)
57method performs additional sanity checks to determine whether the response is
58suitable for time synchronization purposes.
59```go
60err := response.Validate()
61if err == nil {
62    // response data is suitable for synchronization purposes
63}
64```
65
66## Using the NTP pool
67
68The NTP pool is a shared resource used by people all over the world.
69To prevent it from becoming overloaded, please avoid querying the standard
70`pool.ntp.org` zone names in your applications.  Instead, consider requesting
71your own [vendor zone](http://www.pool.ntp.org/en/vendors.html) or [joining
72the pool](http://www.pool.ntp.org/join.html).
73