Merge branch 't/rename_system_files'
[dss.git] / tv.c
1 /*
2 * Copyright (C) 2005-2010 Andre Noll <maan@systemlinux.org>
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 * Convert struct timeval to milliseconds.
21 *
22 * \param tv The time value value to convert.
23 *
24 * \return The number off milliseconds in \a tv.
25 */
26 long unsigned tv2ms(const struct timeval *tv)
27 {
28 return tv->tv_sec * 1000 + (tv->tv_usec + 500)/ 1000;
29 }
30
31 /**
32 * Convert milliseconds to a struct timeval.
33 *
34 * \param n The number of milliseconds.
35 * \param tv Result pointer.
36 */
37 void ms2tv(long unsigned n, struct timeval *tv)
38 {
39 tv->tv_sec = n / 1000;
40 tv->tv_usec = (n % 1000) * 1000;
41 }
42
43 /**
44 * Convert a double to a struct timeval.
45 *
46 * \param x The value to convert.
47 * \param tv Result pointer.
48 */
49 void d2tv(double x, struct timeval *tv)
50 {
51 tv->tv_sec = x;
52 tv->tv_usec = (x - (double)tv->tv_sec) * 1000.0 * 1000.0 + 0.5;
53 }
54
55 /**
56 * Compute the difference of two time values.
57 *
58 * \param b Minuend.
59 * \param a Subtrahend.
60 * \param diff Result pointer.
61 *
62 * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
63 * return.
64 *
65 * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
66 */
67 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff)
68 {
69 int ret = 1;
70
71 if ((b->tv_sec < a->tv_sec) ||
72 ((b->tv_sec == a->tv_sec) && (b->tv_usec < a->tv_usec))) {
73 const struct timeval *tmp = a;
74 a = b;
75 b = tmp;
76 ret = -1;
77 }
78 if (!diff)
79 return ret;
80 diff->tv_sec = b->tv_sec - a->tv_sec;
81 if (b->tv_usec < a->tv_usec) {
82 diff->tv_sec--;
83 diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec;
84 } else
85 diff->tv_usec = b->tv_usec - a->tv_usec;
86 return ret;
87 }
88
89 /**
90 * Add two time values.
91 *
92 * \param a First addend.
93 * \param b Second addend.
94 * \param sum Contains the sum \a + \a b on return.
95 */
96 void tv_add(const struct timeval *a, const struct timeval *b,
97 struct timeval *sum)
98 {
99 sum->tv_sec = a->tv_sec + b->tv_sec;
100 if (a->tv_usec + b->tv_usec >= 1000 * 1000) {
101 sum->tv_sec++;
102 sum->tv_usec = a->tv_usec + b->tv_usec - 1000 * 1000;
103 } else
104 sum->tv_usec = a->tv_usec + b->tv_usec;
105 }
106
107 /**
108 * Compute integer multiple of given struct timeval.
109 *
110 * \param mult The integer value to multiply with.
111 * \param tv The timevalue to multiply.
112 *
113 * \param result Contains \a mult * \a tv on return.
114 */
115 void tv_scale(const unsigned long mult, const struct timeval *tv,
116 struct timeval *result)
117 {
118 result->tv_sec = mult * tv->tv_sec;
119 result->tv_sec += tv->tv_usec * mult / 1000 / 1000;
120 result->tv_usec = tv->tv_usec * mult % (1000 * 1000);
121 }
122
123 /**
124 * Compute a fraction of given struct timeval.
125 *
126 * \param divisor The integer value to divide by.
127 * \param tv The timevalue to divide.
128 * \param result Contains (1 / mult) * tv on return.
129 */
130 void tv_divide(const unsigned long divisor, const struct timeval *tv,
131 struct timeval *result)
132 {
133 uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
134
135 result->tv_sec = x / 1000 / 1000;
136 result->tv_usec = x % (1000 * 1000);
137 }
138
139 int64_t get_current_time(void)
140 {
141 time_t now;
142 time(&now);
143 DSS_DEBUG_LOG(("now: %jd\n", (intmax_t)now));
144 return (int64_t)now;
145 }