]> granicus.if.org Git - fcron/commitdiff
added my_time() to use the more accurate (at least on recent Linux systems) gettimeof...
authorThibault Godouet <fcron@free.fr>
Fri, 12 Jun 2015 21:54:50 +0000 (22:54 +0100)
committerThibault Godouet <fcron@free.fr>
Fri, 12 Jun 2015 21:58:01 +0000 (22:58 +0100)
subs.c

diff --git a/subs.c b/subs.c
index bdae71fb03b0dbc4408136de58acd6d4f8203fc1..69316f4722938a4c060358e6cef4f02a93e0ca43 100644 (file)
--- a/subs.c
+++ b/subs.c
@@ -24,6 +24,7 @@
 
 #include "global.h"
 #include "subs.h"
+#include <sys/time.h>
 
 uid_t
 get_user_uid_safe(char *username)
@@ -475,3 +476,20 @@ imin(int x, int y)
         return y;
     }
 }
+
+time_t
+my_time(void)
+/* return the current time as number of seconds since the Epoch. */
+{
+    /* Use gettimeofday() if available as this is more accurate
+     * than time() on recent Linux systems.
+     * (I suspect time() sacrifies accuracy for speed in the same way as
+     * clock_gettime()'s CLOCK_REALTIME_COARSE, and only updates on systems ticks) */
+#ifdef HAVE_GETTIMEOFDAY
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    return tv.tv_sec;
+#else                           /* HAVE_GETTIMEOFDAY */
+    return time(NULL);
+#endif                          /* HAVE_GETTIMEOFDAY */
+}