std.ntp
Asks a server of NTP what time it is, and says how far the clock of this computer is from it: the offset, the time the question took, and what the server says of its own clock, as its stratum and where its time comes from. It measures, and does not set the clock of the system.
import std.ntp
ntp.Sample sample = ntp.query("pool.ntp.org", 2000)
println("the clock is off by {} ms, measured in {} ms", sample.offset.milliseconds(), sample.delay.milliseconds())
println("stratum {}, from {}", sample.stratum, sample.reference)
ntp.Measure measure = ntp.measure("pool.ntp.org", 5, 2000) // five questions, the best one kept
println("{} ms, give or take {} ms", measure.best.offset.milliseconds(), measure.jitter.milliseconds())
The guide: Networking and HTTP.
Made from the documentation in the sources of the library.
#Types
#Leap
enum LeapWhat a server says of a leap second at the end of the day.
None: nothing: the day has its 86400 secondsInsert: the last minute of the day has 61 secondsRemove: the last minute of the day has 59 secondsUnsynchronized: the clock of the server is not set: its time is not to be trusted
#Sample
struct SampleAn answer of a server, and what it says of the clock of this computer.
string server: the address that answered: "193.204.114.232"time.Duration offset: How far the clock of this computer is from the one of the server: what to add to the time of this computer to have the time of the server. Positive when this computer is behind, negative when it is ahead.time.Duration delay: The time of the question and of the answer on the network, without what the server took to answer. The offset can be wrong by at most half of it: the smaller the delay, the better the measure.i32 stratum: How far the server is from a clock of reference: 1 has one itself, as a receiver of GPS or an atomic clock; 2 asks a server of stratum 1, and so on to 15. 16 is a server whose clock is not set, as 0 in its answer.Leap leap: the leap second announced, or that the server is not setstring reference: Where the server takes its time. For stratum 1 the name of its clock, as "GPS", "PPS", "DCF", "GOOG"; from stratum 2 the address of the server it asks, or four bytes of it for an address of IPv6, written as an address of IPv4.time.Duration rootDelay: the delay from the server to its clock of reference, all the waytime.Duration rootDispersion: how much its time can be off from that clocktime.Duration precision: the smallest step of the clock of the serveri64 pollSeconds: how often the server would be asked again, at mosti32 version: of NTP in the answer: 3 or 4time.DateTime time: the time of the server when it answered, in UTCtime.DateTime referenceTime: when the clock of the server was last set, in UTCbool synchronized: Whether the time of the answer can be used: the server says that its clock is set, with a stratum from 1 to 15.
#Measure
struct MeasureSeveral answers of a server, as measure gives them.
Sample best: the one with the smallest delay: the best measurelist<Sample> samples: all those that arrived, in their orderi64 lost: the questions without an answertime.Duration jitter: How much the offsets of the answers differ, the largest less the smallest: the noise of the network, and how much the measure can be trusted.
#Functions
#query
Sample query(string server, i64 timeout)Asks the server of NTP at server, a name or an address, on port 123, and waits for its answer up to timeout milliseconds. Throws an Error when no answer comes, when the name is not found, and when the server refuses, as with "RATE" for who asks too often. An answer of a server whose clock is not set is given, with synchronized false.
The public servers ask not to be asked more than once every few seconds by the same computer; "pool.ntp.org" gives a different server each time.
#queryPort
Sample queryPort(string server, i32 port, i64 timeout)Asks a server of NTP on another port.
#measure
Measure measure(string server, i64 count, i64 timeout)Asks a server count times, a little apart, and keeps the answer with the smallest delay, which is the best measure: the offset of an answer is right within half its delay. A question without an answer is counted in lost. Throws an Error when no answer comes at all, and for a count below 1.
#measurePort
Measure measurePort(string server, i32 port, i64 count, i64 timeout)A measure as measure takes it, of a server on another port.