daemon: time option support.

No need to have all times in seconds.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-05-10 06:28:17 +09:30
parent 82c2325467
commit 6c7facfd2d
3 changed files with 91 additions and 0 deletions

View File

@ -22,6 +22,7 @@ DAEMON_SRC := \
daemon/jsonrpc.c \
daemon/lightningd.c \
daemon/netaddr.c \
daemon/opt_time.c \
daemon/peer.c \
daemon/packets.c \
daemon/secrets.c \
@ -51,6 +52,7 @@ DAEMON_HEADERS := \
daemon/lightningd.h \
daemon/log.h \
daemon/netaddr.h \
daemon/opt_time.h \
daemon/peer.h \
daemon/pseudorand.h \
daemon/secrets.h \

79
daemon/opt_time.c Normal file
View File

@ -0,0 +1,79 @@
#include "opt_time.h"
#include <ccan/str/str.h>
#include <ccan/tal/str/str.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
static bool match(const char *str, const char *abbrev, const char *full)
{
if (streq(str, abbrev))
return true;
if (streq(str, full))
return true;
/* Allow "seconds" */
if (memcmp(str, full, strlen(full)) == 0
&& streq(str + strlen(full), "s"))
return true;
return false;
}
char *opt_set_time(const char *arg, struct timerel *t)
{
char *endp;
unsigned long int l;
/* This is how the manpage says to do it. Yech. */
errno = 0;
l = strtol(arg, &endp, 0);
if (endp == arg)
return tal_fmt(NULL, "'%s' is not a number", arg);
if (errno)
return tal_fmt(NULL, "'%s' is out of range", arg);
while (isspace(*endp))
endp++;
if (match(endp, "s", "second"))
*t = time_from_sec(l);
else if (match(endp, "m", "minute"))
*t = time_from_sec(l * 60);
else if (match(endp, "h", "hour"))
*t = time_from_sec(l * 60 * 60);
else if (match(endp, "d", "day"))
*t = time_from_sec(l * 60 * 60 * 24);
else if (match(endp, "ms", "millisecond"))
*t = time_from_msec(l);
else if (match(endp, "us", "microsecond"))
*t = time_from_usec(l);
else if (match(endp, "ns", "nanosecond"))
*t = time_from_nsec(l);
else
return tal_fmt(NULL, "Unknown time unit %s", endp);
return NULL;
}
void opt_show_time(char buf[OPT_SHOW_LEN], const struct timerel *t)
{
if (t->ts.tv_nsec) {
if (t->ts.tv_nsec % 1000)
sprintf(buf, "%"PRIu64"ns", time_to_nsec(*t));
else if (t->ts.tv_nsec % 1000000)
sprintf(buf, "%"PRIu64"us", time_to_usec(*t));
else
sprintf(buf, "%"PRIu64"ms", time_to_msec(*t));
} else if (t->ts.tv_sec) {
if (t->ts.tv_sec % (60 * 60 * 24) == 0)
sprintf(buf, "%lud", t->ts.tv_sec / (60 * 60 * 24));
else if (t->ts.tv_sec % (60 * 60) == 0)
sprintf(buf, "%luh", t->ts.tv_sec / (60 * 60));
else if (t->ts.tv_sec % 60 == 0)
sprintf(buf, "%lum", t->ts.tv_sec / 60);
else
sprintf(buf, "%lus", t->ts.tv_sec);
} else
sprintf(buf, "%lus", t->ts.tv_sec);
}

10
daemon/opt_time.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef LIGHTNING_DAEMON_OPT_TIME_H
#define LIGHTNING_DAEMON_OPT_TIME_H
#include "config.h"
#include <ccan/opt/opt.h>
#include <ccan/time/time.h>
char *opt_set_time(const char *arg, struct timerel *t);
void opt_show_time(char buf[OPT_SHOW_LEN], const struct timerel *t);
#endif /* LIGHTNING_DAEMON_OPT_TIME_H */