68 lines
1.3 KiB
C++
68 lines
1.3 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 = (uint16_t)strtol(begin, &check, 10);
|
|
if(begin == check)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
begin = check + 1;
|
|
tmp_m = (uint16_t)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 = (uint16_t)((h * 60) + m);
|
|
|
|
if(parse_HHMM(end_str, &h, &m))
|
|
{
|
|
continue;
|
|
}
|
|
end = (uint16_t)((h * 60) + m);
|
|
|
|
if(start < 0 || start > 24 * 60 || end < 0 || end > 24 * 60)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
result->add_period(start, end);
|
|
}
|
|
|
|
return result;
|
|
}
|