[LTP] [PATCH v4 5/8] tst_timer: Add tst_timespec_sub_us()

Rafael David Tinoco rafael.tinoco@linaro.org
Tue Jan 29 18:36:56 CET 2019


This commit adds a tst_timespec_sub_us() function that subtracts
microseconds from a given timespec struct.

Signed-off-by: Rafael David Tinoco <rafael.tinoco@linaro.org>
---
 include/tst_timer.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/include/tst_timer.h b/include/tst_timer.h
index b57adf7aa..043b71460 100644
--- a/include/tst_timer.h
+++ b/include/tst_timer.h
@@ -170,6 +170,23 @@ static inline struct timespec tst_timespec_add(struct timespec t1,
 	return res;
 }
 
+/*
+ * Subtracts us microseconds from t.
+ */
+static inline struct timespec tst_timespec_sub_us(struct timespec t,
+                                                  long long us)
+{
+	t.tv_sec -= us / 1000000;
+	t.tv_nsec -= (us % 1000000) * 1000;
+
+	if (t.tv_nsec < 0) {
+		t.tv_sec--;
+		t.tv_nsec += 1000000000;
+	}
+
+	return t;
+}
+
 /*
  * Returns difference between two timespec structures.
  */
-- 
2.20.1



More information about the ltp mailing list