X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;ds=sidebyside;f=src%2Futil.c;h=a8ea4a97ab745dbc3ea4400176e10d9bae29f4e9;hb=871d7de47c13ee6cd78b8eefdf9128be3c740ac0;hp=85a8e37d4b559aca10af667fbd6f101c577e99b4;hpb=4288f619215e3dda0b75113d78e4fb7ba219ed58;p=elogind.git diff --git a/src/util.c b/src/util.c index 85a8e37d4..a8ea4a97a 100644 --- a/src/util.c +++ b/src/util.c @@ -72,6 +72,15 @@ usec_t now(clockid_t clock_id) { return timespec_load(&ts); } +timestamp* timestamp_get(timestamp *ts) { + assert(ts); + + ts->realtime = now(CLOCK_REALTIME); + ts->monotonic = now(CLOCK_MONOTONIC); + + return ts; +} + usec_t timespec_load(const struct timespec *ts) { assert(ts); @@ -1398,6 +1407,55 @@ char *format_timestamp(char *buf, size_t l, usec_t t) { return buf; } +char *format_timespan(char *buf, size_t l, usec_t t) { + static const struct { + const char *suffix; + usec_t usec; + } table[] = { + { "w", USEC_PER_WEEK }, + { "d", USEC_PER_DAY }, + { "h", USEC_PER_HOUR }, + { "min", USEC_PER_MINUTE }, + { "s", USEC_PER_SEC }, + { "ms", USEC_PER_MSEC }, + { "us", 1 }, + }; + + unsigned i; + char *p = buf; + + assert(buf); + assert(l > 0); + + if (t == (usec_t) -1) + return NULL; + + /* The result of this function can be parsed with parse_usec */ + + for (i = 0; i < ELEMENTSOF(table); i++) { + int k; + size_t n; + + if (t < table[i].usec) + continue; + + if (l <= 1) + break; + + k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix); + n = MIN((size_t) k, l); + + l -= n; + p += n; + + t %= table[i].usec; + } + + *p = 0; + + return buf; +} + bool fstype_is_network(const char *fstype) { static const char * const table[] = { "cifs",