Asterisk - The Open Source Telephony Project  18.5.0
time.h
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  * \brief Time-related functions and macros
21  */
22 
23 #ifndef _ASTERISK_TIME_H
24 #define _ASTERISK_TIME_H
25 
26 #include "asterisk/autoconfig.h"
27 
28 #ifdef HAVE_SYS_TIME_H
29 #include <sys/time.h>
30 #endif
31 
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 
36 #include "asterisk/inline_api.h"
37 
38 /* We have to let the compiler learn what types to use for the elements of a
39  struct timeval since on linux, it's time_t and suseconds_t, but on *BSD,
40  they are just a long.
41  note:dummy_tv_var_for_types never actually gets exported, only used as
42  local place holder. */
43 extern struct timeval dummy_tv_var_for_types;
44 typedef typeof(dummy_tv_var_for_types.tv_sec) ast_time_t;
45 typedef typeof(dummy_tv_var_for_types.tv_usec) ast_suseconds_t;
46 
47 /*!
48  * \brief Computes the difference (in seconds) between two \c struct \c timeval instances.
49  * \param end the end of the time period
50  * \param start the beginning of the time period
51  * \return the difference in seconds
52  */
54 int64_t ast_tvdiff_sec(struct timeval end, struct timeval start),
55 {
56  int64_t result = end.tv_sec - start.tv_sec;
57  if (result > 0 && end.tv_usec < start.tv_usec)
58  result--;
59  else if (result < 0 && end.tv_usec > start.tv_usec)
60  result++;
61 
62  return result;
63 }
64 )
65 
66 /*!
67  * \brief Computes the difference (in microseconds) between two \c struct \c timeval instances.
68  * \param end the end of the time period
69  * \param start the beginning of the time period
70  * \return the difference in microseconds
71  */
73 int64_t ast_tvdiff_us(struct timeval end, struct timeval start),
74 {
75  return (end.tv_sec - start.tv_sec) * (int64_t) 1000000 +
76  end.tv_usec - start.tv_usec;
77 }
78 )
79 
80 /*!
81  * \brief Computes the difference (in milliseconds) between two \c struct \c timeval instances.
82  * \param end end of the time period
83  * \param start beginning of the time period
84  * \return the difference in milliseconds
85  */
87 int64_t ast_tvdiff_ms(struct timeval end, struct timeval start),
88 {
89  /* the offset by 1,000,000 below is intentional...
90  it avoids differences in the way that division
91  is handled for positive and negative numbers, by ensuring
92  that the divisor is always positive
93  */
94  int64_t sec_dif = (int64_t)(end.tv_sec - start.tv_sec) * 1000;
95  int64_t usec_dif = (1000000 + end.tv_usec - start.tv_usec) / 1000 - 1000;
96  return sec_dif + usec_dif;
97 }
98 )
99 
100 /*!
101  * \brief Returns true if the argument is 0,0
102  */
104 int ast_tvzero(const struct timeval t),
105 {
106  return (t.tv_sec == 0 && t.tv_usec == 0);
107 }
108 )
109 
110 /*!
111  * \brief Compres two \c struct \c timeval instances returning
112  * -1, 0, 1 if the first arg is smaller, equal or greater to the second.
113  */
115 int ast_tvcmp(struct timeval _a, struct timeval _b),
116 {
117  if (_a.tv_sec < _b.tv_sec)
118  return -1;
119  if (_a.tv_sec > _b.tv_sec)
120  return 1;
121  /* now seconds are equal */
122  if (_a.tv_usec < _b.tv_usec)
123  return -1;
124  if (_a.tv_usec > _b.tv_usec)
125  return 1;
126  return 0;
127 }
128 )
129 
130 /*!
131  * \brief Returns true if the two \c struct \c timeval arguments are equal.
132  */
134 int ast_tveq(struct timeval _a, struct timeval _b),
135 {
136  return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
137 }
138 )
139 
140 /*!
141  * \brief Returns current timeval. Meant to replace calls to gettimeofday().
142  */
144 struct timeval ast_tvnow(void),
145 {
146  struct timeval t;
147  gettimeofday(&t, NULL);
148  return t;
149 }
150 )
151 
152 /*!
153  * \brief Returns current timespec. Meant to avoid calling ast_tvnow() just to
154  * create a timespec from the timeval it returns.
155  */
156 #if defined _POSIX_TIMERS && _POSIX_TIMERS > 0
158 struct timespec ast_tsnow(void),
159 {
160  struct timespec ts;
161  clock_gettime(CLOCK_REALTIME, &ts);
162  return ts;
163 }
164 )
165 #else
167 struct timespec ast_tsnow(void),
168 {
169  struct timeval tv = ast_tvnow();
170  struct timespec ts;
171  /* Can't use designated initializer, because it does odd things with
172  * the AST_INLINE_API macro. Go figure. */
173  ts.tv_sec = tv.tv_sec;
174  ts.tv_nsec = tv.tv_usec * 1000;
175  return ts;
176 }
177 )
178 #endif
179 
180 /*!
181  * \brief Returns the sum of two timevals a + b
182  */
183 struct timeval ast_tvadd(struct timeval a, struct timeval b);
184 
185 /*!
186  * \brief Returns the difference of two timevals a - b
187  */
188 struct timeval ast_tvsub(struct timeval a, struct timeval b);
189 
190 /*!
191  * \since 12
192  * \brief Formats a duration into HH:MM:SS
193  *
194  * \param duration The time (in seconds) to format
195  * \param buf A buffer to hold the formatted string'
196  * \param length The size of the buffer
197  */
198 void ast_format_duration_hh_mm_ss(int duration, char *buf, size_t length);
199 
200 
201 /*!
202  * \brief Calculate remaining milliseconds given a starting timestamp
203  * and upper bound
204  *
205  * If the upper bound is negative, then this indicates that there is no
206  * upper bound on the amount of time to wait. This will result in a
207  * negative return.
208  *
209  * \param start When timing started being calculated
210  * \param max_ms The maximum number of milliseconds to wait from start. May be negative.
211  * \return The number of milliseconds left to wait for. May be negative.
212  */
213 int ast_remaining_ms(struct timeval start, int max_ms);
214 
215 /*!
216  * \brief Returns a timeval from sec, usec
217  */
219 struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec),
220 {
221  struct timeval t;
222  t.tv_sec = sec;
223  t.tv_usec = usec;
224  return t;
225 }
226 )
227 
228 /*!
229  * \brief Returns a timeval corresponding to the duration of n samples at rate r.
230  * Useful to convert samples to timevals, or even milliseconds to timevals
231  * in the form ast_samp2tv(milliseconds, 1000)
232  */
234 struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate),
235 {
236  return ast_tv(_nsamp / _rate, (_nsamp % _rate) * (1000000 / (float) _rate));
237 }
238 )
239 
240 /*!
241  * \brief Time units enumeration.
242  */
243 enum TIME_UNIT {
255 };
256 
257 /*!
258  * \brief Convert a string to a time unit enumeration value.
259  *
260  * This method attempts to be as flexible, and forgiving as possible when
261  * converting. In most cases the algorithm will match on the beginning of
262  * up to three strings (short, medium, long form). So that means if the
263  * given string at least starts with one of the form values it will match.
264  *
265  * For example: us, usec, microsecond will all map to TIME_UNIT_MICROSECOND.
266  * So will uss, usecs, miscroseconds, or even microsecondvals
267  *
268  * Matching is also not case sensitive.
269  *
270  * \param unit The string to map to an enumeration
271  *
272  * \return A time unit enumeration
273  */
274 enum TIME_UNIT ast_time_str_to_unit(const char *unit);
275 
276 /*!
277  * \brief Convert a timeval structure to microseconds
278  *
279  * \param tv The timeval to convert
280  *
281  * \return The time in microseconds
282  */
283 ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv);
284 
285 /*!
286  * \brief Create a timeval object initialized to given values.
287  *
288  * \param sec The timeval seconds value
289  * \param usec The timeval microseconds value
290  *
291  * \return A timeval object
292  */
293 struct timeval ast_time_create(ast_time_t sec, ast_suseconds_t usec);
294 
295 /*!
296  * \brief Convert the given unit value, and create a timeval object from it.
297  *
298  * \param val The value to convert to a timeval
299  * \param unit The time unit type of val
300  *
301  * \return A timeval object
302  */
303 struct timeval ast_time_create_by_unit(unsigned long val, enum TIME_UNIT unit);
304 
305 /*!
306  * \brief Convert the given unit value, and create a timeval object from it.
307  *
308  * This will first attempt to convert the unit from a string to a TIME_UNIT
309  * enumeration. If that conversion fails then a zeroed out timeval object
310  * is returned.
311  *
312  * \param val The value to convert to a timeval
313  * \param unit The time unit type of val
314  *
315  * \return A timeval object
316  */
317 struct timeval ast_time_create_by_unit_str(unsigned long val, const char *unit);
318 
319 #endif /* _ASTERISK_TIME_H */
typedef typeof(dummy_tv_var_for_types.tv_sec) ast_time_t
ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv)
Convert a timeval structure to microseconds.
Definition: time.c:89
Definition: ast_expr2.c:325
int ast_tveq(struct timeval _a, struct timeval _b)
Returns true if the two struct timeval arguments are equal.
Definition: time.h:138
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
int64_t ast_tvdiff_sec(struct timeval end, struct timeval start)
Computes the difference (in seconds) between two struct timeval instances.
Definition: time.h:64
TIME_UNIT
Time units enumeration.
Definition: time.h:243
struct timeval ast_time_create_by_unit_str(unsigned long val, const char *unit)
Convert the given unit value, and create a timeval object from it.
Definition: time.c:142
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:108
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
#define NULL
Definition: resample.c:96
char * end
Definition: eagi_proxy.c:73
Inlinable API function macro.
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:238
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compres two struct timeval instances returning -1, 0, 1 if the first arg is smaller, equal or greater to the second.
Definition: time.h:128
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
Definition: main/utils.c:2033
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2283
struct timeval ast_time_create(ast_time_t sec, ast_suseconds_t usec)
Create a timeval object initialized to given values.
Definition: time.c:94
struct timeval dummy_tv_var_for_types
enum TIME_UNIT ast_time_str_to_unit(const char *unit)
Convert a string to a time unit enumeration value.
Definition: time.c:65
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:226
static PGresult * result
Definition: cel_pgsql.c:88
struct timespec ast_tsnow(void)
Returns current timespec. Meant to avoid calling ast_tvnow() just to create a timespec from the timev...
Definition: time.h:177
static struct test_val b
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: extconf.c:2298
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
Definition: time.h:78
void ast_format_duration_hh_mm_ss(int duration, char *buf, size_t length)
Formats a duration into HH:MM:SS.
Definition: main/utils.c:2049
struct timeval ast_time_create_by_unit(unsigned long val, enum TIME_UNIT unit)
Convert the given unit value, and create a timeval object from it.
Definition: time.c:112
static struct test_val a
#define AST_INLINE_API(hdr, body)
Definition: inline_api.h:54