diff options
author | Daniel Stone <daniels@collabora.com> | 2017-03-01 11:34:00 +0000 |
---|---|---|
committer | Pekka Paalanen <pekka.paalanen@collabora.co.uk> | 2017-03-08 11:57:34 +0200 |
commit | 5ae7e84c6bc37436e636b75e53788f478ef78454 (patch) | |
tree | a64d001d3e4f5b6f9f848054f2e3c6c7d946a530 /shared | |
parent | 11ae2a3036bc95ff26b12a6d60f12514d8145143 (diff) |
timespec: Add timespec_add_nsec helper
Add a (timespec) = (timespec) + (nsec) helper, to save intermediate
conversions to nanoseconds in its users.
Signed-off-by: Daniel Stone <daniels@collabora.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Diffstat (limited to 'shared')
-rw-r--r-- | shared/timespec-util.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/shared/timespec-util.h b/shared/timespec-util.h index edd4ec14..a1d6881b 100644 --- a/shared/timespec-util.h +++ b/shared/timespec-util.h @@ -49,6 +49,27 @@ timespec_sub(struct timespec *r, } } +/* Add a nanosecond value to a timespec + * + * \param r[out] result: a + b + * \param a[in] base operand as timespec + * \param b[in] operand in nanoseconds + */ +static inline void +timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b) +{ + r->tv_sec = a->tv_sec + (b / NSEC_PER_SEC); + r->tv_nsec = a->tv_nsec + (b % NSEC_PER_SEC); + + if (r->tv_nsec >= NSEC_PER_SEC) { + r->tv_sec++; + r->tv_nsec -= NSEC_PER_SEC; + } else if (r->tv_nsec < 0) { + r->tv_sec--; + r->tv_nsec += NSEC_PER_SEC; + } +} + /* Convert timespec to nanoseconds * * \param a timespec |