Alex Rivera | Logout

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

Asked 2009-11-14T19:24:23.573
19

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.

Edit
Report

1 Answer

29

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.

answered 2009-11-14T19:28:26.837

Your Answer