Merge branch 'refs/heads/t/kill-w'
[dss.git] / tv.c
1 /*
2 * Copyright (C) 2005-2010 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 #include <sys/time.h>
8 #include <time.h>
9 #include <inttypes.h>
10 #include <assert.h>
11 #include <string.h>
12
13 #include "gcc-compat.h"
14 #include "err.h"
15 #include "str.h"
16 #include "log.h"
17 #include "time.h"
18
19 /**
20 * Compute the difference of two time values.
21 *
22 * \param b Minuend.
23 * \param a Subtrahend.
24 * \param diff Result pointer.
25 *
26 * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
27 * return.
28 *
29 * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
30 */
31 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff)
32 {
33 int ret = 1;
34
35 if ((b->tv_sec < a->tv_sec) ||
36 ((b->tv_sec == a->tv_sec) && (b->tv_usec < a->tv_usec))) {
37 const struct timeval *tmp = a;
38 a = b;
39 b = tmp;
40 ret = -1;
41 }
42 if (!diff)
43 return ret;
44 diff->tv_sec = b->tv_sec - a->tv_sec;
45 if (b->tv_usec < a->tv_usec) {
46 diff->tv_sec--;
47 diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec;
48 } else
49 diff->tv_usec = b->tv_usec - a->tv_usec;
50 return ret;
51 }
52
53 int64_t get_current_time(void)
54 {
55 time_t now;
56 time(&now);
57 return (int64_t)now;
58 }