lib/util: add time_mono() for monotonic time a la time()
[nivanova/samba-autobuild/.git] / lib / util / time.c
index 5ecf93cb77caba269514ebe5b30d28571f140062..ed3b4f8e301c7c09021ce677cd3fe5395ba48baa 100644 (file)
@@ -65,6 +65,27 @@ _PUBLIC_ void clock_gettime_mono(struct timespec *tp)
        }
 }
 
+/**
+a wrapper to preferably get the monotonic time in seconds
+as this is only second resolution we can use the cached
+(and much faster) COARS clock variant
+**/
+_PUBLIC_ time_t time_mono(time_t *t)
+{
+       struct timespec tp;
+       int rc = -1;
+#ifdef CLOCK_MONOTONIC_COARSE
+       rc = clock_gettime(CLOCK_MONOTONIC_COARSE,&tp);
+#endif
+       if (rc != 0) {
+               clock_gettime_mono(&tp);
+       }
+       if (t != NULL) {
+               *t = tp.tv_sec;
+       }
+       return tp.tv_sec;
+}
+
 
 #define TIME_FIXUP_CONSTANT 11644473600LL