C++ Keeping track of how many seconds has passed since start of program


I am writing a program that will be used on a Solaris machine. I need a way of keeping track of how many seconds has passed since the start of the program. I'm talking very simple here. For example I would have an int seconds = 0; but how would I go about updating the seconds variable as each second passes?

It seems that some of the various time functions that I've looked at only work on Windows machines, so I'm just not sure.

Any suggestions would be appreciated.

Thanks for your time.

11/14/2009 7:24:23 PM

A very simple method:

#include <time.h>
time_t start = time(0);

double seconds_since_start = difftime( time(0), start);

The main drawback to this is that you have to poll for the updates. You'll need platform support or some other lib/framework to do this on an event basis.

11/14/2009 7:33:46 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow