chiark / gitweb /
time-util: add new call usec_shift_clock() for converting times between clocks
[elogind.git] / src / basic / time-util.c
index 37d5f78863c9fabf39f3517a0b2cafaa6bd2f51c..7da7ea41c1a632e5896eec40eebbb6dc845e1b8b 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