]>
code.delx.au - pulseaudio/blob - src/pulse/timeval.c
78ece0616468c36e82944cdd39c9f6217e3b1c62
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 #include "../pulsecore/winsock.h"
41 struct timeval
*pa_gettimeofday(struct timeval
*tv
) {
42 #ifdef HAVE_GETTIMEOFDAY
45 return gettimeofday(tv
, NULL
) < 0 ? NULL
: tv
;
46 #elif defined(OS_IS_WIN32)
48 * Copied from implementation by Steven Edwards (LGPL).
49 * Found on wine mailing list.
52 #if defined(_MSC_VER) || defined(__BORLANDC__)
53 #define EPOCHFILETIME (116444736000000000i64)
55 #define EPOCHFILETIME (116444736000000000LL)
64 GetSystemTimeAsFileTime(&ft
);
65 li
.LowPart
= ft
.dwLowDateTime
;
66 li
.HighPart
= ft
.dwHighDateTime
;
67 t
= li
.QuadPart
; /* In 100-nanosecond intervals */
68 t
-= EPOCHFILETIME
; /* Offset to the Epoch time */
69 t
/= 10; /* In microseconds */
70 tv
->tv_sec
= (long)(t
/ 1000000);
71 tv
->tv_usec
= (long)(t
% 1000000);
75 #error "Platform lacks gettimeofday() or equivalent function."
79 pa_usec_t
pa_timeval_diff(const struct timeval
*a
, const struct timeval
*b
) {
83 /* Check which whan is the earlier time and swap the two arguments if reuqired. */
84 if (pa_timeval_cmp(a
, b
) < 0) {
85 const struct timeval
*c
;
91 /* Calculate the second difference*/
92 r
= ((pa_usec_t
) a
->tv_sec
- b
->tv_sec
)* 1000000;
94 /* Calculate the microsecond difference */
95 if (a
->tv_usec
> b
->tv_usec
)
96 r
+= ((pa_usec_t
) a
->tv_usec
- b
->tv_usec
);
97 else if (a
->tv_usec
< b
->tv_usec
)
98 r
-= ((pa_usec_t
) b
->tv_usec
- a
->tv_usec
);
103 int pa_timeval_cmp(const struct timeval
*a
, const struct timeval
*b
) {
106 if (a
->tv_sec
< b
->tv_sec
)
109 if (a
->tv_sec
> b
->tv_sec
)
112 if (a
->tv_usec
< b
->tv_usec
)
115 if (a
->tv_usec
> b
->tv_usec
)
121 pa_usec_t
pa_timeval_age(const struct timeval
*tv
) {
125 return pa_timeval_diff(pa_gettimeofday(&now
), tv
);
128 struct timeval
* pa_timeval_add(struct timeval
*tv
, pa_usec_t v
) {
133 tv
->tv_sec
+= (unsigned long) secs
;
139 while (tv
->tv_usec
>= 1000000) {
141 tv
->tv_usec
-= 1000000;