Public Group

# Best timer (clock) functions to use.

This topic is 2949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,
I am currently using SDL's timer functions and they work great for me so far. However, the timer eventually will go out of range after (42 days) I believe it was. That does seem like a long time I know, but I was wondering if there are better alternatives like 'ctime' or something. Although on paper it looks to me ctime will share the same 42 days flaw, unless someone knows a workaround.

##### Share on other sites

Hello,
I am currently using SDL's timer functions and they work great for me so far. However, the timer eventually will go out of range after (42 days) I believe it was. That does seem like a long time I know, but I was wondering if there are better alternatives like 'ctime' or something. Although on paper it looks to me ctime will share the same 42 days flaw, unless someone knows a workaround.

Do you expect your game to be running on someones machine for more than 42 days? Does SDL's timer work the way you want / need it to?

##### Share on other sites
Use a platform-dependent clock. In POSIX systems, I would use this:
#include <time.h> double get_time_in_seconds() { struct timespec t; clock_gettime(CLOCK_REALTIME, &t); return t.tv_sec + 1e-9 * t.tv_nsec; } 

##### Share on other sites
Perhaps i'm being a little eccentric, but I like to not be limited. Which is why I chose SDL and OpenGL in the first place. Otherwise, I would have picked a single platform to work with. For me, as a programmer, I am inquisitive about learning another way that isn't limited to 42 days.

##### Share on other sites

Use a platform-dependent clock. In POSIX systems, I would use this:
#include <time.h> double get_time_in_seconds() { struct timespec t; clock_gettime(CLOCK_REALTIME, &t); return t.tv_sec + 1e-9 * t.tv_nsec; } 

Ooh, I like that. How is that platform dependent though? Would time.h not work the same on multiple OS?

##### Share on other sites

Perhaps i'm being a little eccentric, but I like to not be limited. Which is why I chose SDL and OpenGL in the first place. Otherwise, I would have picked a single platform to work with. For me, as a programmer, I am inquisitive about learning another way that isn't limited to 42 days.

It's actually 49 days.

If you use SDL to abstract away the differences between platforms, you have to live with the decisions that the SDL writers made, including the fact that the timer function returns a 32-bit number of milliseconds.

I normally keep one module for things that are platform specific. For that I provide a single header file and several implementations. When I've done this, I've often ended up with an implementation for Windows, one for POSIX systems and a fallback implementation for any other system, although I don't know what those systems would be. This might be the only function you need to write in that module.

##### Share on other sites

Ooh, I like that. How is that platform dependent though? Would time.h not work the same on multiple OS?

clock_gettime is not part of C or C++. It is part of the POSIX specification, which is followed by pretty much every OS except for Windows. You need some other way of doing it for Windows.

##### Share on other sites

It's actually 49 days.

I normally keep one module for things that are platform specific. For that I provide a single header file and several implementations. When I've done this, I've often ended up with an implementation for Windows, one for POSIX systems and a fallback implementation for any other system, although I don't know what those systems would be. This might be the only function you need to write in that module.

Very good idea alvaro : )
So then it would only be windows machines that crash after "49 Days".

##### Share on other sites

[quote name='alvaro' timestamp='1316723757' post='4864842']
It's actually 49 days.

I normally keep one module for things that are platform specific. For that I provide a single header file and several implementations. When I've done this, I've often ended up with an implementation for Windows, one for POSIX systems and a fallback implementation for any other system, although I don't know what those systems would be. This might be the only function you need to write in that module.

Very good idea alvaro : )
So then it would only be windows machines that crash after "49 Days".
[/quote]

Thanks. You can write an implementation for Windows around QueryPerformanceCounter. It shouldn't be too hard.

##### Share on other sites
This is actually good news for me, because I was thinking of eventually implementing a client/server and the server would be UNIX anyway. Still 49 days is probably a really long time for a server to run and a client would probably be extremely absurd to run for that long.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 21
• 11
• 25