Skip to content
Jennifer Programming Language

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 an ntp.NtpError (kind "ntp") on an unsound reply, or a Kiss-o'-Death, whose code (RATE: back off, DENY / RSTR: stop asking this server) is the kissCode field. No reply within the timeout is the generic kind "timeout" (an Error); a socket failure (an unresolvable host) is a net.NetError (kind "net").

Import with import "ntp.j" as ntp;. See the ntp guide for prose and examples.

Contents

NameWhat it is
queryQuery an NTP server by host name (port 123, a 5-second timeout).
queryWithQuery an NTP server at a full host:port address with a timeout in milliseconds.
NtpErrorstruct: A failed NTP query.
Resultstruct: The result of a query.

Functions

query

jennifer
ntp.query(host as string)

Query an NTP server by host name (port 123, a 5-second timeout).

host
the server host (e.g. "pool.ntp.org")
Returns Result
the server time, clock offset, and round-trip delay
Throws NtpError
kind "ntp" on a malformed reply or a Kiss-o'-Death; no reply in time is an Error of kind "timeout", a socket failure a net.NetError (kind "net")

queryWith

jennifer
ntp.queryWith(address as string, timeoutMs as int)

Query an NTP server at a full host:port address with a timeout in milliseconds.

address
the server "host:port"
timeoutMs
the receive timeout in milliseconds
Returns Result
the server time, clock offset, and round-trip delay
Throws NtpError
kind "ntp" on a malformed reply or a Kiss-o'-Death; no reply in time is an Error of kind "timeout", a socket failure a net.NetError (kind "net")

Structs

NtpError

A failed NTP query. Every error of kind "ntp" is one, so its fields are safe to read once the kind matches.

jennifer
def struct NtpError {
    kind as string,
    message as string,
    kissCode as string
};
kind
always "ntp"
message
what went wrong
kissCode
a Kiss-o'-Death's four-letter code (RFC 5905 7.4: RATE - poll less often; DENY, RSTR - stop querying this server; INIT, STEP, ...), non-printable bytes as ?; "" for any other failure

Result

The result of a query.

jennifer
def struct Result {
    serverTime as time.Time,
    offset as time.Duration,
    delay as time.Duration
};
serverTime
the server's transmit time
offset
the local clock offset (server minus local)
delay
the measured round-trip delay