Forum ActionScript 3.0 Getting Accurate Epoch Time

Problems and discussions relating to ActionScript 3.0 here.

Getting Accurate Epoch Time

Postby mfranzs » May 26th, 2011, 9:18 pm

Hi-

So today I went to test my game on a friends computer. After opening the game, the game waited to load, got the loading message, then waited for the correct loading time (whatever epoch time the server specified).

It didn't work.

After some debugging, I realized that the friend's computer's time was set one day late
(Today is the 26th, and it was set for the 27th... im not sure how windows messed this up)

Right now, I'm using (new Date()).getTime() to get epoch time. Unfortunately, this seems clock dependent.

Does anyone have a workaround for this / a different (and correct) method of getting epoch time?

Thanks!
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Getting Accurate Epoch Time

Postby Henrik » May 27th, 2011, 7:59 am

Well, if the clock on the machine the code is running on is wrong, you're always going to get the wrong time as long as you ask that. You can't magically get the right time, unless you ask another computer, for example our multiplayer servers. :-)
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Getting Accurate Epoch Time

Postby cjcenizal » May 27th, 2011, 8:10 am

Code: Select all
public static function getUTC():Number {
         var now:Date = new Date();
         var epoch:Number = Math.round( now.time * .001 );
         return epoch;
      }


EDIT: Just realized Henrik is right, lol. Too tired. The above method won't help if your system clock is wrong, sorry!
cjcenizal
Paid Member
 
Posts: 115
Joined: March 29th, 2011, 12:31 am

Re: Getting Accurate Epoch Time

Postby Henrik » May 27th, 2011, 8:53 am

Exactly, getting the UTC from the system clock, or getting the number of seconds or milliseconds since 1970-01-01, all depend on the system clock being accurate in the first place. :-)
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Getting Accurate Epoch Time

Postby mfranzs » May 27th, 2011, 8:20 pm

Yeah :/

'Do you guys have any tips with syncing the time with the server?

How can I determine how long a message takes to get from the server to the client?

Thanks guys!
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Getting Accurate Epoch Time

Postby Benjaminsen » May 27th, 2011, 10:14 pm

mfranzs wrote:Yeah :/

'Do you guys have any tips with syncing the time with the server?

How can I determine how long a message takes to get from the server to the client?

Thanks guys!


Clock synchronization is a whole science in on it self. A good place to start is here: http://en.wikipedia.org/wiki/Clock_synchronization

Additionally I used synchronized clocks for this, now rather outdated, open source Player.IO project:
http://code.google.com/p/multiplayer-asteroids/
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Getting Accurate Epoch Time

Postby mfranzs » May 27th, 2011, 10:24 pm

Is the round trip time on playerIO generally split into equal for to server and from server?

Like 100 rtt would be 50 ms to server and 50 ms to respond
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Getting Accurate Epoch Time

Postby Benjaminsen » May 27th, 2011, 10:31 pm

mfranzs wrote:Is the round trip time on playerIO generally split into equal for to server and from server?

Like 100 rtt would be 50 ms to server and 50 ms to respond


In most cases this will be the case. Most of the delay in any messages comes from distance + inherent lag in network equipment. When you have a synchronized timestamp you can even measure where the time is used yourself!
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Getting Accurate Epoch Time

Postby mfranzs » May 27th, 2011, 10:43 pm

Great, thanks.

I'm going to attempt to use this one:
http://en.wikipedia.org/wiki/Cristian%27s_algorithm

If I shouldn't, please tell me ^_^
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Getting Accurate Epoch Time

Postby mfranzs » May 28th, 2011, 9:34 pm

It worked perfectly!

Thanks!
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am


Return to ActionScript 3.0