Page 1 of 1

Pausing at a certain time for 1 hour or so..

Posted: Thu Dec 02, 2010 12:28 am
by Exempt
I'm trying to find a good way to force my program to pause for one hour at 1:55 am. I'm having trouble with getting time.h functions to work for this, I guess cause i'm new to it.

Ifi could just figure out how to compare the current time to a time i select that would help a ton. Thanks.

Re: Pausing at a certain time for 1 hour or so..

Posted: Thu Dec 02, 2010 5:50 pm
by Administrator
You need to use time() and difftime().
Example:

Code: Select all

time_t startTime, currentTime;
startTime = time();
currentTime = startTime();

while( difftime(currentTime, startTime) < 3600 ) // 3600 = 60*60, or 1 hour
{
    currentTime = time();
    rest(1);
}

Re: Pausing at a certain time for 1 hour or so..

Posted: Thu Dec 02, 2010 7:03 pm
by Exempt
That would work for every hour checks but how would i check for a certain time as in 2 am - 3am?

Thanks

Re: Pausing at a certain time for 1 hour or so..

Posted: Fri Dec 03, 2010 12:45 am
by Administrator
Same idea, just use mktime() to create your future date and compare it to 'now'.

Code: Select all

time_t startTime, endTime, currentTime;
struct tm *pTm;

startTime = time();
endTime = startTime;

// Change our hour to 2AM, then set our time.
pTm = localtime(&startTime);
pTm->tm_hour = 2; // 2 AM
mktime(pTm);

// Same thing, but with our endTime
pTm = localtime(&endTime);
pTm->tm_hour = 3; // 3AM
mktime(pTm);

// Now we can just check to see if the time is between 2 and 3.
currentTime = time();
while( difftime(currentTime, startTime) > 0 && // Currently it's after starTime...
         difftime(currentTime, endTime) < 0 )     // But before endTime...
{
  currentTime = time();
  rest(1);
}
This code is untested, but the general idea should work.


Oh, and before you just copy and paste, the way this code functions is far from optimal. You should not be using a while loop like that. Instead, you should do something like this:

Code: Select all

int toggle = true;

while( true ) // your main loop
{
    // do your time stuff from above here...
    toggle = ( difftime(currentTime, startTime) < 0 && difftime(currentTime, endTime) > 0 );

    if( toggle ) {
        // Do whatever it is your program should do here
    } else {
        rest(1); // Do nothing.
    }
}

Re: Pausing at a certain time for 1 hour or so..

Posted: Fri Dec 03, 2010 10:38 pm
by Exempt
The struct your using is where i got confused trying to do this myself. I need to need into that more I'm a little lost how it's working the way you have it.

Code: Select all

// Change our hour to 2AM, then set our time.
pTm = localtime(&startTime);
pTm->tm_hour = 2; // 2 AM
mktime(pTm);

// Same thing, but with our endTime
pTm = localtime(&endTime);
pTm->tm_hour = 3; // 3AM
mktime(pTm);
I don't quite understand how pTm is being used with both the start and the end time but I do at least see whats going on. My biggest problem was I didn't want to use the full date I just needed time but doing it like that makes since.

Re: Pausing at a certain time for 1 hour or so..

Posted: Fri Dec 03, 2010 11:38 pm
by Administrator
The struct is simply so you can modify the timestamp easily. Once we set the hour for those, mktime() is used to stuff it back into timestamp with the modified information.