X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Fmisc.c;h=7322dd2a0f56c89cde3db3d24c14c51bb4ad1a4a;hb=ddb0f96f0004f26f621aba88ae41c70a4c6070fe;hp=2ba52a38ae58fd935de74e9c13662184a2a52c90;hpb=5c35fdb275b519373e6eb34621d956d09a52bf31;p=chaz%2Ftar diff --git a/src/misc.c b/src/misc.c index 2ba52a3..7322dd2 100644 --- a/src/misc.c +++ b/src/misc.c @@ -206,6 +206,61 @@ unquote_string (char *string) return result; } +/* Handling numbers. */ + +/* Output fraction and trailing digits appropriate for a nanoseconds + count equal to NS, but don't output unnecessary '.' or trailing + zeros. */ + +void +code_ns_fraction (int ns, char *p) +{ + if (ns == 0) + *p = '\0'; + else + { + int i = 9; + *p++ = '.'; + + while (ns % 10 == 0) + { + ns /= 10; + i--; + } + + p[i] = '\0'; + + for (;;) + { + p[--i] = '0' + ns % 10; + if (i == 0) + break; + ns /= 10; + } + } +} + +char const * +code_timespec (struct timespec t, char sbuf[TIMESPEC_STRSIZE_BOUND]) +{ + time_t s = t.tv_sec; + int ns = t.tv_nsec; + char *np; + bool negative = s < 0; + + if (negative && ns != 0) + { + s++; + ns = BILLION - ns; + } + + np = umaxtostr (negative ? - (uintmax_t) s : (uintmax_t) s, sbuf + 1); + if (negative) + *--np = '-'; + code_ns_fraction (ns, sbuf + UINTMAX_STRSIZE_BOUND); + return np; +} + /* File handling. */ /* Saved names in case backup needs to be undone. */