From b428952eefd250d6d4ce77ad89325e9803940d11 Mon Sep 17 00:00:00 2001 From: Björn Jacke Date: Mon, 6 Sep 2010 20:27:24 +0200 Subject: lib/util: add time_mono() for monotonic time a la time() --- lib/util/time.c | 21 +++++++++++++++++++++ lib/util/time.h | 5 +++++ 2 files changed, 26 insertions(+) (limited to 'lib/util') diff --git a/lib/util/time.c b/lib/util/time.c index 5ecf93cb77..ed3b4f8e30 100644 --- a/lib/util/time.c +++ b/lib/util/time.c @@ -65,6 +65,27 @@ _PUBLIC_ void clock_gettime_mono(struct timespec *tp) } } +/** +a wrapper to preferably get the monotonic time in seconds +as this is only second resolution we can use the cached +(and much faster) COARS clock variant +**/ +_PUBLIC_ time_t time_mono(time_t *t) +{ + struct timespec tp; + int rc = -1; +#ifdef CLOCK_MONOTONIC_COARSE + rc = clock_gettime(CLOCK_MONOTONIC_COARSE,&tp); +#endif + if (rc != 0) { + clock_gettime_mono(&tp); + } + if (t != NULL) { + *t = tp.tv_sec; + } + return tp.tv_sec; +} + #define TIME_FIXUP_CONSTANT 11644473600LL diff --git a/lib/util/time.h b/lib/util/time.h index 720a262b29..345382b80a 100644 --- a/lib/util/time.h +++ b/lib/util/time.h @@ -55,6 +55,11 @@ a wrapper to preferably get the monotonic time **/ _PUBLIC_ void clock_gettime_mono(struct timespec *tp); +/** +a wrapper to preferably get the monotonic time in s +**/ +_PUBLIC_ time_t time_mono(time_t *t); + /** interpret an 8 byte "filetime" structure to a time_t It's originally in "100ns units since jan 1st 1601" -- cgit