From 6c7facfd2dd4871147e40936dd0fae2ede2c267d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 10 May 2016 06:28:17 +0930 Subject: [PATCH] daemon: time option support. No need to have all times in seconds. Signed-off-by: Rusty Russell --- daemon/Makefile | 2 ++ daemon/opt_time.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++ daemon/opt_time.h | 10 ++++++ 3 files changed, 91 insertions(+) create mode 100644 daemon/opt_time.c create mode 100644 daemon/opt_time.h diff --git a/daemon/Makefile b/daemon/Makefile index dbc62ca90..e275768cb 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -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 \ diff --git a/daemon/opt_time.c b/daemon/opt_time.c new file mode 100644 index 000000000..db958d504 --- /dev/null +++ b/daemon/opt_time.c @@ -0,0 +1,79 @@ +#include "opt_time.h" +#include +#include +#include +#include +#include + +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); +} diff --git a/daemon/opt_time.h b/daemon/opt_time.h new file mode 100644 index 000000000..9b5f8091e --- /dev/null +++ b/daemon/opt_time.h @@ -0,0 +1,10 @@ +#ifndef LIGHTNING_DAEMON_OPT_TIME_H +#define LIGHTNING_DAEMON_OPT_TIME_H +#include "config.h" +#include +#include + +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 */