sntp
An SNTP v4 client (RFC4330) for node. Simpy connects to the NTP or SNTP server requested and returns the server time
along with the roundtrip duration and clock offset. To adjust the local time to the NTP time, add the returned t
offset
to the local time.
Usage
const Sntp = require('@hapi/sntp');
const options = {
host: 'nist1-sj.ustiming.org',
port: 123,
resolveReference: true,
timeout: 1000
};
const exec = async function () {
try {
const time = await Sntp.time(options);
console.log('Local clock is off by: ' + time.t + ' milliseconds');
process.exit(0);
}
catch (err) {
console.log('Failed: ' + err.message);
process.exit(1);
}
};
exec();
If an application needs to maintain continuous time synchronization, the module provides a stateful method for
querying the current offset only when the last one is too old (defaults to daily).
const exec = async function () {
const offset1 = await Sntp.offset();
console.log(offset1);
const offset2 = await Sntp.offset();
console.log(offset2);
};
exec();
To set a background offset refresh, start the interval and use the provided now() method. If for any reason the
client fails to obtain an up-to-date offset, the current system clock is used.
const before = Sntp.now();
const exec = async function () {
await Sntp.start();
const now = Sntp.now();
Sntp.stop();
};
exec();