Time between allow else error...

Sorry to ask what i thought would be an easy to find answer LOL.

I am trying to do the following...

if current time is between 07:00-09:00 allow the following for that time then continue to return NOk until next day between same time.

everything i read is finding time between 2 times or adding a sleep timer. im after a block between set times based on current time.

1
2
3
4
5
6
7
  	if (ud->idx[2] == 0x1A && ud->idx[3] == 0x2A)
	{
	logscreen("Allowed Between 07:00-09:00");  
 	return OK; 
	}else return NOk;



I do thank you in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <chrono>
#include <ctime>

int main ()
{
    using namespace std::chrono;
    //http://stackoverflow.com/questions/13439401/why-does-chrono-have-its-own-namespace

    std::time_t current =  system_clock::to_time_t ( system_clock::now() );
    //converts current time into an object of the traditional time type of C and POSIX, type time_t

    struct std::tm local_time = *localtime(&current);//local time conversion
    //struct std::tm gm_time = *gmtime(&current);//greenwich mean time conversion

    //time_t needs to be converted to type tm which has data members for hour, min, sec, etc
    if((local_time.tm_hour >= 7)&&(local_time.tm_hour <= 9))
    {
        std::cout << "OK \n";
    }
    else
    {
        std::cout << "Not OK \n";
    }
}
> if((local_time.tm_hour >= 7)&&(local_time.tm_hour <= 9))

Need to also check tm_min and tm_sec - current time may be 09:32:46

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <ctime>

static std::time_t today_at( int hh, int mm = 0 )
{
    const auto now = std::time(nullptr) ;
    auto tm = *std::localtime( std::addressof(now) ) ;
    tm.tm_hour = hh ;
    tm.tm_min = mm ;
    tm.tm_sec = 0 ;
    return std::mktime( std::addressof(tm) ) ;
}

static bool is_login_allowed_now()
{
    const auto now = std::time(nullptr) ;
    return now >= today_at(7) && now <= today_at(9) ;
}
Thanks i will give this ago, basically i want to simply allow connection standard but between the 0700-0900 restrict a couple of things then allow again after set time.

Topic archived. No new replies allowed.