]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
Split mdate(), mwait(), msleep() to POSIX and Win32 files
[vlc] / src / misc / mtime.c
1 /*****************************************************************************
2  * mtime.c: high resolution time management functions
3  * Functions are prototyped in vlc_mtime.h.
4  *****************************************************************************
5  * Copyright (C) 1998-2007 the VideoLAN team
6  * Copyright © 2006-2007 Rémi Denis-Courmont
7  * $Id$
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Rémi Denis-Courmont <rem$videolan,org>
11  *          Gisle Vanem
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <assert.h>
38
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42 #if !defined (_POSIX_TIMERS)
43 # define _POSIX_TIMERS (-1)
44 # include <sys/time.h>
45 #else
46 # include <time.h> /* clock_gettime() */
47 #endif
48
49 /**
50  * Return a date in a readable format
51  *
52  * This function converts a mtime date into a string.
53  * psz_buffer should be a buffer long enough to store the formatted
54  * date.
55  * \param date to be converted
56  * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
57  * \return psz_buffer is returned so this can be used as printf parameter.
58  */
59 char *mstrtime( char *psz_buffer, mtime_t date )
60 {
61     static const mtime_t ll1000 = 1000, ll60 = 60, ll24 = 24;
62
63     snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%02d:%02d:%02d-%03d.%03d",
64              (int) (date / (ll1000 * ll1000 * ll60 * ll60) % ll24),
65              (int) (date / (ll1000 * ll1000 * ll60) % ll60),
66              (int) (date / (ll1000 * ll1000) % ll60),
67              (int) (date / ll1000 % ll1000),
68              (int) (date % ll1000) );
69     return( psz_buffer );
70 }
71
72 /**
73  * Convert seconds to a time in the format h:mm:ss.
74  *
75  * This function is provided for any interface function which need to print a
76  * time string in the format h:mm:ss
77  * date.
78  * \param secs  the date to be converted
79  * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
80  * \return psz_buffer is returned so this can be used as printf parameter.
81  */
82 char *secstotimestr( char *psz_buffer, int32_t i_seconds )
83 {
84     if( unlikely(i_seconds < 0) )
85     {
86         secstotimestr( psz_buffer + 1, -i_seconds );
87         *psz_buffer = '-';
88         return psz_buffer;
89     }
90
91     div_t d;
92
93     d = div( i_seconds, 60 );
94     i_seconds = d.rem;
95     d = div( d.quot, 60 );
96
97     if( d.quot )
98         snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%u:%02u:%02u",
99                  d.quot, d.rem, i_seconds );
100     else
101         snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%02u:%02u",
102                   d.rem, i_seconds );
103     return psz_buffer;
104 }
105
106 /*
107  * Date management (internal and external)
108  */
109
110 /**
111  * Initialize a date_t.
112  *
113  * \param date to initialize
114  * \param divider (sample rate) numerator
115  * \param divider (sample rate) denominator
116  */
117
118 void date_Init( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
119 {
120     p_date->date = 0;
121     p_date->i_divider_num = i_divider_n;
122     p_date->i_divider_den = i_divider_d;
123     p_date->i_remainder = 0;
124 }
125
126 /**
127  * Change a date_t.
128  *
129  * \param date to change
130  * \param divider (sample rate) numerator
131  * \param divider (sample rate) denominator
132  */
133
134 void date_Change( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
135 {
136     /* change time scale of remainder */
137     p_date->i_remainder = p_date->i_remainder * i_divider_n / p_date->i_divider_num;
138     p_date->i_divider_num = i_divider_n;
139     p_date->i_divider_den = i_divider_d;
140 }
141
142 /**
143  * Set the date value of a date_t.
144  *
145  * \param date to set
146  * \param date value
147  */
148 void date_Set( date_t *p_date, mtime_t i_new_date )
149 {
150     p_date->date = i_new_date;
151     p_date->i_remainder = 0;
152 }
153
154 /**
155  * Get the date of a date_t
156  *
157  * \param date to get
158  * \return date value
159  */
160 mtime_t date_Get( const date_t *p_date )
161 {
162     return p_date->date;
163 }
164
165 /**
166  * Move forwards or backwards the date of a date_t.
167  *
168  * \param date to move
169  * \param difference value
170  */
171 void date_Move( date_t *p_date, mtime_t i_difference )
172 {
173     p_date->date += i_difference;
174 }
175
176 /**
177  * Increment the date and return the result, taking into account
178  * rounding errors.
179  *
180  * \param date to increment
181  * \param incrementation in number of samples
182  * \return date value
183  */
184 mtime_t date_Increment( date_t *p_date, uint32_t i_nb_samples )
185 {
186     mtime_t i_dividend = i_nb_samples * CLOCK_FREQ * p_date->i_divider_den;
187     lldiv_t d = lldiv( i_dividend, p_date->i_divider_num );
188
189     p_date->date += d.quot;
190     p_date->i_remainder += (int)d.rem;
191
192     if( p_date->i_remainder >= p_date->i_divider_num )
193     {
194         /* This is Bresenham algorithm. */
195         assert( p_date->i_remainder < 2*p_date->i_divider_num);
196         p_date->date += 1;
197         p_date->i_remainder -= p_date->i_divider_num;
198     }
199
200     return p_date->date;
201 }
202
203 /**
204  * Decrement the date and return the result, taking into account
205  * rounding errors.
206  *
207  * \param date to decrement
208  * \param decrementation in number of samples
209  * \return date value
210  */
211 mtime_t date_Decrement( date_t *p_date, uint32_t i_nb_samples )
212 {
213     mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000 * p_date->i_divider_den;
214     p_date->date -= i_dividend / p_date->i_divider_num;
215     unsigned i_rem_adjust = i_dividend % p_date->i_divider_num;
216
217     if( p_date->i_remainder < i_rem_adjust )
218     {
219         /* This is Bresenham algorithm. */
220         assert( p_date->i_remainder > -p_date->i_divider_num);
221         p_date->date -= 1;
222         p_date->i_remainder += p_date->i_divider_num;
223     }
224
225     p_date->i_remainder -= i_rem_adjust;
226
227     return p_date->date;
228 }
229
230 #ifndef HAVE_GETTIMEOFDAY
231
232 #ifdef WIN32
233
234 /*
235  * Number of micro-seconds between the beginning of the Windows epoch
236  * (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970).
237  *
238  * This assumes all Win32 compilers have 64-bit support.
239  */
240 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) || defined(__WATCOMC__)
241 #   define DELTA_EPOCH_IN_USEC  11644473600000000Ui64
242 #else
243 #   define DELTA_EPOCH_IN_USEC  11644473600000000ULL
244 #endif
245
246 static uint64_t filetime_to_unix_epoch (const FILETIME *ft)
247 {
248     uint64_t res = (uint64_t) ft->dwHighDateTime << 32;
249
250     res |= ft->dwLowDateTime;
251     res /= 10;                   /* from 100 nano-sec periods to usec */
252     res -= DELTA_EPOCH_IN_USEC;  /* from Win epoch to Unix epoch */
253     return (res);
254 }
255
256 static int gettimeofday (struct timeval *tv, void *tz )
257 {
258     FILETIME  ft;
259     uint64_t tim;
260
261     if (!tv) {
262         return VLC_EGENERIC;
263     }
264     GetSystemTimeAsFileTime (&ft);
265     tim = filetime_to_unix_epoch (&ft);
266     tv->tv_sec  = (long) (tim / 1000000L);
267     tv->tv_usec = (long) (tim % 1000000L);
268     return (0);
269 }
270
271 #endif
272
273 #endif
274
275 /**
276  * @return NTP 64-bits timestamp in host byte order.
277  */
278 uint64_t NTPtime64 (void)
279 {
280 #if (_POSIX_TIMERS > 0)
281     struct timespec ts;
282
283     clock_gettime (CLOCK_REALTIME, &ts);
284 #else
285     struct timeval tv;
286     struct
287     {
288         uint32_t tv_sec;
289         uint32_t tv_nsec;
290     } ts;
291
292     gettimeofday (&tv, NULL);
293     ts.tv_sec = tv.tv_sec;
294     ts.tv_nsec = tv.tv_usec * 1000;
295 #endif
296
297     /* Convert nanoseconds to 32-bits fraction (232 picosecond units) */
298     uint64_t t = (uint64_t)(ts.tv_nsec) << 32;
299     t /= 1000000000;
300
301
302     /* There is 70 years (incl. 17 leap ones) offset to the Unix Epoch.
303      * No leap seconds during that period since they were not invented yet.
304      */
305     assert (t < 0x100000000);
306     t |= ((70LL * 365 + 17) * 24 * 60 * 60 + ts.tv_sec) << 32;
307     return t;
308 }
309