2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
20 * \brief Time-related functions and macros
23 #ifndef _ASTERISK_TIME_H
24 #define _ASTERISK_TIME_H
26 #ifdef HAVE_SYS_TIME_H
30 #include "asterisk/inline_api.h"
32 /* We have to let the compiler learn what types to use for the elements of a
33 struct timeval since on linux, it's time_t and suseconds_t, but on *BSD,
34 they are just a long. */
35 extern struct timeval tv;
36 typedef typeof(tv.tv_sec) ast_time_t;
37 typedef typeof(tv.tv_usec) ast_suseconds_t;
40 * \brief Computes the difference (in milliseconds) between two \c struct \c timeval instances.
41 * \param end end of the time period
42 * \param start beginning of the time period
43 * \return the difference in milliseconds
46 int ast_tvdiff_ms(struct timeval end, struct timeval start),
48 /* the offset by 1,000,000 below is intentional...
49 it avoids differences in the way that division
50 is handled for positive and negative numbers, by ensuring
51 that the divisor is always positive
53 return ((end.tv_sec - start.tv_sec) * 1000) +
54 (((1000000 + end.tv_usec - start.tv_usec) / 1000) - 1000);
59 * \brief Returns true if the argument is 0,0
62 int ast_tvzero(const struct timeval t),
64 return (t.tv_sec == 0 && t.tv_usec == 0);
69 * \brief Compres two \c struct \c timeval instances returning
70 * -1, 0, 1 if the first arg is smaller, equal or greater to the second.
73 int ast_tvcmp(struct timeval _a, struct timeval _b),
75 if (_a.tv_sec < _b.tv_sec)
77 if (_a.tv_sec > _b.tv_sec)
79 /* now seconds are equal */
80 if (_a.tv_usec < _b.tv_usec)
82 if (_a.tv_usec > _b.tv_usec)
89 * \brief Returns true if the two \c struct \c timeval arguments are equal.
92 int ast_tveq(struct timeval _a, struct timeval _b),
94 return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
99 * \brief Returns current timeval. Meant to replace calls to gettimeofday().
102 struct timeval ast_tvnow(void),
105 gettimeofday(&t, NULL);
111 * \brief Returns the sum of two timevals a + b
113 struct timeval ast_tvadd(struct timeval a, struct timeval b);
116 * \brief Returns the difference of two timevals a - b
118 struct timeval ast_tvsub(struct timeval a, struct timeval b);
121 * \brief Returns a timeval from sec, usec
124 struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec),
134 * \brief Returns a timeval corresponding to the duration of n samples at rate r.
135 * Useful to convert samples to timevals, or even milliseconds to timevals
136 * in the form ast_samp2tv(milliseconds, 1000)
139 struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate),
141 return ast_tv(_nsamp / _rate, (_nsamp % _rate) * (1000000 / _rate));
145 #endif /* _ASTERISK_TIME_H */