Initial commit
[yaffs-website] / node_modules / sntp / README.md
1 # sntp
2
3 An SNTP v4 client (RFC4330) for node. Simpy connects to the NTP or SNTP server requested and returns the server time
4 along with the roundtrip duration and clock offset. To adjust the local time to the NTP time, add the returned `t` offset
5 to the local time.
6
7 [![Build Status](https://secure.travis-ci.org/hueniverse/sntp.png)](http://travis-ci.org/hueniverse/sntp)
8
9 # Usage
10
11 ```javascript
12 var Sntp = require('sntp');
13
14 // All options are optional
15
16 var options = {
17     host: 'nist1-sj.ustiming.org',  // Defaults to pool.ntp.org
18     port: 123,                      // Defaults to 123 (NTP)
19     resolveReference: true,         // Default to false (not resolving)
20     timeout: 1000                   // Defaults to zero (no timeout)
21 };
22
23 // Request server time
24
25 Sntp.time(options, function (err, time) {
26
27     if (err) {
28         console.log('Failed: ' + err.message);
29         process.exit(1);
30     }
31
32     console.log('Local clock is off by: ' + time.t + ' milliseconds');
33     process.exit(0);
34 });
35 ```
36
37 If an application needs to maintain continuous time synchronization, the module provides a stateful method for
38 querying the current offset only when the last one is too old (defaults to daily).
39
40 ```javascript
41 // Request offset once
42
43 Sntp.offset(function (err, offset) {
44
45     console.log(offset);                    // New (served fresh)
46
47     // Request offset again
48
49     Sntp.offset(function (err, offset) {
50
51         console.log(offset);                // Identical (served from cache)
52     });
53 });
54 ```
55
56 To set a background offset refresh, start the interval and use the provided now() method. If for any reason the
57 client fails to obtain an up-to-date offset, the current system clock is used.
58
59 ```javascript
60 var before = Sntp.now();                    // System time without offset
61
62 Sntp.start(function () {
63
64     var now = Sntp.now();                   // With offset
65     Sntp.stop();
66 });
67 ```
68