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
var Sntp = ; // All options are optional var options = host: 'nist1-sj.ustiming.org' // Defaults to pool.ntp.org port: 123 // Defaults to 123 (NTP) resolveReference: true // Default to false (not resolving) timeout: 1000 // Defaults to zero (no timeout); // Request server time const exec = { try const time = await Sntp; console; process; catch err console; process; }; ;
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).
// Request offset once const exec = { const offset1 = await Sntp; console; // New (served fresh) // Request offset again const offset2 = await Sntp; console; // Identical (served from cache)}; ;
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.
var before = Sntp; // System time without offset const exec = { await Sntpstart; var now = Sntp; // With offset Sntp;}; ;