From: Alexander Kuleshov Date: Thu, 4 Feb 2016 18:02:39 +0000 (+0600) Subject: time-util: introduce usec_sub() X-Git-Tag: v229.1~1^2~26 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=c549c705f8856bec29760614bd507e53c2e2a97f time-util: introduce usec_sub() The dual_timestamp_from_realtime(), dual_timestamp_from_monotonic() and dual_timestamp_from_boottime_or_monotonic() shares the same code for comparison given ts with delta. Let's move it to the separate inline function to prevent code duplication. --- diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 28b20f418..047d2a66d 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -73,12 +73,7 @@ dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) { ts->realtime = u; delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u; - ts->monotonic = now(CLOCK_MONOTONIC); - - if ((int64_t) ts->monotonic > delta) - ts->monotonic -= delta; - else - ts->monotonic = 0; + ts->monotonic = usec_sub(now(CLOCK_MONOTONIC), delta); return ts; } @@ -95,12 +90,7 @@ dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) { ts->monotonic = u; delta = (int64_t) now(CLOCK_MONOTONIC) - (int64_t) u; - - ts->realtime = now(CLOCK_REALTIME); - if ((int64_t) ts->realtime > delta) - ts->realtime -= delta; - else - ts->realtime = 0; + ts->realtime = usec_sub(now(CLOCK_REALTIME), delta); return ts; } @@ -115,16 +105,8 @@ dual_timestamp* dual_timestamp_from_boottime_or_monotonic(dual_timestamp *ts, us dual_timestamp_get(ts); delta = (int64_t) now(clock_boottime_or_monotonic()) - (int64_t) u; - - if ((int64_t) ts->realtime > delta) - ts->realtime -= delta; - else - ts->realtime = 0; - - if ((int64_t) ts->monotonic > delta) - ts->monotonic -= delta; - else - ts->monotonic = 0; + ts->realtime = usec_sub(ts->realtime, delta); + ts->monotonic = usec_sub(ts->monotonic, delta); return ts; }