>From b641e268ab0aa1597e46f79accd169c388f06c7c Mon Sep 17 00:00:00 2001 From: Alex Samilovskih <alexsamilovskih@xxxxxxxxx> Date: Fri, 2 May 2014 18:15:12 +0000 Subject: [PATCH 1/3] Add support for monotonic clock --- include/timeutils.h | 1 + lib/timeutils.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/timeutils.h b/include/timeutils.h index bcae613..7cf972d 100644 --- a/include/timeutils.h +++ b/include/timeutils.h @@ -51,5 +51,6 @@ typedef uint64_t nsec_t; #define FORMAT_TIMESPAN_MAX 64 int parse_timestamp(const char *t, usec_t *usec); +int gettime_monotonic(struct timeval *tv); #endif /* UTIL_LINUX_TIME_UTIL_H */ diff --git a/lib/timeutils.c b/lib/timeutils.c index 7fe6218..b3fb87b 100644 --- a/lib/timeutils.c +++ b/lib/timeutils.c @@ -336,3 +336,27 @@ int parse_timestamp(const char *t, usec_t *usec) return 0; } + +/* Get the current time that is not a subject to resetting and drifting */ +int gettime_monotonic(struct timeval *tv) +{ +#ifdef CLOCK_MONOTONIC + int ret; + struct timespec ts; + +#ifdef CLOCK_MONOTONIC_RAW + if (!(ret = clock_gettime(CLOCK_MONOTONIC_RAW, &ts))) { +#else /* CLOCK_MONOTONIC_RAW is not available */ + if (!(ret = clock_gettime(CLOCK_MONOTONIC, &ts))) { +#endif + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / 1000; + } + + return ret; + +#else /* CLOCK_MONOTONIC is not available */ + + return gettimeofday(tv, NULL); +#endif +} -- 1.9.2 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html