From e8e6b286fd0e65709fe372773e58b1b7ebcfffcd Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 22 Jun 2017 20:52:23 +0200 Subject: [PATCH] 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. --- src/basic/time-util.c | 19 +++++++++++++++++++ src/basic/time-util.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 37d5f7886..7da7ea41c 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -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 diff --git a/src/basic/time-util.h b/src/basic/time-util.h index a6df932ad..58b8c5300 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -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) \ -- 2.30.2