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 * Time-related functions and macros
23 #ifndef _ASTERISK_TIME_H
24 #define _ASTERISK_TIME_H
28 #include "asterisk/inline_api.h"
30 /* We have to let the compiler learn what types to use for the elements of a
31 struct timeval since on linux, it's time_t and suseconds_t, but on *BSD,
32 they are just a long. */
33 extern struct timeval tv;
34 typedef typeof(tv.tv_sec) ast_time_t;
35 typedef typeof(tv.tv_usec) ast_suseconds_t;
38 * \brief Computes the difference (in milliseconds) between two \c struct \c timeval instances.
39 * \param end the beginning of the time period
40 * \param start the end of the time period
41 * \return the difference in milliseconds
44 int ast_tvdiff_ms(struct timeval end, struct timeval start),
46 /* the offset by 1,000,000 below is intentional...
47 it avoids differences in the way that division
48 is handled for positive and negative numbers, by ensuring
49 that the divisor is always positive
51 return ((end.tv_sec - start.tv_sec) * 1000) +
52 (((1000000 + end.tv_usec - start.tv_usec) / 1000) - 1000);
57 * \brief Returns true if the argument is 0,0
60 int ast_tvzero(const struct timeval t),
62 return (t.tv_sec == 0 && t.tv_usec == 0);
67 * \brief Compres two \c struct \c timeval instances returning
68 * -1, 0, 1 if the first arg is smaller, equal or greater to the second.
71 int ast_tvcmp(struct timeval _a, struct timeval _b),
73 if (_a.tv_sec < _b.tv_sec)
75 if (_a.tv_sec > _b.tv_sec)
77 /* now seconds are equal */
78 if (_a.tv_usec < _b.tv_usec)
80 if (_a.tv_usec > _b.tv_usec)
87 * \brief Returns true if the two \c struct \c timeval arguments are equal.
90 int ast_tveq(struct timeval _a, struct timeval _b),
92 return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
97 * \brief Returns current timeval. Meant to replace calls to gettimeofday().
100 struct timeval ast_tvnow(void),
103 gettimeofday(&t, NULL);
109 * \brief Returns the sum of two timevals a + b
111 struct timeval ast_tvadd(struct timeval a, struct timeval b);
114 * \brief Returns the difference of two timevals a - b
116 struct timeval ast_tvsub(struct timeval a, struct timeval b);
119 * \brief Returns a timeval from sec, usec
123 struct timeval ast_tv(int sec, int usec),
125 struct timeval t = { sec, usec};
131 struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec),
141 * \brief Returns a timeval corresponding to the duration of n samples at rate r.
142 * Useful to convert samples to timevals, or even milliseconds to timevals
143 * in the form ast_samp2tv(milliseconds, 1000)
146 struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate),
148 return ast_tv(_nsamp / _rate, (_nsamp % _rate) * (1000000 / _rate));
152 #endif /* _ASTERISK_TIME_H */