Forum ‹ ActionScript 3.0 ‹ Getting Accurate Epoch Time
10 posts
• Page 1 of 1
Getting Accurate Epoch Time
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!
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
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: 570
- Joined: January 4th, 2010, 1:53 pm
Re: Getting Accurate Epoch Time
- 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: 116
- Joined: March 29th, 2011, 12:31 am
Re: Getting Accurate Epoch Time
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: 570
- Joined: January 4th, 2010, 1:53 pm
Re: Getting Accurate Epoch Time
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!
'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
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/
Have more questions? Join us at #player.io on the freenode irc network. Making something cool with Player.IO? I would love to talk! My Skype handle is q-rious
-

Benjaminsen - .IO
- Posts: 808
- Joined: January 12th, 2010, 11:54 am
- Location: Denmark
Re: Getting Accurate Epoch Time
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
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
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!
Have more questions? Join us at #player.io on the freenode irc network. Making something cool with Player.IO? I would love to talk! My Skype handle is q-rious
-

Benjaminsen - .IO
- Posts: 808
- Joined: January 12th, 2010, 11:54 am
- Location: Denmark
Re: Getting Accurate Epoch Time
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 ^_^
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
It worked perfectly!
Thanks!
Thanks!
- mfranzs
- Posts: 98
- Joined: August 29th, 2010, 3:27 am
10 posts
• Page 1 of 1