From 1ac209036249e18ebb9cb42d3afeebdd14d5eed1 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 23 Dec 2009 17:37:59 +0100 Subject: [PATCH] src/ros_parse.[ch]: Added functions to parse the uptime string into seconds. --- src/ros_parse.c | 29 +++++++++++++++++++++++++++++ src/ros_parse.h | 3 +++ 2 files changed, 32 insertions(+) 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 : */ diff --git a/src/ros_parse.h b/src/ros_parse.h index a39ac89..75473a4 100644 --- a/src/ros_parse.h +++ b/src/ros_parse.h @@ -31,6 +31,9 @@ double sstrtod (const char *str); int sstrto_rx_tx_counters (const char *str, uint64_t *rx, uint64_t *tx); +uint64_t _sstrtodate (const char *str, _Bool have_hour); +#define sstrtodate(str) _sstrtodate((str), 0) + #endif /* ROS_PARSE_H */ /* vim: set ts=2 sw=2 noet fdm=marker : */ -- 2.11.0