1 /*****************************************************************************
2 * mtime.c: high resolution time management functions
3 * Functions are prototyped in vlc_tick.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>
43 * Convert seconds to a time in the format h:mm:ss.
45 * This function is provided for any interface function which need to print a
46 * time string in the format h:mm:ss
48 * \param secs the date to be converted
49 * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
50 * \return psz_buffer is returned so this can be used as printf parameter.
52 char *secstotimestr( char *psz_buffer
, int32_t i_seconds
)
54 if( unlikely(i_seconds
< 0) )
56 secstotimestr( psz_buffer
+ 1, -i_seconds
);
63 d
= div( i_seconds
, 60 );
65 d
= div( d
.quot
, 60 );
68 snprintf( psz_buffer
, MSTRTIME_MAX_SIZE
, "%u:%02u:%02u",
69 d
.quot
, d
.rem
, i_seconds
);
71 snprintf( psz_buffer
, MSTRTIME_MAX_SIZE
, "%02u:%02u",
77 * Date management (internal and external)
80 void date_Init( date_t
*p_date
, uint32_t i_divider_n
, uint32_t i_divider_d
)
82 p_date
->date
= VLC_TICK_INVALID
;
83 p_date
->i_divider_num
= i_divider_n
;
84 p_date
->i_divider_den
= i_divider_d
;
85 p_date
->i_remainder
= 0;
88 void date_Change( date_t
*p_date
, uint32_t i_divider_n
, uint32_t i_divider_d
)
90 /* change time scale of remainder */
91 p_date
->i_remainder
= p_date
->i_remainder
* i_divider_n
/ p_date
->i_divider_num
;
92 p_date
->i_divider_num
= i_divider_n
;
93 p_date
->i_divider_den
= i_divider_d
;
96 vlc_tick_t
date_Increment( date_t
*p_date
, uint32_t i_nb_samples
)
98 if(unlikely(p_date
->date
== VLC_TICK_INVALID
))
99 return VLC_TICK_INVALID
;
100 assert( p_date
->i_divider_num
!= 0 );
101 vlc_tick_t i_dividend
= i_nb_samples
* CLOCK_FREQ
* p_date
->i_divider_den
;
102 lldiv_t d
= lldiv( i_dividend
, p_date
->i_divider_num
);
104 p_date
->date
+= d
.quot
;
105 p_date
->i_remainder
+= (int)d
.rem
;
107 if( p_date
->i_remainder
>= p_date
->i_divider_num
)
109 /* This is Bresenham algorithm. */
110 assert( p_date
->i_remainder
< 2*p_date
->i_divider_num
);
112 p_date
->i_remainder
-= p_date
->i_divider_num
;
118 vlc_tick_t
date_Decrement( date_t
*p_date
, uint32_t i_nb_samples
)
120 if(unlikely(p_date
->date
== VLC_TICK_INVALID
))
121 return VLC_TICK_INVALID
;
122 vlc_tick_t i_dividend
= (vlc_tick_t
)i_nb_samples
* CLOCK_FREQ
* p_date
->i_divider_den
;
123 p_date
->date
-= i_dividend
/ p_date
->i_divider_num
;
124 unsigned i_rem_adjust
= i_dividend
% p_date
->i_divider_num
;
126 if( p_date
->i_remainder
< i_rem_adjust
)
128 /* This is Bresenham algorithm. */
129 assert( p_date
->i_remainder
< p_date
->i_divider_num
);
131 p_date
->i_remainder
+= p_date
->i_divider_num
;
134 p_date
->i_remainder
-= i_rem_adjust
;
140 * @return NTP 64-bits timestamp in host byte order.
142 uint64_t NTPtime64(void)
146 timespec_get(&ts
, TIME_UTC
);
148 /* Convert nanoseconds to 32-bits fraction (232 picosecond units) */
149 uint64_t t
= (uint64_t)(ts
.tv_nsec
) << 32;
152 /* The offset to Unix epoch is 70 years (incl. 17 leap ones). There were
153 * no leap seconds during that period since they had not been invented yet.
155 t
|= ((UINT64_C(70) * 365 + 17) * 24 * 60 * 60 + ts
.tv_sec
) << 32;
159 struct timespec
timespec_from_vlc_tick (vlc_tick_t date
)
161 lldiv_t d
= lldiv (date
, CLOCK_FREQ
);
162 struct timespec ts
= { d
.quot
, NS_FROM_VLC_TICK( d
.rem
) };