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
29 #include "asterisk/inline_api.h"
31 /* We have to let the compiler learn what types to use for the elements of a
32 struct timeval since on linux, it's time_t and suseconds_t, but on *BSD,
33 they are just a long. */
34 extern struct timeval tv
;
35 typedef typeof(tv
.tv_sec
) ast_time_t
;
36 typedef typeof(tv
.tv_usec
) ast_suseconds_t
;
39 * \brief Computes the difference (in milliseconds) between two \c struct \c timeval instances.
40 * \param end end of the time period
41 * \param start beginning of the time period
42 * \return the difference in milliseconds
45 int ast_tvdiff_ms(struct timeval end
, struct timeval start
),
47 /* the offset by 1,000,000 below is intentional...
48 it avoids differences in the way that division
49 is handled for positive and negative numbers, by ensuring
50 that the divisor is always positive
52 return ((end
.tv_sec
- start
.tv_sec
) * 1000) +
53 (((1000000 + end
.tv_usec
- start
.tv_usec
) / 1000) - 1000);
58 * \brief Returns true if the argument is 0,0
61 int ast_tvzero(const struct timeval t
),
63 return (t
.tv_sec
== 0 && t
.tv_usec
== 0);
68 * \brief Compres two \c struct \c timeval instances returning
69 * -1, 0, 1 if the first arg is smaller, equal or greater to the second.
72 int ast_tvcmp(struct timeval _a
, struct timeval _b
),
74 if (_a
.tv_sec
< _b
.tv_sec
)
76 if (_a
.tv_sec
> _b
.tv_sec
)
78 /* now seconds are equal */
79 if (_a
.tv_usec
< _b
.tv_usec
)
81 if (_a
.tv_usec
> _b
.tv_usec
)
88 * \brief Returns true if the two \c struct \c timeval arguments are equal.
91 int ast_tveq(struct timeval _a
, struct timeval _b
),
93 return (_a
.tv_sec
== _b
.tv_sec
&& _a
.tv_usec
== _b
.tv_usec
);
98 * \brief Returns current timeval. Meant to replace calls to gettimeofday().
101 struct timeval
ast_tvnow(void),
104 gettimeofday(&t
, NULL
);
110 * \brief Returns the sum of two timevals a + b
112 struct timeval
ast_tvadd(struct timeval a
, struct timeval b
);
115 * \brief Returns the difference of two timevals a - b
117 struct timeval
ast_tvsub(struct timeval a
, struct timeval b
);
120 * \brief Returns a timeval from sec, usec
123 struct timeval
ast_tv(ast_time_t sec
, ast_suseconds_t usec
),
133 * \brief Returns a timeval corresponding to the duration of n samples at rate r.
134 * Useful to convert samples to timevals, or even milliseconds to timevals
135 * in the form ast_samp2tv(milliseconds, 1000)
138 struct timeval
ast_samp2tv(unsigned int _nsamp
, unsigned int _rate
),
140 return ast_tv(_nsamp
/ _rate
, (_nsamp
% _rate
) * (1000000 / _rate
));
144 #endif /* _ASTERISK_TIME_H */