Skip to content

Commit b3a8095

Browse files
committed
Initial commit, works nicely.
0 parents  commit b3a8095

12 files changed

+306
-0
lines changed

.gitattributes

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
*.sln merge=union
7+
*.csproj merge=union
8+
*.vbproj merge=union
9+
*.fsproj merge=union
10+
*.dbproj merge=union
11+
12+
# Standard to msysgit
13+
*.doc diff=astextplain
14+
*.DOC diff=astextplain
15+
*.docx diff=astextplain
16+
*.DOCX diff=astextplain
17+
*.dot diff=astextplain
18+
*.DOT diff=astextplain
19+
*.pdf diff=astextplain
20+
*.PDF diff=astextplain
21+
*.rtf diff=astextplain
22+
*.RTF diff=astextplain

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
*.sublime-workspace
3+
.idea

.jshintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"node": true,
3+
"strict": true
4+
}

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: node_js
2+
3+
node_js:
4+
- "0.8"
5+
- "0.10"
6+
before_script:
7+
- npm install -g grunt-cli
8+
9+
notifications:
10+
email: false

Gruntfile.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
module.exports = function (grunt) {
4+
5+
// Project configuration.
6+
grunt.initConfig({
7+
nodeunit: {
8+
files: ['test/**/*_test.js']
9+
},
10+
jshint: {
11+
options: {
12+
jshintrc: '.jshintrc'
13+
},
14+
gruntfile: {
15+
src: 'Gruntfile.js'
16+
},
17+
lib: {
18+
src: ['lib/**/*.js']
19+
},
20+
test: {
21+
src: ['test/**/*.js']
22+
}
23+
},
24+
watch: {
25+
gruntfile: {
26+
files: '<%= jshint.gruntfile.src %>',
27+
tasks: ['jshint:gruntfile']
28+
},
29+
lib: {
30+
files: '<%= jshint.lib.src %>',
31+
tasks: ['jshint:lib', 'nodeunit']
32+
},
33+
test: {
34+
files: '<%= jshint.test.src %>',
35+
tasks: ['jshint:test', 'nodeunit']
36+
}
37+
}
38+
});
39+
40+
// These plugins provide necessary tasks.
41+
grunt.loadNpmTasks('grunt-contrib-nodeunit');
42+
grunt.loadNpmTasks('grunt-contrib-jshint');
43+
grunt.loadNpmTasks('grunt-contrib-watch');
44+
45+
// Default task.
46+
grunt.registerTask('default', ['jshint', 'nodeunit']);
47+
48+
};

LICENSE-MIT

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2013 Clément Bourgeois
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ntp-client [![Build Status](https://secure.travis-ci.org/moonpyk/node-ntp-client.png?branch=master)](http://travis-ci.org/moonpyk/node-ntp-client)
2+
3+
Pure Javascript implementation of the NTP Client Protocol
4+
5+
## Getting Started
6+
Install the module with: `npm install ntp-client`
7+
8+
```javascript
9+
var ntpClient = require('ntp-client');
10+
11+
ntpClient.getNetworkTime("pool.ntp.org", 123, function(err, date) {
12+
if(err) {
13+
console.error(err);
14+
return;
15+
}
16+
17+
console.log("Current time : ");
18+
console.log(date); // Mon Jul 08 2013 21:31:31 GMT+0200 (Paris, Madrid (heure d’été))
19+
});
20+
```
21+
22+
## License
23+
Copyright (c) 2013 Clément Bourgeois
24+
Licensed under the MIT license.

bin/node-ntp-client

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../lib/ntp-client.js').demo(process.argv);

lib/ntp-client.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* ntp-client
3+
* https://github.com/moonpyk/node-ntp
4+
*
5+
* Copyright (c) 2013 Clément Bourgeois
6+
* Licensed under the MIT license.
7+
*/
8+
9+
(function (exports) {
10+
"use strict";
11+
12+
var dgram = require('dgram');
13+
14+
exports.defaultNtpPort = 123;
15+
exports.defaultNtpServer = "pool.ntp.org";
16+
17+
/**
18+
* @param {string} server IP/Hostname of the remote NTP Server
19+
* @param {number} port Remote NTP Server port number
20+
* @param {function(Object, Date)} callback
21+
*/
22+
exports.getNetworkTime = function (server, port, callback) {
23+
var client = dgram.createSocket("udp4"),
24+
ntpData = new Buffer(48);
25+
26+
ntpData[0] = 0x1B; // RFC 2030
27+
28+
for (var i = 1; i < 48; i++) {
29+
ntpData[i] = 0;
30+
}
31+
32+
client.send(ntpData, 0, ntpData.length, port, server, function (err) {
33+
if (err) {
34+
callback(err, null);
35+
client.close();
36+
return;
37+
}
38+
39+
client.on('message', function (msg) {
40+
client.close();
41+
42+
// Offset to get to the "Transmit Timestamp" field (time at which the reply
43+
// departed the server for the client, in 64-bit timestamp format."
44+
var offsetTransmitTime = 40,
45+
intpart = 0,
46+
fractpart = 0;
47+
48+
// Get the seconds part
49+
for (var i = 0; i <= 3; i++) {
50+
intpart = 256 * intpart + msg[offsetTransmitTime + i];
51+
}
52+
53+
// Get the seconds fraction
54+
for (i = 4; i <= 7; i++) {
55+
fractpart = 256 * fractpart + msg[offsetTransmitTime + i];
56+
}
57+
58+
var milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000);
59+
60+
// **UTC** time
61+
var date = new Date("Jan 01 1900 GMT");
62+
date.setUTCMilliseconds(date.getUTCMilliseconds() + milliseconds);
63+
64+
callback(err, date);
65+
});
66+
});
67+
};
68+
69+
exports.demo = function (argv) {
70+
exports.getNetworkTime(
71+
exports.defaultNtpServer,
72+
exports.defaultNtpPort,
73+
function (err, date) {
74+
if (err) {
75+
console.error(err);
76+
return;
77+
}
78+
79+
console.log(date);
80+
});
81+
};
82+
}(exports));

node-ntp-client.sublime-project

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"folders": [
3+
{
4+
"path": ".",
5+
"file_exclude_patterns": ["*.sublime-workspace", "*.project"],
6+
"folder_exclude_patterns": ["*.*"]
7+
}
8+
],
9+
"ternjs": {
10+
"exclude": ["node_modules/**"]
11+
}
12+
}

0 commit comments

Comments
 (0)