4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include "../pulsecore/winsock.h"
38 struct timeval *pa_gettimeofday(struct timeval *tv) {
39 #ifdef HAVE_GETTIMEOFDAY
42 return gettimeofday(tv, NULL) < 0 ? NULL : tv;
43 #elif defined(OS_IS_WIN32)
45 * Copied from implementation by Steven Edwards (LGPL).
46 * Found on wine mailing list.
49 #if defined(_MSC_VER) || defined(__BORLANDC__)
50 #define EPOCHFILETIME (116444736000000000i64)
52 #define EPOCHFILETIME (116444736000000000LL)
61 GetSystemTimeAsFileTime(&ft);
62 li.LowPart = ft.dwLowDateTime;
63 li.HighPart = ft.dwHighDateTime;
64 t = li.QuadPart; /* In 100-nanosecond intervals */
65 t -= EPOCHFILETIME; /* Offset to the Epoch time */
66 t /= 10; /* In microseconds */
67 tv->tv_sec = (long)(t / 1000000);
68 tv->tv_usec = (long)(t % 1000000);
72 #error "Platform lacks gettimeofday() or equivalent function."
76 pa_usec_t pa_timeval_diff(const struct timeval *a, const struct timeval *b) {
80 /* Check which whan is the earlier time and swap the two arguments if reuqired. */
81 if (pa_timeval_cmp(a, b) < 0) {
82 const struct timeval *c;
88 /* Calculate the second difference*/
89 r = ((pa_usec_t) a->tv_sec - b->tv_sec)* 1000000;
91 /* Calculate the microsecond difference */
92 if (a->tv_usec > b->tv_usec)
93 r += ((pa_usec_t) a->tv_usec - b->tv_usec);
94 else if (a->tv_usec < b->tv_usec)
95 r -= ((pa_usec_t) b->tv_usec - a->tv_usec);
100 int pa_timeval_cmp(const struct timeval *a, const struct timeval *b) {
103 if (a->tv_sec < b->tv_sec)
106 if (a->tv_sec > b->tv_sec)
109 if (a->tv_usec < b->tv_usec)
112 if (a->tv_usec > b->tv_usec)
118 pa_usec_t pa_timeval_age(const struct timeval *tv) {
122 return pa_timeval_diff(pa_gettimeofday(&now), tv);
125 struct timeval* pa_timeval_add(struct timeval *tv, pa_usec_t v) {
130 tv->tv_sec += (unsigned long) secs;
136 while (tv->tv_usec >= 1000000) {
138 tv->tv_usec -= 1000000;