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

Ask questions about cheating in any games you would like. Does not need to pertain to MicroMacro.
Post Reply
Message
Author
Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

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

#1 Post by Exempt » Thu Dec 02, 2010 12:28 am

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.

User avatar
Administrator
Site Admin
Posts: 4863
Joined: Sat Jan 05, 2008 4:21 pm

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

#2 Post by Administrator » Thu Dec 02, 2010 5:50 pm

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);
}

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

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

#3 Post by Exempt » Thu Dec 02, 2010 7:03 pm

That would work for every hour checks but how would i check for a certain time as in 2 am - 3am?

Thanks

User avatar
Administrator
Site Admin
Posts: 4863
Joined: Sat Jan 05, 2008 4:21 pm

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

#4 Post by Administrator » Fri Dec 03, 2010 12:45 am

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.
    }
}

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

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

#5 Post by Exempt » Fri Dec 03, 2010 10:38 pm

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.

User avatar
Administrator
Site Admin
Posts: 4863
Joined: Sat Jan 05, 2008 4:21 pm

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

#6 Post by Administrator » Fri Dec 03, 2010 11:38 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest