Replace license boilerplate with single line SPDX comments.
[dss.git] / tv.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <sys/time.h>
3 #include <time.h>
4 #include <inttypes.h>
5 #include <assert.h>
6 #include <string.h>
7
8 #include "gcc-compat.h"
9 #include "err.h"
10 #include "str.h"
11 #include "log.h"
12 #include "time.h"
13
14 /**
15 * Compute the difference of two time values.
16 *
17 * \param b Minuend.
18 * \param a Subtrahend.
19 * \param diff Result pointer.
20 *
21 * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
22 * return.
23 *
24 * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
25 */
26 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff)
27 {
28 int ret = 1;
29
30 if ((b->tv_sec < a->tv_sec) ||
31 ((b->tv_sec == a->tv_sec) && (b->tv_usec < a->tv_usec))) {
32 const struct timeval *tmp = a;
33 a = b;
34 b = tmp;
35 ret = -1;
36 }
37 if (!diff)
38 return ret;
39 diff->tv_sec = b->tv_sec - a->tv_sec;
40 if (b->tv_usec < a->tv_usec) {
41 diff->tv_sec--;
42 diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec;
43 } else
44 diff->tv_usec = b->tv_usec - a->tv_usec;
45 return ret;
46 }
47
48 int64_t get_current_time(void)
49 {
50 time_t now;
51 time(&now);
52 return (int64_t)now;
53 }