From 394f3a47e464b67b17e2cb7166df066829250e88 Mon Sep 17 00:00:00 2001 From: dana Date: Wed, 20 Jun 2018 17:29:56 -0500 Subject: 43075: Support nanosecond-precision time formatting * Teach ztrftime() %9. and %N for nanoseconds * Update prompt expansion to pass sub-second times for time formatting * Update zsh/stat to pass sub-second times for atime/mtime/ctime Patch heavily based on Oliver's earlier work @ workers/24059 --- Src/compat.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'Src/compat.c') diff --git a/Src/compat.c b/Src/compat.c index a130d9264..7b5c4411c 100644 --- a/Src/compat.c +++ b/Src/compat.c @@ -94,6 +94,39 @@ gettimeofday(struct timeval *tv, struct timezone *tz) #endif +/* Provide clock time with nanoseconds */ + +/**/ +mod_export int +zgettime(struct timespec *ts) +{ + int ret = -1; + +#ifdef HAVE_CLOCK_GETTIME + struct timespec dts; + if (clock_gettime(CLOCK_REALTIME, &dts) < 0) { + zwarn("unable to retrieve time: %e", errno); + ret--; + } else { + ret++; + ts->tv_sec = (time_t) dts.tv_sec; + ts->tv_nsec = (long) dts.tv_nsec; + } +#endif + + if (ret) { + struct timeval dtv; + struct timezone dtz; + gettimeofday(&dtv, &dtz); + ret++; + ts->tv_sec = (time_t) dtv.tv_sec; + ts->tv_nsec = (long) dtv.tv_usec * 1000; + } + + return ret; +} + + /* compute the difference between two calendar times */ /**/ -- cgit v1.2.3