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