From b2dfd57669ba5835feb024b4ce1b3208a797d568 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Wed, 20 Nov 2013 12:00:17 +0100 Subject: lib-util: add functions to get elapsed from given timespec structs Signed-off-by: Volker Lendecke Reviewed-by: Stefan Metzmacher Reviewed-by: Jeremy Allison --- lib/util/time.c | 18 ++++++++++++++++++ lib/util/time.h | 10 ++++++++++ 2 files changed, 28 insertions(+) (limited to 'lib') diff --git a/lib/util/time.c b/lib/util/time.c index 05251dd3b5..a09490a198 100644 --- a/lib/util/time.c +++ b/lib/util/time.c @@ -652,6 +652,24 @@ _PUBLIC_ double timeval_elapsed(const struct timeval *tv) struct timeval tv2 = timeval_current(); return timeval_elapsed2(tv, &tv2); } +/** + * return the number of seconds elapsed between two times + **/ +_PUBLIC_ double timespec_elapsed2(const struct timespec *ts1, + const struct timespec *ts2) +{ + return (ts2->tv_sec - ts1->tv_sec) + + (ts2->tv_nsec - ts1->tv_nsec)*1.0e-9; +} + +/** + * return the number of seconds elapsed since a given time + */ +_PUBLIC_ double timespec_elapsed(const struct timespec *ts) +{ + struct timespec ts2 = timespec_current(); + return timespec_elapsed2(ts, &ts2); +} /** return the lesser of two timevals diff --git a/lib/util/time.h b/lib/util/time.h index 69ba783774..b5302f84c2 100644 --- a/lib/util/time.h +++ b/lib/util/time.h @@ -246,6 +246,16 @@ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2); */ double timeval_elapsed(const struct timeval *tv); +/** + return the number of seconds elapsed between two times +*/ +double timespec_elapsed2(const struct timespec *ts1, + const struct timespec *ts2); +/** + return the number of seconds elapsed since a given time +*/ +double timespec_elapsed(const struct timespec *ts); + /** return the lesser of two timevals */ -- cgit