core-legacy/helpers/parse_periods.cc
Tobias Reisinger cac5781222 add: schedules
2019-07-19 11:41:39 +02:00

81 lines
1.7 KiB
C++

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