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.
Pausing at a certain time for 1 hour or so..
- Administrator
- Site Admin
- Posts: 5313
- Joined: Sat Jan 05, 2008 4:21 pm
Re: Pausing at a certain time for 1 hour or so..
You need to use time() and difftime().
Example:
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..
That would work for every hour checks but how would i check for a certain time as in 2 am - 3am?
Thanks
Thanks
- Administrator
- Site Admin
- Posts: 5313
- Joined: Sat Jan 05, 2008 4:21 pm
Re: Pausing at a certain time for 1 hour or so..
Same idea, just use mktime() to create your future date and compare it to 'now'.
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
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);
}
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..
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.
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.
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);
- Administrator
- Site Admin
- Posts: 5313
- Joined: Sat Jan 05, 2008 4:21 pm
Re: Pausing at a certain time for 1 hour or so..
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.
Who is online
Users browsing this forum: No registered users and 0 guests