shared: Add helpers to convert between various time units and timespec
authorAlexandros Frantzis <alexandros.frantzis@collabora.com>
Thu, 16 Nov 2017 16:20:51 +0000 (18:20 +0200)
committerPekka Paalanen <pekka.paalanen@collabora.co.uk>
Mon, 27 Nov 2017 09:18:25 +0000 (11:18 +0200)
Add helper functions to make it easy and less error-prone to convert
between values in various time units (nsec, usec, msec) and struct
timespec. These helpers are going to be used in the upcoming commits to
transition the Weston codebase to struct timespec.

Signed-off-by: Alexandros Frantzis <alexandros.frantzis@collabora.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
shared/timespec-util.h
tests/timespec-test.c

index 7260dc8..f9736c2 100644 (file)
@@ -134,6 +134,53 @@ timespec_sub_to_msec(const struct timespec *a, const struct timespec *b)
        return timespec_sub_to_nsec(a, b) / 1000000;
 }
 
+/* Convert timespec to microseconds
+ *
+ * \param a timespec
+ * \return microseconds
+ *
+ * Rounding to integer microseconds happens always down (floor()).
+ */
+static inline int64_t
+timespec_to_usec(const struct timespec *a)
+{
+       return (int64_t)a->tv_sec * 1000000 + a->tv_nsec / 1000;
+}
+
+/* Convert nanoseconds to timespec
+ *
+ * \param a timespec
+ * \param b nanoseconds
+ */
+static inline void
+timespec_from_nsec(struct timespec *a, int64_t b)
+{
+       a->tv_sec = b / NSEC_PER_SEC;
+       a->tv_nsec = b % NSEC_PER_SEC;
+}
+
+/* Convert microseconds to timespec
+ *
+ * \param a timespec
+ * \param b microseconds
+ */
+static inline void
+timespec_from_usec(struct timespec *a, int64_t b)
+{
+       timespec_from_nsec(a, b * 1000);
+}
+
+/* Convert milliseconds to timespec
+ *
+ * \param a timespec
+ * \param b milliseconds
+ */
+static inline void
+timespec_from_msec(struct timespec *a, int64_t b)
+{
+       timespec_from_nsec(a, b * 1000000);
+}
+
 /* Check if a timespec is zero
  *
  * \param a timespec
index 4e83605..f127bce 100644 (file)
@@ -60,6 +60,15 @@ ZUC_TEST(timespec_test, timespec_to_nsec)
        ZUC_ASSERT_EQ(timespec_to_nsec(&a), (NSEC_PER_SEC * 4ULL) + 4);
 }
 
+ZUC_TEST(timespec_test, timespec_to_usec)
+{
+       struct timespec a;
+
+       a.tv_sec = 4;
+       a.tv_nsec = 4000;
+       ZUC_ASSERT_EQ(timespec_to_usec(&a), (4000000ULL) + 4);
+}
+
 ZUC_TEST(timespec_test, timespec_to_msec)
 {
        struct timespec a;
@@ -165,6 +174,69 @@ ZUC_TEST(timespec_test, timespec_sub_to_msec)
        ZUC_ASSERT_EQ((998 * 1000) + 1, timespec_sub_to_msec(&a, &b));
 }
 
+ZUC_TEST(timespec_test, timespec_from_nsec)
+{
+       struct timespec a;
+
+       timespec_from_nsec(&a, 0);
+       ZUC_ASSERT_EQ(0, a.tv_sec);
+       ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+       timespec_from_nsec(&a, NSEC_PER_SEC - 1);
+       ZUC_ASSERT_EQ(0, a.tv_sec);
+       ZUC_ASSERT_EQ(NSEC_PER_SEC - 1, a.tv_nsec);
+
+       timespec_from_nsec(&a, NSEC_PER_SEC);
+       ZUC_ASSERT_EQ(1, a.tv_sec);
+       ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+       timespec_from_nsec(&a, (5L * NSEC_PER_SEC) + 1);
+       ZUC_ASSERT_EQ(5, a.tv_sec);
+       ZUC_ASSERT_EQ(1, a.tv_nsec);
+}
+
+ZUC_TEST(timespec_test, timespec_from_usec)
+{
+       struct timespec a;
+
+       timespec_from_usec(&a, 0);
+       ZUC_ASSERT_EQ(0, a.tv_sec);
+       ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+       timespec_from_usec(&a, 999999);
+       ZUC_ASSERT_EQ(0, a.tv_sec);
+       ZUC_ASSERT_EQ(999999 * 1000, a.tv_nsec);
+
+       timespec_from_usec(&a, 1000000);
+       ZUC_ASSERT_EQ(1, a.tv_sec);
+       ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+       timespec_from_usec(&a, 5000001);
+       ZUC_ASSERT_EQ(5, a.tv_sec);
+       ZUC_ASSERT_EQ(1000, a.tv_nsec);
+}
+
+ZUC_TEST(timespec_test, timespec_from_msec)
+{
+       struct timespec a;
+
+       timespec_from_msec(&a, 0);
+       ZUC_ASSERT_EQ(0, a.tv_sec);
+       ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+       timespec_from_msec(&a, 999);
+       ZUC_ASSERT_EQ(0, a.tv_sec);
+       ZUC_ASSERT_EQ(999 * 1000000, a.tv_nsec);
+
+       timespec_from_msec(&a, 1000);
+       ZUC_ASSERT_EQ(1, a.tv_sec);
+       ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+       timespec_from_msec(&a, 5001);
+       ZUC_ASSERT_EQ(5, a.tv_sec);
+       ZUC_ASSERT_EQ(1000000, a.tv_nsec);
+}
+
 ZUC_TEST(timespec_test, timespec_is_zero)
 {
        struct timespec zero = { 0 };