ntp API reference
A simple SNTP client (RFC 4330 / 5905): query a time server over UDP and get back its time plus the local clock offset and round-trip delay. This is the one-shot query half of NTP - it speaks the standard NTP wire protocol (the 48-byte packet on port 123) but does not discipline the clock or run as a daemon; it reports the measurement and leaves acting on it to the caller.
The packet is packed / unpacked with bytes and the bitwise operators, and NTP timestamps (seconds since 1900) are converted to time.Time through time. Needs the default jennifer binary (net); a query throws Error{kind: "ntp"} on timeout or a short reply.
Import with import "ntp.j" as ntp;. See the ntp guide for prose and examples.
Functions
ntp.query(host as string)
Query an NTP server by host name (port 123, a 5-second timeout).
Parameters
host{string}- the server host (e.g. "pool.ntp.org")
Returns {Result} - the server time, clock offset, and round-trip delay
Throws
{Error}- kind "ntp" on timeout or a malformed reply
ntp.queryWith(address as string, timeoutMs as int)
Query an NTP server at a full host:port address with a timeout in milliseconds.
Parameters
address{string}- the server "host:port"timeoutMs{int}- the receive timeout in milliseconds
Returns {Result} - the server time, clock offset, and round-trip delay
Throws
{Error}- kind "ntp" on timeout or a malformed reply
Structs
ntp.Result
The result of a query.
| Field | Type | Description |
|---|---|---|
serverTime | time.Time | the server's transmit time |
offset | time.Duration | the local clock offset (server minus local) |
delay | time.Duration | the measured round-trip delay |