Advertisement

Generating a timestamp

Started by January 31, 2005 02:37 PM
6 comments, last by jflanglois 20 years ago
I am looking for a function that can beused to generate a timestamp on packets. Has anyoneever manually done this? I want to account for clock changes and stuff like that.

The GetSystemTime function retrieves the current system date and time. The system time is expressed in Coordinated Universal Time (UTC). 

VOID GetSystemTime(

    LPSYSTEMTIME lpSystemTime 	// address of system time structure  
   );

typedef struct _SYSTEMTIME {  // st 
    WORD wYear; 
    WORD wMonth; 
    WORD wDayOfWeek; 
    WORD wDay; 
    WORD wHour; 
    WORD wMinute; 
    WORD wSecond; 
    WORD wMilliseconds; 
} SYSTEMTIME;


Is the s a good function to use? [Edited by - try_catch_this on January 31, 2005 8:47:43 PM]
What's wrong with using GetTickCount()? Its not affected by clock changes. The only thing you need to worry about is that it wraps back to 0 every 8 days.
Advertisement
Quote:
Original post by Evil Steve
What's wrong with using GetTickCount()? Its not affected by clock changes. The only thing you need to worry about is that it wraps back to 0 every 8 days.


i believe what hes talking about is synchronizing the time on all machines.

to do this, i dont think your going to want to mess with the system time / date or anything like that. all you should really care about is milliseconds.

ive never done this by hand, since RakNet does it for me, but i believe you want to do something like have the client send GetTickCount() to the server, then the server takes the time shown here, and then finds the ABS of his GetTickCount() and the clients GetTickCount() + the clients average ping time/2. then he has a rough estimate of the time that client holds. (use something more precise then GetTickCount() though!) youd probably want to keep a lookup table mapping that clients connection to a struct that looks something like this:

struct Time_Info{  //whats the difference between this clients time and my (the server) time?  Uint32 time_difference;   //is this clients time greater then our time?  bool is_greater;}; 


you would probably want to do this a bunch of times and average them (or something like that) to get it as accurate as possible.

once you have this lookup table set up, and have done it enough to get it decently accurate, a client can just send his GetTickCount() into the packet. the server does a lookup, grabs that clients Time_Info, and uses that data to calculate how long ago the packet was sent. the server should be responsible for all of this. keep in mind, ive never done this before and just thought of this off the top of my head, so take my hint with a grain of salt. hopefully this pushes you in the right direction though
FTA, my 2D futuristic action MMORPG
@gfilla

All I want is a function that will tell me the time in miliseconds since the epoch.
Jan 1 1970
In UTC time of course.

If I know this time than I can add it to my my packets as a timestamp.

(Only prob with this is the client must have the correct time.)
Why don't you do it as the time since connection of the client?
ftime() or gettimeofday() are candidates for the requirements you gave.
enum Bool { True, False, FileNotFound };
Advertisement
Quote:
Original post by jflanglois
Why don't you do it as the time since connection of the client?


I thought about it somemore and you may have a point there. All I need to know is the time since the game started.
Quote:
Original post by try_catch_this
Quote:
Original post by jflanglois
Why don't you do it as the time since connection of the client?


I thought about it somemore and you may have a point there. All I need to know is the time since the game started.


Well, for that you can have a main timer that starts at server initialization and have the client connections be based on that for inter-connection synchronization.

[edit] Note that I have never done any real server programming, so I'm just throwing ideas out here. Others will undoubtedly be infinitely more qualified to give you suggestions ;)

This topic is closed to new replies.

Advertisement