Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / src / pulse / timeval.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
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.
13
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.
18
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
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <assert.h>
30 #include <stddef.h>
31 #include <sys/time.h>
32
33 #ifdef HAVE_WINDOWS_H
34 #include <windows.h>
35 #endif
36
37 #include "../pulsecore/winsock.h"
38
39 #include "timeval.h"
40
41 struct timeval *pa_gettimeofday(struct timeval *tv) {
42 #ifdef HAVE_GETTIMEOFDAY
43     assert(tv);
44
45     return gettimeofday(tv, NULL) < 0 ? NULL : tv;
46 #elif defined(OS_IS_WIN32)
47     /*
48      * Copied from implementation by Steven Edwards (LGPL).
49      * Found on wine mailing list.
50      */
51
52 #if defined(_MSC_VER) || defined(__BORLANDC__)
53 #define EPOCHFILETIME (116444736000000000i64)
54 #else
55 #define EPOCHFILETIME (116444736000000000LL)
56 #endif
57
58     FILETIME        ft;
59     LARGE_INTEGER   li;
60     __int64         t;
61
62     assert(tv);
63
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);
72
73     return tv;
74 #else
75 #error "Platform lacks gettimeofday() or equivalent function."
76 #endif
77 }
78
79 pa_usec_t pa_timeval_diff(const struct timeval *a, const struct timeval *b) {
80     pa_usec_t r;
81     assert(a && b);
82
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;
86         c = a;
87         a = b;
88         b = c;
89     }
90
91     /* Calculate the second difference*/
92     r = ((pa_usec_t) a->tv_sec - b->tv_sec)* 1000000;
93
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);
99
100     return r;
101 }
102
103 int pa_timeval_cmp(const struct timeval *a, const struct timeval *b) {
104     assert(a && b);
105
106     if (a->tv_sec < b->tv_sec)
107         return -1;
108
109     if (a->tv_sec > b->tv_sec)
110         return 1;
111
112     if (a->tv_usec < b->tv_usec)
113         return -1;
114
115     if (a->tv_usec > b->tv_usec)
116         return 1;
117
118     return 0;
119 }
120
121 pa_usec_t pa_timeval_age(const struct timeval *tv) {
122     struct timeval now;
123     assert(tv);
124
125     return pa_timeval_diff(pa_gettimeofday(&now), tv);
126 }
127
128 struct timeval* pa_timeval_add(struct timeval *tv, pa_usec_t v) {
129     unsigned long secs;
130     assert(tv);
131
132     secs = (v/1000000);
133     tv->tv_sec += (unsigned long) secs;
134     v -= secs*1000000;
135
136     tv->tv_usec += v;
137
138     /* Normalize */
139     while (tv->tv_usec >= 1000000) {
140         tv->tv_sec++;
141         tv->tv_usec -= 1000000;
142     }
143
144     return tv;
145 }