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: 2d579005e7a44845cba99871abc5452d4c3212a6 $
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Rémi Denis-Courmont
11  *          Gisle Vanem
12  *
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.
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 Lesser General Public License for more details.
22  *
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  *****************************************************************************/
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 #include <time.h>
40 
41 /**
42  * Convert seconds to a time in the format h:mm:ss.
43  *
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.
50  */
secstotimestr(char * psz_buffer,int32_t i_seconds)51 char *secstotimestr( char *psz_buffer, int32_t i_seconds )
52 {
53     if( unlikely(i_seconds < 0) )
54     {
55         secstotimestr( psz_buffer + 1, -i_seconds );
56         *psz_buffer = '-';
57         return psz_buffer;
58     }
59 
60     div_t d;
61 
62     d = div( i_seconds, 60 );
63     i_seconds = d.rem;
64     d = div( d.quot, 60 );
65 
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;
73 }
74 
75 /*
76  * Date management (internal and external)
77  */
78 
79 /**
80  * Initialize a date_t.
81  *
82  * \param date to initialize
83  * \param divider (sample rate) numerator
84  * \param divider (sample rate) denominator
85  */
86 
date_Init(date_t * p_date,uint32_t i_divider_n,uint32_t i_divider_d)87 void date_Init( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
88 {
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;
93 }
94 
95 /**
96  * Change a date_t.
97  *
98  * \param date to change
99  * \param divider (sample rate) numerator
100  * \param divider (sample rate) denominator
101  */
102 
date_Change(date_t * p_date,uint32_t i_divider_n,uint32_t i_divider_d)103 void date_Change( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
104 {
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;
109 }
110 
111 /**
112  * Set the date value of a date_t.
113  *
114  * \param date to set
115  * \param date value
116  */
date_Set(date_t * p_date,mtime_t i_new_date)117 void date_Set( date_t *p_date, mtime_t i_new_date )
118 {
119     p_date->date = i_new_date;
120     p_date->i_remainder = 0;
121 }
122 
123 /**
124  * Get the date of a date_t
125  *
126  * \param date to get
127  * \return date value
128  */
date_Get(const date_t * p_date)129 mtime_t date_Get( const date_t *p_date )
130 {
131     return p_date->date;
132 }
133 
134 /**
135  * Move forwards or backwards the date of a date_t.
136  *
137  * \param date to move
138  * \param difference value
139  */
date_Move(date_t * p_date,mtime_t i_difference)140 void date_Move( date_t *p_date, mtime_t i_difference )
141 {
142     p_date->date += i_difference;
143 }
144 
145 /**
146  * Increment the date and return the result, taking into account
147  * rounding errors.
148  *
149  * \param date to increment
150  * \param incrementation in number of samples
151  * \return date value
152  */
date_Increment(date_t * p_date,uint32_t i_nb_samples)153 mtime_t date_Increment( date_t *p_date, uint32_t i_nb_samples )
154 {
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 );
158 
159     p_date->date += d.quot;
160     p_date->i_remainder += (int)d.rem;
161 
162     if( p_date->i_remainder >= p_date->i_divider_num )
163     {
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;
168     }
169 
170     return p_date->date;
171 }
172 
173 /**
174  * Decrement the date and return the result, taking into account
175  * rounding errors.
176  *
177  * \param date to decrement
178  * \param decrementation in number of samples
179  * \return date value
180  */
date_Decrement(date_t * p_date,uint32_t i_nb_samples)181 mtime_t date_Decrement( date_t *p_date, uint32_t i_nb_samples )
182 {
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;
186 
187     if( p_date->i_remainder < i_rem_adjust )
188     {
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;
193     }
194 
195     p_date->i_remainder -= i_rem_adjust;
196 
197     return p_date->date;
198 }
199 
200 /**
201  * @return NTP 64-bits timestamp in host byte order.
202  */
NTPtime64(void)203 uint64_t NTPtime64(void)
204 {
205     struct timespec ts;
206 
207     timespec_get(&ts, TIME_UTC);
208 
209     /* Convert nanoseconds to 32-bits fraction (232 picosecond units) */
210     uint64_t t = (uint64_t)(ts.tv_nsec) << 32;
211     t /= 1000000000;
212 
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.
215      */
216     t |= ((UINT64_C(70) * 365 + 17) * 24 * 60 * 60 + ts.tv_sec) << 32;
217     return t;
218 }
219