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
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10 * Rémi Denis-Courmont <rem$videolan,org>
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 /*****************************************************************************
30 *****************************************************************************/
36 #include <vlc_common.h>
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
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
);
62 d
= div( i_seconds
, 60 );
64 d
= div( d
.quot
, 60 );
67 snprintf( psz_buffer
, MSTRTIME_MAX_SIZE
, "%u:%02u:%02u",
68 d
.quot
, d
.rem
, i_seconds
);
70 snprintf( psz_buffer
, MSTRTIME_MAX_SIZE
, "%02u:%02u",
76 * Date management (internal and external)
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
)
90 p_date
->i_divider_num
= i_divider_n
;
91 p_date
->i_divider_den
= i_divider_d
;
92 p_date
->i_remainder
= 0;
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.
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
129 mtime_t
date_Get( const date_t
*p_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
149 * \param date to increment
150 * \param incrementation in number of samples
153 mtime_t
date_Increment( date_t
*p_date
, uint32_t i_nb_samples
)
155 mtime_t i_dividend
= i_nb_samples
* CLOCK_FREQ
* p_date
->i_divider_den
;
156 lldiv_t d
= lldiv( i_dividend
, p_date
->i_divider_num
);
158 p_date
->date
+= d
.quot
;
159 p_date
->i_remainder
+= (int)d
.rem
;
161 if( p_date
->i_remainder
>= p_date
->i_divider_num
)
163 /* This is Bresenham algorithm. */
164 assert( p_date
->i_remainder
< 2*p_date
->i_divider_num
);
166 p_date
->i_remainder
-= p_date
->i_divider_num
;
173 * Decrement the date and return the result, taking into account
176 * \param date to decrement
177 * \param decrementation in number of samples
180 mtime_t
date_Decrement( date_t
*p_date
, uint32_t i_nb_samples
)
182 mtime_t i_dividend
= (mtime_t
)i_nb_samples
* CLOCK_FREQ
* p_date
->i_divider_den
;
183 p_date
->date
-= i_dividend
/ p_date
->i_divider_num
;
184 unsigned i_rem_adjust
= i_dividend
% p_date
->i_divider_num
;
186 if( p_date
->i_remainder
< i_rem_adjust
)
188 /* This is Bresenham algorithm. */
189 assert( p_date
->i_remainder
< p_date
->i_divider_num
);
191 p_date
->i_remainder
+= p_date
->i_divider_num
;
194 p_date
->i_remainder
-= i_rem_adjust
;
200 * @return NTP 64-bits timestamp in host byte order.
202 uint64_t NTPtime64(void)
206 timespec_get(&ts
, TIME_UTC
);
208 /* Convert nanoseconds to 32-bits fraction (232 picosecond units) */
209 uint64_t t
= (uint64_t)(ts
.tv_nsec
) << 32;
212 /* The offset to Unix epoch is 70 years (incl. 17 leap ones). There were
213 * no leap seconds during that period since they had not been invented yet.
215 t
|= ((UINT64_C(70) * 365 + 17) * 24 * 60 * 60 + ts
.tv_sec
) << 32;