* [PATCH 1/3] lib/timeutils: add monotonic clock support
@ 2014-05-05 18:29 Alexander
0 siblings, 0 replies; only message in thread
From: Alexander @ 2014-05-05 18:29 UTC (permalink / raw)
To: alexander samilovskih, util-linux, kerolasa
>From b641e268ab0aa1597e46f79accd169c388f06c7c Mon Sep 17 00:00:00 2001
From: Alex Samilovskih <alexsamilovskih@gmail.com>
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
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2014-05-05 13:29 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-05 18:29 [PATCH 1/3] lib/timeutils: add monotonic clock support Alexander
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox