From 61f6d7d372f600de36412a41269909050978bcd6 Mon Sep 17 00:00:00 2001 From: Daniel Stone Date: Wed, 1 Mar 2017 11:34:01 +0000 Subject: [PATCH] timespec: Add timespec_add_msec helper Add a (timespec) = (timespec) + (msec) helper, to save intermediate conversions in its users. Signed-off-by: Daniel Stone Reviewed-by: Pekka Paalanen --- shared/timespec-util.h | 12 ++++++++++++ tests/timespec-test.c | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/shared/timespec-util.h b/shared/timespec-util.h index a1d6881b..13948b1a 100644 --- a/shared/timespec-util.h +++ b/shared/timespec-util.h @@ -70,6 +70,18 @@ timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b) } } +/* Add a millisecond value to a timespec + * + * \param r[out] result: a + b + * \param a[in] base operand as timespec + * \param b[in] operand in milliseconds + */ +static inline void +timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b) +{ + return timespec_add_nsec(r, a, b * 1000000); +} + /* Convert timespec to nanoseconds * * \param a timespec diff --git a/tests/timespec-test.c b/tests/timespec-test.c index 91d53f73..cd3b1c1a 100644 --- a/tests/timespec-test.c +++ b/tests/timespec-test.c @@ -122,3 +122,14 @@ ZUC_TEST(timespec_test, timespec_add_nsec) ZUC_ASSERT_EQ(16, r.tv_sec); ZUC_ASSERT_EQ(0, r.tv_nsec); } + +ZUC_TEST(timespec_test, timespec_add_msec) +{ + struct timespec a, r; + + a.tv_sec = 1000; + a.tv_nsec = 1; + timespec_add_msec(&r, &a, 2002); + ZUC_ASSERT_EQ(1002, r.tv_sec); + ZUC_ASSERT_EQ(2000001, r.tv_nsec); +}