/*
 * helper.c
 *
 *  Created on: Jul 14, 2022
 *      Author: tom
 */
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>	/* sscanf */

#include <at1_defines.h> /* include my defines from a global view, else */
#ifndef DATE_COMPILE_CENTURY
#define DATE_COMPILE_CENTURY (20)
#define DATE_COMPILE_YEAR	 (22)
#endif

#if !defined _SYS_ERRNO_H_ && !defined __ERRNO_H__ && !defined __AT1_ERROR_NUMBERS__
#include <errno.h>
#endif
/* do not use the #include <time.h> library */
#include "helper.h"

char *ltrim(char *s)
{
    while(isspace((int)*s))	s++;
    return s;
}

char *rtrim(char *s)
{
    char* back = s + strlen(s);
    while(isspace((int)*--back));
    *(back+1) = '\0';
    return s;
}

char *trim(char *s)
{
    return rtrim(ltrim(s));
}

/* this is ISO 8601  2018-12-31 with separator == '-' */
int strtotime (char *s, struct _tm_ *tp) {

	int rv = 0;
	uint32_t tmp;
	struct _tm_ tm = {0};
	do {
		/* Check for the day and/or year */
		tmp = strtoul(s, &s, 10); /* base 10 */
		/* Check of plausibility: Year must be within compilation year and max 10 centuries from now on, i.e. 3000 */
		if (tmp >= (DATE_COMPILE_CENTURY * 100 + DATE_COMPILE_YEAR) && tmp < ((DATE_COMPILE_CENTURY + 10) * 100)) {
			tm.year = (uint8_t)(tmp % 100UL);
		} else {
			rv = -EINVAL;
			break;
		}
		rv = sscanf(s, "-%hu-%hu %hu:%hu:%hu", &tm.mon, &tm.day, &tm.hour, &tm.min, &tm.sec);
		if (rv >= 3)
			*tp = tm;
		else
			rv = -EINVAL;

	} while(0);

	return rv;
}

#if 0

		while (*str == ' ') {
			str++;
		};
		*args = str;
		return CMD_OK;
	} else {
		separator = '/';
		if (tmp > 0 && tmp < 32)
			timeinfo.tm_mday = tmp;
		else
			goto error;
	}

	str++;
	/* Read the month */
	tmp = strtoul(str, &str, 10);
	if (*str != separator || tmp == 0 || tmp > 12)
		goto error;
	timeinfo.tm_mon = tmp - 1; /* the month is interpreted from 0..11 */
	str++;
	/* Check either the day or the four-digit year */
	tmp = strtoul(str, &str, 10);
	/* there should either spaces or a T between date and time info */
	if ((toupper(*str) != 'T' && *str != ' ') || tmp == 0 || tmp >= 2106)
		goto error;
	if (separator == '-') {
		timeinfo.tm_mday = tmp;
	} else if (tmp >= FW_VERSION_YEAR) {
		timeinfo.tm_year = tmp - 1900;
	} else {
		goto error;
	}
	do {
		str++;
	} while (*str == ' ');

	/* the hour have to be 0..23 */
	tmp = strtoul(str, &str, 10);
	if (*str != ':' || tmp > 23)
		goto error;
	timeinfo.tm_hour = tmp;
	str++;
	/* the minute have to be 0..59 */
	tmp = strtoul(str, &str, 10);
	if (tmp > 59)
		goto error;
	timeinfo.tm_min = tmp;

	if (*str == ':') {
		str++;
		tmp = strtoul(str, &str, 10);
	} else {
		tmp = 0;
	}
	if (tmp > 59)
		goto error;
	timeinfo.tm_sec = tmp;

	while (*str == ' ')
		str++;
	if (*str != '\0' && (flags & ARG_LAST))
		goto error;
	/* Try to create the time stamp */
	{
		time_t tmp_unixtime = mktime(&timeinfo);
		if (tmp_unixtime == (unsigned)-1) {
			goto error;
		}
		*args = str;
		*unixtime = tmp_unixtime;
	}
	return CMD_OK;

error:
	*args = str;
	return CMD_ERROR_PARAMETERS;
}
#endif
