core-legacy/helpers/parse_periods.cc

82 lines
1.7 KiB
C++
Raw Normal View History

2019-07-19 09:41:39 +00:00
#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;
2019-07-20 12:51:45 +00:00
tmp_h = (uint16_t)strtol(begin, &check, 10);
2019-07-19 09:41:39 +00:00
if(begin == check)
{
return 1;
}
begin = check + 1;
2019-07-20 12:51:45 +00:00
tmp_m = (uint16_t)strtol(begin, &check, 10);
2019-07-19 09:41:39 +00:00
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;
}
2019-07-20 12:51:45 +00:00
start = (uint16_t)((h * 60) + m);
2019-07-19 09:41:39 +00:00
if(parse_HHMM(end_str, &h, &m))
{
continue;
}
2019-07-20 12:51:45 +00:00
end = (uint16_t)((h * 60) + m);
2019-07-19 09:41:39 +00:00
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;
}