tags:

views:

377

answers:

5

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.

A: 

You just need to store the date/time when application started. Whenever you need to display for how long your program is running get current date/time and subtract the when application started.

see me no more
+1  A: 

I believe this is what you're looking for.

Evän Vrooksövich
+5  A: 

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.

Michael Burr
`time()` returns wall-clock time, `clock()` returns processor-time.
Michael Burr
+1 Man, I get confused by the date/time stuff always :)
AraK
`clock()` could be useful, too... Since the OP is only interested in elapsed time, wall time may not be necessary. Additionally, for long-running programs, `time()` can be influenced by things like NTP drifts, DST, user changes, etc... That may throw off the results.
jheddings
@AraK why'd you delete your answer?
jheddings
@jheddings To be honest, I am not really sure about the answer, and Michael explained the difference. So, maybe I have to study the library better so I give a better answer. Sorry for the off-topic comment.
AraK
There's no doubt that there can be an awful lot of subtly when dealing with time, depending on what you want the time of, how accurate you want it, if you need it in calendar form, etc. But since the OP was "talking very simple here", I figured it wasn't necessary to complicate the answer with all the rigmarole. Sometimes people just want a seconds counter.
Michael Burr
Thanks Michael, I think that will work perfectly.
Noob
A: 

You are approaching it backwards. Instead of having a variable you have to worry about updating every second, just initialize a variable on program start with the current time, and then whenever you need to know how many seconds have elapsed, you subtract the now current time from that initial time. Much less overhead that way, and no need to nurse some timing related variable update.

kaleidomedallion
+1  A: 
#include <stdio.h>
#include <time.h>
#include <windows.h>
using namespace std;
void wait ( int seconds );
int main ()
{
  time_t start, end;
  double diff;
  time (&start); //useful call
  for (int i=0;i<10;i++) //this loop is useless, just to pass some time.
  {
  printf ("%s\n", ctime(&start));
  wait(1);
  }
  time (&end);//useful call

  diff = difftime(end,start);//this will give you time spent between those two calls.
  printf("difference in seconds=%f",diff); //convert secs as u like
  system("pause");
  return 0;
}
void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

this should work fine on solaris/unix also, just remove win refs

calvin