add: schedules
This commit is contained in:
		
							parent
							
								
									2d24339421
								
							
						
					
					
						commit
						cac5781222
					
				
					 21 changed files with 1030 additions and 39 deletions
				
			
		| 
						 | 
				
			
			@ -12,7 +12,7 @@ helpers::get_server_port(int fd)
 | 
			
		|||
    socklen_t addrlen = sizeof(sin);
 | 
			
		||||
    if(getsockname(fd, (struct sockaddr *)&sin, &addrlen) == 0)
 | 
			
		||||
    {
 | 
			
		||||
        return sin.sin_port;
 | 
			
		||||
        return ntohs(sin.sin_port);
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										81
									
								
								helpers/parse_periods.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								helpers/parse_periods.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,81 @@
 | 
			
		|||
#include <helpers.h>
 | 
			
		||||
#include <drogon/drogon.h>
 | 
			
		||||
#include <models/period_list.h>
 | 
			
		||||
 | 
			
		||||
static int
 | 
			
		||||
parse_HHMM(const char *begin, uint16_t *h, uint16_t *m)
 | 
			
		||||
{
 | 
			
		||||
    uint16_t tmp_h, tmp_m;
 | 
			
		||||
    char *check = nullptr;
 | 
			
		||||
 | 
			
		||||
    tmp_h = strtol(begin, &check, 10);
 | 
			
		||||
    if(begin == check)
 | 
			
		||||
    {
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    begin = check + 1;
 | 
			
		||||
    tmp_m = strtol(begin, &check, 10);
 | 
			
		||||
    if(begin == check)
 | 
			
		||||
    {
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    *h = tmp_h;
 | 
			
		||||
    *m = tmp_m;
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
period_list*
 | 
			
		||||
helpers::parse_periods(Json::Value periods_json)
 | 
			
		||||
{
 | 
			
		||||
    auto result = new period_list();
 | 
			
		||||
 | 
			
		||||
    for (Json::Value::ArrayIndex i = 0; i != periods_json.size(); i++)
 | 
			
		||||
    {
 | 
			
		||||
        Json::Value p = periods_json[i];
 | 
			
		||||
        if(!(p.isMember("start") && p.isMember("end")))
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        const char *start_str = p["start"].asCString();
 | 
			
		||||
        const char *end_str = p["end"].asCString();
 | 
			
		||||
 | 
			
		||||
        uint16_t h, m, start, end;
 | 
			
		||||
 | 
			
		||||
        if(parse_HHMM(start_str, &h, &m))
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        start = (h * 60) + m;
 | 
			
		||||
 | 
			
		||||
        if(parse_HHMM(end_str, &h, &m))
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        end = (h * 60) + m;
 | 
			
		||||
 | 
			
		||||
        if(start < 0 || start > 24 * 60 || end < 0 || end > 24 * 60)
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        bool period_overlap = false;
 | 
			
		||||
        for(int j = 0; j < result->length; j++)
 | 
			
		||||
        {
 | 
			
		||||
            if(result->periods[j]->is_in_period(start) || result->periods[j]->is_in_period(end))
 | 
			
		||||
            {
 | 
			
		||||
                period_overlap = true;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if(period_overlap)
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        result->add_period(start, end);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue