0

I logged the X server time and js time at the same time. I got these values:

"gdk_x11_get_server_time()": 4413960
"js time-new Date().getTime():" 1425529054961

Is there anyway I can make the x server time from javascript? (so in above im trying to get the 4413960 from js) without the need to tap into ctypes or something and use gdk_x11_get_server_time ()?

Thanks

Here are some time dumps from x11 and then from js, with the js time toLocaleString at start. it shows them spaced out at 10s for 0, 10, and 20s.

"time now is:" "10:26:39 PM" "gdk_x11_get_server_time:" 173753 "new Date().getTime():" 1425536800642
"time now is:" "10:26:50 PM" "gdk_x11_get_server_time:" 183757 "new Date().getTime():" 1425536810644
"time now is:" "10:27:00 PM" "gdk_x11_get_server_time:" 193758 "new Date().getTime():" 1425536820644

more data:

study set   gdk time    js time js to string    gdk time diff   js time diff    stamp diff (s)
a   173753  1425536800642   10:26:39 PM         
a   183757  1425536810644   10:26:50 PM 10004   10002   11
a   193758  1425536820644   10:27:00 PM 10001   10000   10
b   771689  1425537398575   10:36:38 PM         
b   771690  1425537398576   10:36:38 PM 1   1   00
b   781690  1425537408576   10:36:48 PM 10000   10000   10
b   791690  1425537418576   10:36:58 PM 10000   10000   10
b   801690  1425537428576   10:37:08 PM 10000   10000   10
b   811690  1425537438576   10:37:18 PM 10000   10000   10
b   821690  1425537448576   10:37:28 PM 10000   10000   10
b   831691  1425537458577   10:37:38 PM 10001   10001   10
b   841691  1425537468578   10:37:48 PM 10000   10001   10
b   851691  1425537478577   10:37:58 PM 10000   9999    10
Noitidart
  • 35,443
  • 37
  • 154
  • 323

2 Answers2

0

hope this helps :)

 var timestamp = Math.floor( Date.now() / 1000);  
Wreeecks
  • 2,186
  • 1
  • 33
  • 53
  • It's not the same :( this is what it gave me: `"gdk_x11_get_server_time:" 4765773 "new Date().getTime()" 1425529406768 "Math.floor( Date.now() / 1000):" 1425529406` im tryong to get that `4765773` from js – Noitidart Mar 05 '15 at 04:23
  • you doing this? http://stackoverflow.com/questions/8297369/guint32-date-to-timestamp-in-php – Wreeecks Mar 05 '15 at 04:43
  • Thanks @bwa I'm doing something similar. He has the server time and wants to make it a php time which is i think same as js time without the milliseocnds. So i have the js time in milliseconds and I want to get the x server time please. – Noitidart Mar 05 '15 at 04:45
  • 1
    good luck, i have no idea what you are doing. i thought you only need generate timestamp using JS. – Wreeecks Mar 05 '15 at 04:47
0

we can tell that the unit is ms, but the offset is unknown. it seems that the server time is the # of ms since its local midnight. if we can capture just one servertime snapshot, we can memorize the offset and compute a server timestamp at-will. the only caveat with the simple code below is that the generated timestamp function needs to be run at least once every 24 hours, or you can periodically re-generate a new timer from a fresh server timestamp to combat DST, lag, clock adjustments, etc.

you call the getTimer() with a server timestamp, it returns a new function that works just like Date.now(), but (hopefully) returns the server timestamp instead of the UTC timestamp (+/- any lag).

function getTimer(serverTime){
  var now=Date.now(), max=1000*60*60*24;
  serverTime*=1; //force number
 return function(){  
    var diff=(Date.now()-now)+serverTime;  
    if(diff<max) return diff;
    now+=max;
    return (Date.now()-now)+serverTime;
 };
}

var serverClock=getTimer(4413960); // make a new timer function
setTimeout(function(){console.info(serverClock());  }, 5000); 
console.info(n=serverClock()); // should be ~5000 more than first time

EDIT: ok, if it doesn't reset every 24 hours, you'll need to manually run the generator every time it does reset. if that's easy for you, then you can simplify the generator somewhat:

function getTimer(serverTime){
  var now=Date.now();
  serverTime*=1; //force number
 return function(){  
    return (Date.now()-now)+serverTime;
 };
}

you may want to re-sync more often than once a day or upon reboot, but at least with this, you don't need to bother the server every time, you can calc it locally with some reliability.

dandavis
  • 16,370
  • 5
  • 40
  • 36