X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Ftime-util.c;h=12f1b193be550aa7b3962891c99ea7fe75943b7e;hb=d896ac2d2fbce41a0b11a0618a685adeaf18b8fe;hp=43ad9db91e5ce4114c4086487b84383267a8678c;hpb=65de0395ffe1cfb0f9af86504e8588fb31bb0fbc;p=elogind.git diff --git a/src/shared/time-util.c b/src/shared/time-util.c index 43ad9db91..12f1b193b 100644 --- a/src/shared/time-util.c +++ b/src/shared/time-util.c @@ -49,25 +49,20 @@ dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) { int64_t delta; assert(ts); - if (u == USEC_INFINITY) { - ts->realtime = ts->monotonic = USEC_INFINITY; + if (u == USEC_INFINITY || u <= 0) { + ts->realtime = ts->monotonic = u; return ts; } ts->realtime = u; - if (u == 0) - ts->monotonic = 0; - else { - delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u; - - ts->monotonic = now(CLOCK_MONOTONIC); + delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u; + ts->monotonic = now(CLOCK_MONOTONIC); - if ((int64_t) ts->monotonic > delta) - ts->monotonic -= delta; - else - ts->monotonic = 0; - } + if ((int64_t) ts->monotonic > delta) + ts->monotonic -= delta; + else + ts->monotonic = 0; return ts; } @@ -301,8 +296,14 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) { assert(buf); assert(l > 0); - if (t == USEC_INFINITY || t <= 0) { - strncpy(p, t == USEC_INFINITY ? "infinity" : "0", l); + if (t == USEC_INFINITY) { + strncpy(p, "infinity", l-1); + p[l-1] = 0; + return p; + } + + if (t <= 0) { + strncpy(p, "0", l-1); p[l-1] = 0; return p; } @@ -397,18 +398,21 @@ void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t) { t->monotonic); } -void dual_timestamp_deserialize(const char *value, dual_timestamp *t) { +int dual_timestamp_deserialize(const char *value, dual_timestamp *t) { unsigned long long a, b; assert(value); assert(t); - if (sscanf(value, "%llu %llu", &a, &b) != 2) - log_debug("Failed to parse finish timestamp value %s", value); - else { - t->realtime = a; - t->monotonic = b; + if (sscanf(value, "%llu %llu", &a, &b) != 2) { + log_debug("Failed to parse finish timestamp value %s.", value); + return -EINVAL; } + + t->realtime = a; + t->monotonic = b; + + return 0; } int parse_timestamp(const char *t, usec_t *usec) { @@ -785,7 +789,7 @@ int parse_nsec(const char *t, nsec_t *nsec) { s = startswith(p, "infinity"); if (s) { s += strspn(s, WHITESPACE); - if (!*s != 0) + if (*s != 0) return -EINVAL; *nsec = NSEC_INFINITY; @@ -964,7 +968,7 @@ bool timezone_is_valid(const char *name) { if (slash) return false; - t = strappenda("/usr/share/zoneinfo/", name); + t = strjoina("/usr/share/zoneinfo/", name); if (stat(t, &st) < 0) return false;