replace obsolete call to ftime(3)
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 3 Feb 2020 14:35:38 +0000 (15:35 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 4 Feb 2020 14:15:45 +0000 (15:15 +0100)
ftime() and timeb.h have been deprecated since POSIX 2008 and
will be removed in future versions of glibc [0]. Replace them
with (probably faster) calls to clock_gettime(2).

[0] https://sourceware.org/ml/libc-announce/2020/msg00001.html

src/timefunc.cpp

index a4d38b8..6f4ffb5 100644 (file)
@@ -38,7 +38,6 @@ on this file might be covered by the GNU General Public License.
 #include <time.h>
 #include <unistd.h>
 #include <string.h>
-#include <sys/timeb.h>
 
 #include <timefunc.hxx>
 #include <i18n.h>
@@ -59,12 +58,17 @@ using namespace std;
 
 double prec_time(void)
 {
-    struct timeb tb;
-    double ret;
+    struct timespec ts = { 0 };
+    double ret = 0.0;
 
-    ftime(&tb);
+    if (clock_gettime(CLOCK_REALTIME_COARSE, &ts) == -1) {
+        /* Something’s wrong on the kernel end! */
+        return ret;
+    }
 
-    ret=tb.time+(static_cast<float>(tb.millitm)/1000);
+    ret = static_cast<double>(ts.tv_sec);
+    ret += static_cast<double>(ts.tv_nsec)
+         / static_cast<double>(TIME_CONST_FACTOR_NANO);
 
     return ret;
 }