Merge branch 'maint'
[paraslash.git] / time.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 #include "para.h"
4 /** \file time.c Helper functions for dealing with time values. */
5
6 /**
7 * Convert struct timeval to milliseconds.
8 *
9 * \param tv The time value value to convert.
10 *
11 * \return The number of milliseconds in \a tv.
12 */
13 long unsigned tv2ms(const struct timeval *tv)
14 {
15 return tv->tv_sec * 1000 + (tv->tv_usec + 500)/ 1000;
16 }
17
18 /**
19 * Convert milliseconds to a struct timeval.
20 *
21 * \param n The number of milliseconds.
22 * \param tv Result pointer.
23 */
24 void ms2tv(long unsigned n, struct timeval *tv)
25 {
26 tv->tv_sec = n / 1000;
27 tv->tv_usec = (n % 1000) * 1000;
28 }
29
30 /**
31 * Compute the difference of two time values.
32 *
33 * \param b Minuend.
34 * \param a Subtrahend.
35 * \param diff Result pointer.
36 *
37 * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
38 * return.
39 *
40 * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
41 */
42 int tv_diff(const struct timeval *b, const struct timeval *a,
43 struct timeval *diff)
44 {
45 int ret = 1;
46
47 if ((b->tv_sec < a->tv_sec) ||
48 ((b->tv_sec == a->tv_sec) && (b->tv_usec < a->tv_usec))) {
49 const struct timeval *tmp = a;
50 a = b;
51 b = tmp;
52 ret = -1;
53 }
54 if (!diff)
55 return ret;
56 diff->tv_sec = b->tv_sec - a->tv_sec;
57 if (b->tv_usec < a->tv_usec) {
58 diff->tv_sec--;
59 diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec;
60 } else
61 diff->tv_usec = b->tv_usec - a->tv_usec;
62 return ret;
63 }
64
65 /**
66 * Add two time values.
67 *
68 * \param a First addend.
69 * \param b Second addend.
70 * \param sum Contains the sum \a a + \a b on return.
71 */
72 void tv_add(const struct timeval *a, const struct timeval *b,
73 struct timeval *sum)
74 {
75 sum->tv_sec = a->tv_sec + b->tv_sec;
76 if (a->tv_usec + b->tv_usec >= 1000 * 1000) {
77 sum->tv_sec++;
78 sum->tv_usec = a->tv_usec + b->tv_usec - 1000 * 1000;
79 } else
80 sum->tv_usec = a->tv_usec + b->tv_usec;
81 }
82
83 /**
84 * Compute integer multiple of given struct timeval.
85 *
86 * \param mult The integer value to multiply with.
87 * \param tv The timevalue to multiply.
88 *
89 * \param result Contains \a mult * \a tv on return.
90 */
91 void tv_scale(const unsigned long mult, const struct timeval *tv,
92 struct timeval *result)
93 {
94 uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) * mult;
95
96 result->tv_sec = x / 1000 / 1000;
97 result->tv_usec = x % (1000 * 1000);
98 }
99
100 /**
101 * Compute a fraction of given struct timeval.
102 *
103 * \param divisor The integer value to divide by.
104 * \param tv The timevalue to divide.
105 * \param result Contains (1 / mult) * tv on return.
106 */
107 void tv_divide(const unsigned long divisor, const struct timeval *tv,
108 struct timeval *result)
109 {
110 uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
111
112 result->tv_sec = x / 1000 / 1000;
113 result->tv_usec = x % (1000 * 1000);
114 }
115
116 /**
117 * Compute a convex combination of two time values.
118 *
119 * \param a The first coefficient.
120 * \param tv1 The first time value.
121 * \param b The second coefficient.
122 * \param tv2 The second time value.
123 * \param result Contains the convex combination upon return.
124 *
125 * Compute x := (a * tv1 + b * tv2) / (|a| + |b|) and store |x| in \a result.
126 * Both \a a and \a b may be negative.
127 *
128 * \return Zero, 1 or -1, if \a x is zero, positive or negative, respectively.
129 */
130 int tv_convex_combination(const long a, const struct timeval *tv1,
131 const long b, const struct timeval *tv2,
132 struct timeval *result)
133 {
134 struct timeval tmp1, tmp2, tmp3;
135 int ret = 1;
136 unsigned long a1, b1;
137
138 if (a == 0 && b == 0) {
139 result->tv_sec = 0;
140 result->tv_usec = 0;
141 return 0;
142 }
143 a1 = PARA_ABS(a);
144 b1 = PARA_ABS(b);
145 tv_scale(a1, tv1, &tmp1);
146 tv_scale(b1, tv2, &tmp2);
147 if ((a > 0 && b < 0) || (a < 0 && b > 0)) /* subtract */
148 ret = tv_diff(&tmp1, &tmp2, &tmp3);
149 else
150 tv_add(&tmp1, &tmp2, &tmp3);
151 tv_divide(a1 + b1, &tmp3, result);
152 if (!a || !b) {
153 if (a + b < 0)
154 ret = -1;
155 } else if (a < 0)
156 ret = -ret;
157 return ret;
158 }
159
160 /**
161 * Compute when to send a chunk of an audio file.
162 *
163 * \param chunk_num The number of the chunk.
164 * \param chunk_tv The duration of one chunk.
165 * \param stream_start When the first chunk was sent.
166 * \param result The time when to send chunk number \a chunk_num.
167 *
168 * This function computes \a stream_start + \a chunk_num * \a chunk_time.
169 */
170 void compute_chunk_time(long unsigned chunk_num,
171 struct timeval *chunk_tv, struct timeval *stream_start,
172 struct timeval *result)
173 {
174 struct timeval tmp;
175
176 tv_scale(chunk_num, chunk_tv, &tmp);
177 tv_add(&tmp, stream_start, result);
178 }
179
180 /**
181 * Retrieve the time of the realtime clock.
182 *
183 * \param tv Where to store the result.
184 *
185 * Gets the current value of the system-wide real-time clock (identified by id
186 * \p CLOCK_REALTIME). If \a tv is \p NULL, the value is stored in a static
187 * buffer, otherwise it is stored at the location given by \a tv.
188 *
189 * \return This function aborts on errors. On success it returns a pointer to
190 * memory containing the current time.
191 *
192 * \sa clock_gettime(2), gettimeofday(2).
193 */
194 struct timeval *clock_get_realtime(struct timeval *tv)
195 {
196 static struct timeval user_friendly;
197 struct timespec t;
198 int ret;
199
200 if (!tv)
201 tv = &user_friendly;
202 ret = clock_gettime(CLOCK_REALTIME, &t);
203 assert(ret == 0);
204 tv->tv_sec = t.tv_sec;
205 tv->tv_usec = t.tv_nsec / 1000;
206 return tv;
207 }