Extend nl_time2int() and rename it to nl_str2msec()
Support parsing of more complex time duration input.
This commit is contained in:
parent
d49018fd78
commit
c48a17694b
2 changed files with 30 additions and 14 deletions
|
@ -51,6 +51,7 @@ extern long nl_prob2int(const char *);
|
|||
extern int nl_get_hz(void);
|
||||
extern uint32_t nl_us2ticks(uint32_t);
|
||||
extern uint32_t nl_ticks2us(uint32_t);
|
||||
extern int nl_str2msec(const char *, uint64_t *);
|
||||
extern char * nl_msec2str(uint64_t, char *, size_t);
|
||||
|
||||
/* link layer protocol translations */
|
||||
|
|
43
lib/utils.c
43
lib/utils.c
|
@ -356,25 +356,40 @@ uint32_t nl_ticks2us(uint32_t ticks)
|
|||
return ticks / ticks_per_usec;
|
||||
}
|
||||
|
||||
long nl_time2int(const char *str)
|
||||
int nl_str2msec(const char *str, uint64_t *result)
|
||||
{
|
||||
uint64_t total = 0, l;
|
||||
int plen;
|
||||
char *p;
|
||||
long l = strtol(str, &p, 0);
|
||||
if (p == str)
|
||||
return -NLE_INVAL;
|
||||
|
||||
if (*p) {
|
||||
if (!strcasecmp(p, "min") == 0 || !strcasecmp(p, "m"))
|
||||
l *= 60;
|
||||
else if (!strcasecmp(p, "hour") || !strcasecmp(p, "h"))
|
||||
l *= 60*60;
|
||||
else if (!strcasecmp(p, "day") || !strcasecmp(p, "d"))
|
||||
l *= 60*60*24;
|
||||
else if (strcasecmp(p, "s") != 0)
|
||||
do {
|
||||
l = strtoul(str, &p, 0);
|
||||
if (p == str)
|
||||
return -NLE_INVAL;
|
||||
}
|
||||
else if (*p) {
|
||||
plen = strcspn(p, " \t");
|
||||
|
||||
return l;
|
||||
if (!plen)
|
||||
total += l;
|
||||
else if (!strncasecmp(p, "sec", plen))
|
||||
total += (l * 1000);
|
||||
else if (!strncasecmp(p, "min", plen))
|
||||
total += (l * 1000*60);
|
||||
else if (!strncasecmp(p, "hour", plen))
|
||||
total += (l * 1000*60*60);
|
||||
else if (!strncasecmp(p, "day", plen))
|
||||
total += (l * 1000*60*60*24);
|
||||
else
|
||||
return -NLE_INVAL;
|
||||
|
||||
str = p + plen;
|
||||
} else
|
||||
total += l;
|
||||
} while (*str && *p);
|
||||
|
||||
*result = total;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue