X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Ftime-util.c;h=860be61e8b7b8af74a9e13c9b50da782776bf182;hb=442e00839e4fc3c11506f5c8a9477b465865aecc;hp=b6a2bec15693161bc7e1b6ccd38cb6515611a3d4;hpb=2fa4092c2829dd14e50c430ae2f23551d23c6c1d;p=elogind.git diff --git a/src/shared/time-util.c b/src/shared/time-util.c index b6a2bec15..860be61e8 100644 --- a/src/shared/time-util.c +++ b/src/shared/time-util.c @@ -168,6 +168,28 @@ char *format_timestamp(char *buf, size_t l, usec_t t) { return buf; } +char *format_timestamp_us(char *buf, size_t l, usec_t t) { + struct tm tm; + time_t sec; + + assert(buf); + assert(l > 0); + + if (t <= 0) + return NULL; + + sec = (time_t) (t / USEC_PER_SEC); + localtime_r(&sec, &tm); + + if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm) <= 0) + return NULL; + snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", t % USEC_PER_SEC); + if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0) + return NULL; + + return buf; +} + char *format_timestamp_relative(char *buf, size_t l, usec_t t) { usec_t n, d; @@ -251,6 +273,12 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) { if (t == (usec_t) -1) return NULL; + if (t <= 0) { + snprintf(p, l, "0"); + p[l-1] = 0; + return p; + } + /* The result of this function can be parsed with parse_sec */ for (i = 0; i < ELEMENTSOF(table); i++) { @@ -259,15 +287,11 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) { bool done = false; usec_t a, b; - if (t == 0 || t < accuracy) { - if (!something) { - snprintf(p, l, "0"); - p[l-1] = 0; - return p; - } + if (t <= 0) + break; + if (t < accuracy && something) break; - } if (t < table[i].usec) continue; @@ -322,7 +346,6 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) { l -= n; p += n; - something = true; }