SNTP Module
This sntp (About Modules) module implements a SNTP for Espruino. It is a stripped down version of https://github.com/hueniverse/sntp.
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 = require('sntp');
// All options are optional
var options = {
host: 'nist1-sj.ustiming.org', // Defaults to pool.ntp.org
port: 123, // Defaults to 123 (NTP)
timeout: 1000 // Defaults to zero (no timeout)
};
// Request server time
Sntp.time(options, function (err, time) {
if (err) {
console.log('Failed: ' + err.message);
return;
}
console.log('Local clock is off by: ' + time.t + ' milliseconds');
});
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
Sntp.offset(function (err, offset) {
console.log(offset); // New (served fresh)
// Request offset again
Sntp.offset(function (err, offset) {
console.log(offset); // 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.now(); // System time without offset
Sntp.start(function () {
var now = Sntp.now(); // With offset
Sntp.stop();
});
Advanced Usage
To get more information (the full NtpMessage) parsed on needs to pass the parser
option.
var options = {
parser: Sntp.parseVerbose
};
// Request server time
Sntp.time(options, function (err, time) {
if (err) {
console.log('Failed: ' + err.message);
return;
}
console.log('Local clock is off by: ' + time.t + ' milliseconds');
console.log('Full NtpMessage:', time);
});
This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.