X-Git-Url: https://git.octo.it/?p=routeros-api.git;a=blobdiff_plain;f=src%2Fros_parse.c;h=95126ef4950c97e03384746b616efcf251f5467e;hp=b9d7e9908b4c254872cc7279bd067ea6480f7177;hb=1ac209036249e18ebb9cb42d3afeebdd14d5eed1;hpb=3b8827c7a6f36c26b4aefe0afa4f6b6d44bd6c60 diff --git a/src/ros_parse.c b/src/ros_parse.c index b9d7e99..95126ef 100644 --- a/src/ros_parse.c +++ b/src/ros_parse.c @@ -145,4 +145,33 @@ int sstrto_rx_tx_counters (const char *str, /* {{{ */ return (0); } /* }}} int sstrto_rx_tx_counters */ +/* have_hour is initially set to false and later set to true when the first + * colon is found. It is used to determine whether the number before the colon + * is hours or minutes. External code should use the sstrtodate() macro. */ +uint64_t _sstrtodate (const char *str, _Bool have_hour) /* {{{ */ +{ + uint64_t ret; + char *endptr; + + if ((str == NULL) || (*str == 0)) + return (0); + + /* Example string: 6w6d18:33:07 */ + errno = 0; + endptr = NULL; + ret = (uint64_t) strtoull (str, &endptr, /* base = */ 10); + if ((endptr == str) || (errno != 0)) + return (0); + + switch (*endptr) + { + case 'y': ret *= 365 * 86400; break; + case 'w': ret *= 7 * 86400; break; + case 'd': ret *= 86400; break; + case ':': ret *= have_hour ? 60 : 3600; have_hour = true; break; + } + + return (ret + _sstrtodate (endptr + 1, have_hour)); +} /* }}} uint64_t _sstrtodate */ + /* vim: set ts=2 sw=2 noet fdm=marker : */