hi,

to get the current time you use this statement: time_t now = time(0); i was wondering how can i check the time stored in this variable if it falls in a specific time interval, i.e. between 5 and 6 am.
I am thinking one way to do this is to convert the now variable to a structure tm* localtm = localtime(&now); and then check the time field of the structure, but i was wondering if there is a faster way to do this....

thanks for your help.

> but i was wondering if there is a faster way to do this
Why do you need it to be faster?
Seems like a case of POD

If you need to do it a lot, then construct (with mktime) two time_t's which are 5am and 6am. Then do difftime() to see if you're in that range.

5:00 am is (60*60*5) (60 seconds in an minute times 60 minutes in an hour) = 18000 seconds after midnight. 6:00 am is (60*60*6) = 21600 seconds. After getting local time, convert the hour, minutes and seconds to an integer ((hour-1) * 60 + (minute-1) * 60 + seconds), then check if it is between 18,000 and 21,600.

>
....
Seems like a case of POD

If you need to do it a lot, then construct (with mktime) two time_t's which are 5am and 6am. Then do difftime() to see if you're in that range.

thanks for the link, it was an interesting aspect...:)

5:00 am is (60*60*5) (60 seconds in an minute times 60 minutes in an hour) = 18000 seconds after midnight. 6:00 am is (60*60*6) = 21600 seconds. After getting local time, convert the hour, minutes and seconds to an integer ((hour-1) * 60 + (minute-1) * 60 + seconds), then check if it is between 18,000 and 21,600.

both of the answers don't solve my problem because basically what i wanted to do is pass the current time {in time_t format} in a function, that will check {among other things} if the time is between 5 and 6 am....

both of the answers don't solve my problem because basically what i wanted to do is pass the current time {in time_t format} in a function, that will check {among other things} if the time is between 5 and 6 am....

you must not have comprehended what we posted because the suggestions will do just what you want.

int foo(time_t time_to_check)
{
    struct tm *tm = localtime(time_to_check);
    const time_t five_am = 18000;
    const time_t six_am = 21600;
    // convert current time into seconds since midnight
    time_t now = ( (tm->tm_hour-1) * 60 * 60) 
                         + ((tm->tm_min-1) * 60)
                         + tm_tm_sec;
   if( now >= five_am && now <= six_am)
   {

          cout << "Great time";
   }

}

It may not be as simple as it sounds.
time() returns seconds elapsed since epoch. This means that 5am and 6am by themselves can not be compared with what is returned by time() because they are relative times. (relative to start of today).
So @n.aggel => what is the date associated with given 5am and 6am?
When you have the reference date you gotta convert that to format returned by time() and this will not be very small amount of code, may be 10-15 lines need to be written.
If you are sure that time returned by time() AND the 5am and 6am always belong to the same day then following code will work for your purpose:

const time_t NUM_SEC_IN_A_DAY = 24*60*60, FIVE_AM = 5*60*60, SIX_AM = 6*60*60 ;

time_t epoch_time = time(0) ;
time_t seconds_elapsed_today = epoch_time % NUM_SEC_IN_A_DAY ;

if( FIVE_AM <= seconds_elapsed_today <= SIX_AM )
     cout << "Current time is between 5 and 6 in morning." << endl ;
else
     cout << "Current time is NOT between 5 and 6 in morning." << endl ;

>>if( FIVE_AM <= seconds_elapsed_today <= SIX_AM )
If you want to post code, please at least post valid code.

>>time_t seconds_elapsed_today = epoch_time % NUM_SEC_IN_A_DAY ;
Good idea :) , which I had thought of that too.

>>if( FIVE_AM <= seconds_elapsed_today <= SIX_AM )
If you want to post code, please at least post valid code.

>>time_t seconds_elapsed_today = epoch_time % NUM_SEC_IN_A_DAY ;
Good idea :) , which I had thought of that too.

Sorry but I didn't get what's wrong with "if( FIVE_AM <= seconds_elapsed_today <= SIX_AM )" ?

you must not have comprehended what we posted because the suggestions will do just what you want.

int foo(time_t time_to_check)
{
    struct tm *tm = localtime(time_to_check);
    const time_t five_am = 18000;
    const time_t six_am = 21600;
    // convert current time into seconds since midnight
    time_t now = ( (tm->tm_hour-1) * 60 * 60) 
                         + ((tm->tm_min-1) * 60)
                         + tm_tm_sec;
   if( now >= five_am && now <= six_am)
   {

          cout << "Great time";
   }

}

my fault, i din't say correctly what i needed:)
what i wanted is to pass a time_t argument to a function, named funcA{that it's main purpose is irrelevant to time} and then do a check based on the time_t argument{but without constracting a tm structure....because the function's purpose is other than time checking}...

what i did is create another function :

int getHour(time_t now)
{
    tm* localtm = localtime(&now);   

    return localtm->tm_hour;    
}

so now i give to the other function{funcA} a simple integer that contains only a number from 0 to 23.... :)

thanks again for your help

PS can anyone explain this line

time_t seconds_elapsed_today = epoch_time % NUM_SEC_IN_A_DAY ;

why use this operator % ??

>>so now i give to the other function{funcA} a simple integer that contains only a number from 0 to 23....

that is not a time_t variables that is initialized from time() function. If all you are going to do is pass an integer between 0 and 23 then the check is trivial.

bool foo(int hourl)
{
   if( hour >= 5 && hour <= 5)
       return true;
   return false;
}

>>PS can anyone explain this line
The mod operator returns the remainder after division. In this case it will return the number of seconds that has passed since midnight. The time() function returns the number of seconds since 1 Jan 1970 (I think that's the date). There are 86,400 seconds in a 24 hour day and the remainder of any number divided by 86400 is the number of seconds in the current day.

Sorry but I didn't get what's wrong with "if( FIVE_AM <= seconds_elapsed_today <= SIX_AM )" ?

Illegal syntax -- here is the correction if( FIXE_AM <= seconds_elapsed_today && seconds_elapsed_today <= SIX_AM) or if( seconds_elapsed_today >= FIXE_AM && seconds_elapsed_today <= SIX_AM)

This question has already been answered. Start a new discussion instead.