Replace gettimeofday() by clock_gettime().
[paraslash.git] / time.c
diff --git a/time.c b/time.c
index 18b5a35ffb678d80635887a14b88ba6fdaa12af9..6f6dd49e330df2f4d85565aa65ab1e02615abebd 100644 (file)
--- a/time.c
+++ b/time.c
@@ -191,3 +191,40 @@ void compute_chunk_time(long unsigned chunk_num,
        tv_scale(chunk_num, chunk_tv, &tmp);
        tv_add(&tmp, stream_start, result);
 }
+
+/**
+ * Retrieve the time of the realtime clock.
+ *
+ * \param tv Where to store the result.
+ *
+ * Gets the current value of the system-wide real-time clock (identified by id
+ * \p CLOCK_REALTIME). If \a tv is \p NULL, the value is stored in a static
+ * buffer, otherwise it is stored at the location given by \a tv.
+ *
+ * \return This function aborts on errors. On success it returns a pointer to
+ * memory containing the current time.
+ *
+ * \sa clock_gettime(2), gettimeofday(2).
+ */
+struct timeval *clock_get_realtime(struct timeval *tv)
+{
+       static struct timeval user_friendly;
+
+       if (!tv)
+               tv = &user_friendly;
+#ifdef HAVE_CLOCK_GETTIME
+       {
+               struct timespec t;
+               int ret;
+
+               ret = clock_gettime(CLOCK_REALTIME, &t);
+               assert(ret == 0);
+               tv->tv_sec = t.tv_sec;
+               tv->tv_usec = t.tv_nsec / 1000;
+       }
+#else
+       #include <sys/time.h>
+       gettimeofday(tv, NULL);
+#endif /* HAVE_CLOCK_GETTIME */
+       return tv;
+}