-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNTP.js
104 lines (98 loc) · 3.1 KB
/
NTP.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
NTP.js http://jehiah.cz/a/ntp-for-javascript
copyright Jehiah Czebotar jehiah@gmail.com
licensed under http://unlicense.org/ please modify as needed
to use configure serverUrl to an endpoint that when queried
GET serverUrl + '?t=' + timestamp_in_miliseconds
returns
time_offset_in_miliseconds + ':' + argument_t
*/
var NTP = {
cookieShelfLife : 7, //7 days
requiredResponses : 2,
serverTimes : new Array,
serverUrl : "/site_that_responds.html",
resyncTime : 10, // minutes
sync : function(){
// if the time was set within the last x minutes; ignore this set request; time was synce recently enough
var offset = NTP.getCookie("NTPClockOffset");
if (offset){try{
var t = offset.split("|")[1];
var d = NTP.fixTime()-parseInt(t,10);
if (d < (1000 * 60 * NTP.resyncTime)){return false;} // x minutes; return==skip
}catch(e){}
}
NTP.serverTimes = new Array;
NTP.getServerTime();
},
getNow : function(){
var date = new Date();
return date.getTime();
return (date.getTime() + (date.getTimezoneOffset() * 60000));
},
parseServerResponse : function(data){
var offset = parseInt(data.responseText.split(":")[0]);
var origtime = parseInt(data.responseText.split(":")[1]);
var delay = ((NTP.getNow() - origtime) / 2);
offset = offset - delay;
NTP.serverTimes.push(offset);
// if we have enough responces set cookie
if (NTP.serverTimes.length >= NTP.requiredResponses){
// build average
var average = 0;
var i=0;
for (i=0; i < NTP.serverTimes.length;i++){
average += NTP.serverTimes[i];
}
average = Math.round(average / i);
NTP.setCookie("NTPClockOffset",average); // set the new offset
NTP.setCookie("NTPClockOffset",average+'|'+NTP.fixTime()); // save the timestamp that we are setting it
}
else{
NTP.getServerTime();
}
},
getServerTime : function(){
try{
var req = new Ajax.Request(NTP.serverUrl,{
onSuccess : NTP.parseServerResponse,
method : "get",
parameters : "t=" + NTP.getNow()
});
}
catch(e){
return false;
//prototype.js not available
}
},
setCookie : function(aCookieName,aCookieValue){
var date = new Date();
date.setTime(date.getTime() + (NTP.cookieShelfLife * 24*60*60*1000));
var expires = '; expires=' + date.toGMTString();
document.cookie = aCookieName + '=' + aCookieValue + expires + '; path=/';
},
getCookie : function(aCookieName){
var crumbs = document.cookie.split('; ');
for (var i = 0; i < crumbs.length; i++)
{
var crumb = crumbs[i].split('=');
if (crumb[0] == aCookieName && crumb[1] != null)
{
return crumb[1];
}
}
return false;
},
fixTime : function(timeStamp){
if(!timeStamp){timeStamp = NTP.getNow();}
var offset = NTP.getCookie("NTPClockOffset") ;
try{
if (!offset){offset = 0;}else{offset=offset.split("|")[0];}
if (isNaN(parseInt(offset,10))){return timeStamp;}
return timeStamp + parseInt(offset,10);
}catch(e){
return timeStamp;
}
}
}
setTimeout('NTP.sync()',2500);