#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 = (uint16_t)strtol(&hhmm_str[0], NULL, 10);
    uint16_t tmp_m = (uint16_t)strtol(&hhmm_str[3], NULL, 10);

    if(tmp_h > 24 || tmp_m >= 60)
    {
        return 1;
    }
    if(tmp_h == 24 && tmp_m > 0)
    {
        return 1;
    }

    *hhmm = (tmp_h * 60) + tmp_m;

    return 0;
}