micro-utils/libmu/duration.c
2024-07-09 22:33:45 +03:00

45 lines
562 B
C

#include <stdlib.h>
#include <string.h>
unsigned long long parse_uint(const char *str) {
char *p = NULL;
unsigned long long res = strtoull(str, &p, 0);
/* Parse suffix */
if (*p) {
switch (p[0]) {
case 'm':
res *= 60;
break;
case 'h':
res *= 3600;
break;
case 'd':
res *= 86400;
break;
case 's':
default:
break;
}
}
return res;
}
unsigned long long mu_parse_duration(const char *arg) {
if (strchr(arg, '.')) {
/* TODO */
}
else {
/* Sec */
return parse_uint(arg) * 1000000;
}
return 0;
}