1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
4 /** \file time.c Helper functions for dealing with time values. */
7 * Convert struct timeval to milliseconds.
9 * \param tv The time value value to convert.
11 * \return The number of milliseconds in \a tv.
13 long unsigned tv2ms(const struct timeval *tv)
15 return tv->tv_sec * 1000 + (tv->tv_usec + 500)/ 1000;
19 * Convert milliseconds to a struct timeval.
21 * \param n The number of milliseconds.
22 * \param tv Result pointer.
24 void ms2tv(long unsigned n, struct timeval *tv)
26 tv->tv_sec = n / 1000;
27 tv->tv_usec = (n % 1000) * 1000;
31 * Compute the difference of two time values.
34 * \param a Subtrahend.
35 * \param diff Result pointer.
37 * If \a diff is not \p NULL, it contains the absolute value |\a b - \a a| on
40 * \return If \a b < \a a, this function returns -1, otherwise it returns 1.
42 int tv_diff(const struct timeval *b, const struct timeval *a,
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;
56 diff->tv_sec = b->tv_sec - a->tv_sec;
57 if (b->tv_usec < a->tv_usec) {
59 diff->tv_usec = 1000 * 1000 - a->tv_usec + b->tv_usec;
61 diff->tv_usec = b->tv_usec - a->tv_usec;
66 * Add two time values.
68 * \param a First addend.
69 * \param b Second addend.
70 * \param sum Contains the sum \a a + \a b on return.
72 void tv_add(const struct timeval *a, const struct timeval *b,
75 sum->tv_sec = a->tv_sec + b->tv_sec;
76 if (a->tv_usec + b->tv_usec >= 1000 * 1000) {
78 sum->tv_usec = a->tv_usec + b->tv_usec - 1000 * 1000;
80 sum->tv_usec = a->tv_usec + b->tv_usec;
84 * Compute integer multiple of given struct timeval.
86 * \param mult The integer value to multiply with.
87 * \param tv The timevalue to multiply.
89 * \param result Contains \a mult * \a tv on return.
91 void tv_scale(const unsigned long mult, const struct timeval *tv,
92 struct timeval *result)
94 uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) * mult;
96 result->tv_sec = x / 1000 / 1000;
97 result->tv_usec = x % (1000 * 1000);
101 * Compute a fraction of given struct timeval.
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.
107 void tv_divide(const unsigned long divisor, const struct timeval *tv,
108 struct timeval *result)
110 uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) / divisor;
112 result->tv_sec = x / 1000 / 1000;
113 result->tv_usec = x % (1000 * 1000);
117 * Compute a convex combination of two time values.
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.
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.
128 * \return Zero, 1 or -1, if \a x is zero, positive or negative, respectively.
130 int tv_convex_combination(const long a, const struct timeval *tv1,
131 const long b, const struct timeval *tv2,
132 struct timeval *result)
134 struct timeval tmp1, tmp2, tmp3;
136 unsigned long a1, b1;
138 if (a == 0 && b == 0) {
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);
150 tv_add(&tmp1, &tmp2, &tmp3);
151 tv_divide(a1 + b1, &tmp3, result);
161 * Compute when to send a chunk of an audio file.
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.
168 * This function computes \a stream_start + \a chunk_num * \a chunk_time.
170 void compute_chunk_time(long unsigned chunk_num,
171 struct timeval *chunk_tv, struct timeval *stream_start,
172 struct timeval *result)
176 tv_scale(chunk_num, chunk_tv, &tmp);
177 tv_add(&tmp, stream_start, result);
181 * Retrieve the time of the realtime clock.
183 * \param tv Where to store the result.
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.
189 * \return This function aborts on errors. On success it returns a pointer to
190 * memory containing the current time.
192 * \sa clock_gettime(2), gettimeofday(2).
194 struct timeval *clock_get_realtime(struct timeval *tv)
196 static struct timeval user_friendly;
202 ret = clock_gettime(CLOCK_REALTIME, &t);
204 tv->tv_sec = t.tv_sec;
205 tv->tv_usec = t.tv_nsec / 1000;