core-legacy/models/period.c
2020-05-05 11:42:02 +02:00

37 lines
673 B
C

#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <models/period.h>
int
period_helper_parse_hhmm(const char *hhmm_str, uint16_t *hhmm)
{
if(strlen(hhmm_str) != 5)
{
return 1;
}
if(hhmm_str[2] != ':')
{
return 1;
}
for(int i = 0; i < 5; ++i)
{
if(i != 2)
{
if(hhmm_str[i] < '0' || hhmm_str[i] > '9')
{
return 1;
}
}
}
uint16_t tmp_h, tmp_m;
tmp_h = (uint16_t)strtol(&hhmm_str[0], NULL, 10);
tmp_m = (uint16_t)strtol(&hhmm_str[3], NULL, 10);
*hhmm = (tmp_h * 60) + tmp_m;
return 0;
}