asx: remove useless test
[vlc.git] / src / misc / mtime.c
blobd08187cd66d8776966ab3e02e0cc106c04084b77
1 /*****************************************************************************
2 * mtime.c: high resolution time management functions
3 * Functions are prototyped in vlc_mtime.h.
4 *****************************************************************************
5 * Copyright (C) 1998-2007 VLC authors and VideoLAN
6 * Copyright © 2006-2007 Rémi Denis-Courmont
7 * $Id$
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10 * Rémi Denis-Courmont <rem$videolan,org>
11 * Gisle Vanem
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
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 Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 /*****************************************************************************
29 * Preamble
30 *****************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
36 #include <vlc_common.h>
37 #include <assert.h>
39 #include <time.h>
41 /**
42 * Convert seconds to a time in the format h:mm:ss.
44 * This function is provided for any interface function which need to print a
45 * time string in the format h:mm:ss
46 * date.
47 * \param secs the date to be converted
48 * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
49 * \return psz_buffer is returned so this can be used as printf parameter.
51 char *secstotimestr( char *psz_buffer, int32_t i_seconds )
53 if( unlikely(i_seconds < 0) )
55 secstotimestr( psz_buffer + 1, -i_seconds );
56 *psz_buffer = '-';
57 return psz_buffer;
60 div_t d;
62 d = div( i_seconds, 60 );
63 i_seconds = d.rem;
64 d = div( d.quot, 60 );
66 if( d.quot )
67 snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%u:%02u:%02u",
68 d.quot, d.rem, i_seconds );
69 else
70 snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%02u:%02u",
71 d.rem, i_seconds );
72 return psz_buffer;
76 * Date management (internal and external)
79 /**
80 * Initialize a date_t.
82 * \param date to initialize
83 * \param divider (sample rate) numerator
84 * \param divider (sample rate) denominator
87 void date_Init( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
89 p_date->date = 0;
90 p_date->i_divider_num = i_divider_n;
91 p_date->i_divider_den = i_divider_d;
92 p_date->i_remainder = 0;
95 /**
96 * Change a date_t.
98 * \param date to change
99 * \param divider (sample rate) numerator
100 * \param divider (sample rate) denominator
103 void date_Change( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
105 /* change time scale of remainder */
106 p_date->i_remainder = p_date->i_remainder * i_divider_n / p_date->i_divider_num;
107 p_date->i_divider_num = i_divider_n;
108 p_date->i_divider_den = i_divider_d;
112 * Set the date value of a date_t.
114 * \param date to set
115 * \param date value
117 void date_Set( date_t *p_date, mtime_t i_new_date )
119 p_date->date = i_new_date;
120 p_date->i_remainder = 0;
124 * Get the date of a date_t
126 * \param date to get
127 * \return date value
129 mtime_t date_Get( const date_t *p_date )
131 return p_date->date;
135 * Move forwards or backwards the date of a date_t.
137 * \param date to move
138 * \param difference value
140 void date_Move( date_t *p_date, mtime_t i_difference )
142 p_date->date += i_difference;
146 * Increment the date and return the result, taking into account
147 * rounding errors.
149 * \param date to increment
150 * \param incrementation in number of samples
151 * \return date value
153 mtime_t date_Increment( date_t *p_date, uint32_t i_nb_samples )
155 assert( p_date->i_divider_num != 0 );
156 mtime_t i_dividend = i_nb_samples * CLOCK_FREQ * p_date->i_divider_den;
157 lldiv_t d = lldiv( i_dividend, p_date->i_divider_num );
159 p_date->date += d.quot;
160 p_date->i_remainder += (int)d.rem;
162 if( p_date->i_remainder >= p_date->i_divider_num )
164 /* This is Bresenham algorithm. */
165 assert( p_date->i_remainder < 2*p_date->i_divider_num);
166 p_date->date += 1;
167 p_date->i_remainder -= p_date->i_divider_num;
170 return p_date->date;
174 * Decrement the date and return the result, taking into account
175 * rounding errors.
177 * \param date to decrement
178 * \param decrementation in number of samples
179 * \return date value
181 mtime_t date_Decrement( date_t *p_date, uint32_t i_nb_samples )
183 mtime_t i_dividend = (mtime_t)i_nb_samples * CLOCK_FREQ * p_date->i_divider_den;
184 p_date->date -= i_dividend / p_date->i_divider_num;
185 unsigned i_rem_adjust = i_dividend % p_date->i_divider_num;
187 if( p_date->i_remainder < i_rem_adjust )
189 /* This is Bresenham algorithm. */
190 assert( p_date->i_remainder < p_date->i_divider_num);
191 p_date->date -= 1;
192 p_date->i_remainder += p_date->i_divider_num;
195 p_date->i_remainder -= i_rem_adjust;
197 return p_date->date;
201 * @return NTP 64-bits timestamp in host byte order.
203 uint64_t NTPtime64(void)
205 struct timespec ts;
207 timespec_get(&ts, TIME_UTC);
209 /* Convert nanoseconds to 32-bits fraction (232 picosecond units) */
210 uint64_t t = (uint64_t)(ts.tv_nsec) << 32;
211 t /= 1000000000;
213 /* The offset to Unix epoch is 70 years (incl. 17 leap ones). There were
214 * no leap seconds during that period since they had not been invented yet.
216 t |= ((UINT64_C(70) * 365 + 17) * 24 * 60 * 60 + ts.tv_sec) << 32;
217 return t;