summary refs log tree commit diff
path: root/Src/compat.c
diff options
context:
space:
mode:
authordana <dana@dana.is>2018-06-20 17:29:56 -0500
committerdana <dana@dana.is>2018-06-20 17:29:56 -0500
commit394f3a47e464b67b17e2cb7166df066829250e88 (patch)
tree0fe4262c7857d546878f64b53fc2813e6f59296b /Src/compat.c
parenteada7e1138a3fca90f085dd764ad86098e58c9ac (diff)
downloadzsh-394f3a47e464b67b17e2cb7166df066829250e88.tar.gz
zsh-394f3a47e464b67b17e2cb7166df066829250e88.tar.xz
zsh-394f3a47e464b67b17e2cb7166df066829250e88.zip
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
Diffstat (limited to 'Src/compat.c')
-rw-r--r--Src/compat.c33
1 files changed, 33 insertions, 0 deletions
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 */
 
 /**/