time-util: add new call usec_shift_clock() for converting times between clocks

We use that quite often, let's implement one clean version of it.
This commit is contained in:
Lennart Poettering
2017-06-22 20:52:23 +02:00
parent 54d8ef14d8
commit 1007ec60e6
3 changed files with 60 additions and 0 deletions
+19
View File
@@ -1351,3 +1351,22 @@ 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));
}
+2
View File
@@ -145,6 +145,8 @@ bool clock_boottime_supported(void);
bool clock_supported(clockid_t clock);
clockid_t clock_boottime_or_monotonic(void);
usec_t usec_shift_clock(usec_t, clockid_t from, clockid_t to);
#define xstrftime(buf, fmt, tm) \
assert_message_se(strftime(buf, ELEMENTSOF(buf), fmt, tm) > 0, \
"xstrftime: " #buf "[] must be big enough")