]> code.delx.au - pulseaudio/blob - src/pulse/timeval.c
Huge trailing whitespace cleanup. Let's keep the tree pure from here on,
[pulseaudio] / src / pulse / timeval.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <stddef.h>
28 #include <sys/time.h>
29
30 #ifdef HAVE_WINDOWS_H
31 #include <windows.h>
32 #endif
33
34 #include "../pulsecore/winsock.h"
35
36 #include "timeval.h"
37
38 struct timeval *pa_gettimeofday(struct timeval *tv) {
39 #ifdef HAVE_GETTIMEOFDAY
40 assert(tv);
41
42 return gettimeofday(tv, NULL) < 0 ? NULL : tv;
43 #elif defined(OS_IS_WIN32)
44 /*
45 * Copied from implementation by Steven Edwards (LGPL).
46 * Found on wine mailing list.
47 */
48
49 #if defined(_MSC_VER) || defined(__BORLANDC__)
50 #define EPOCHFILETIME (116444736000000000i64)
51 #else
52 #define EPOCHFILETIME (116444736000000000LL)
53 #endif
54
55 FILETIME ft;
56 LARGE_INTEGER li;
57 __int64 t;
58
59 assert(tv);
60
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);
69
70 return tv;
71 #else
72 #error "Platform lacks gettimeofday() or equivalent function."
73 #endif
74 }
75
76 pa_usec_t pa_timeval_diff(const struct timeval *a, const struct timeval *b) {
77 pa_usec_t r;
78 assert(a && b);
79
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;
83 c = a;
84 a = b;
85 b = c;
86 }
87
88 /* Calculate the second difference*/
89 r = ((pa_usec_t) a->tv_sec - b->tv_sec)* 1000000;
90
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);
96
97 return r;
98 }
99
100 int pa_timeval_cmp(const struct timeval *a, const struct timeval *b) {
101 assert(a && b);
102
103 if (a->tv_sec < b->tv_sec)
104 return -1;
105
106 if (a->tv_sec > b->tv_sec)
107 return 1;
108
109 if (a->tv_usec < b->tv_usec)
110 return -1;
111
112 if (a->tv_usec > b->tv_usec)
113 return 1;
114
115 return 0;
116 }
117
118 pa_usec_t pa_timeval_age(const struct timeval *tv) {
119 struct timeval now;
120 assert(tv);
121
122 return pa_timeval_diff(pa_gettimeofday(&now), tv);
123 }
124
125 struct timeval* pa_timeval_add(struct timeval *tv, pa_usec_t v) {
126 unsigned long secs;
127 assert(tv);
128
129 secs = (v/1000000);
130 tv->tv_sec += (unsigned long) secs;
131 v -= secs*1000000;
132
133 tv->tv_usec += v;
134
135 /* Normalize */
136 while (tv->tv_usec >= 1000000) {
137 tv->tv_sec++;
138 tv->tv_usec -= 1000000;
139 }
140
141 return tv;
142 }