chiark / gitweb /
time-util: add new call usec_shift_clock() for converting times between clocks
authorLennart Poettering <lennart@poettering.net>
Thu, 22 Jun 2017 18:52:23 +0000 (20:52 +0200)
committerSven Eden <yamakuzure@gmx.net>
Tue, 25 Jul 2017 07:46:52 +0000 (09:46 +0200)
We use that quite often, let's implement one clean version of it.

src/basic/time-util.c
src/basic/time-util.h

index 37d5f78..7da7ea4 100644 (file)
@@ -1372,4 +1372,23 @@ unsigned long usec_to_jiffies(usec_t u) {
 
         return DIV_ROUND_UP(u , USEC_PER_SEC / hz);
 }
+
+usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) {
+        usec_t a, b;
+
+        if (x == USEC_INFINITY)
+                return USEC_INFINITY;
+        if (map_clock_id(from) == map_clock_id(to))
+                return x;
+
+        a = now(from);
+        b = now(to);
+
+        if (x > a)
+                /* x lies in the future */
+                return usec_add(b, usec_sub_unsigned(x, a));
+        else
+                /* x lies in the past */
+                return usec_sub_unsigned(b, usec_sub_unsigned(a, x));
+}
 #endif // 0
index a6df932..58b8c53 100644 (file)
@@ -161,6 +161,8 @@ bool clock_boottime_supported(void);
 bool clock_supported(clockid_t clock);
 #if 0 /// UNNEEDED by elogind
 clockid_t clock_boottime_or_monotonic(void);
+
+usec_t usec_shift_clock(usec_t, clockid_t from, clockid_t to);
 #endif // 0
 
 #define xstrftime(buf, fmt, tm) \