i am developing an online auction system in c......can someone pls help me to help me setup a timer that runs in the background
(or)
i dont know how to store the time/date so that it can be compared with current date/time.....
Thanks

Recommended Answers

All 4 Replies

use the functions in time.h.

>> dont know how to store the time/date so that it can be compared with current date/time

use the time_t (normally an unsigned int) that is returned by time() for that. initialize a time_t object when starting then get it again in another variable, then simply compare the two integers. Something like this to wait for 2 minutes.

time_t t1, t2;

// initialize 
t1 = time(0);
t2 = t1 + (2*60); // want a 2 minute timer
while( t1 < t2)
{
   t1 = time();
   // do something here   
}

use the time_t (normally an unsigned int)

Really? I've always been annoyed by it being a signed long.

>t2 = t1 + (2*60); // want a 2 minute timer
>while( t1 < t2)
How wonderfully pointless if you want a general solution. While time_t is an arithmetic type, the size and range are implementation-defined and contents are unspecified. The only thing you can assume is that you can compare a time_t with (time_t)-1. For a more portable solution you can defer to the standard library instead of doing the magic yourself:

time_t start = time ( 0 );

while ( difftime ( time ( 0 ), start ) < ( 2 * 60 ) )
  ;

Really? I've always been annoyed by it being a signed long.

actually, on the two compilers I use its now a 64-bit int because int isn't big enough any more.

>>How wonderfully pointless if you want a general solution
Where did you see anyone say they wanted a "general solution"? I have never seen an os where my solution didn't work. But then I have not worked with all operating systems either, so I suppose there is a remote obscure chance that it won't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.