Adds DAHDI support alongside Zaptel. DAHDI usage favored, but all Zap stuff should...
[asterisk-bristuff.git] / include / asterisk / time.h
blob92a5346a5a8bfaf1e552080e02b7e7896c871e9a
1 /*
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.
19 /*! \file
20 * \brief Time-related functions and macros
23 #ifndef _ASTERISK_TIME_H
24 #define _ASTERISK_TIME_H
26 #include <sys/time.h>
27 #include <stdlib.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;
38 /*!
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
44 AST_INLINE_API(
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);
57 /*!
58 * \brief Returns true if the argument is 0,0
60 AST_INLINE_API(
61 int ast_tvzero(const struct timeval t),
63 return (t.tv_sec == 0 && t.tv_usec == 0);
67 /*!
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.
71 AST_INLINE_API(
72 int ast_tvcmp(struct timeval _a, struct timeval _b),
74 if (_a.tv_sec < _b.tv_sec)
75 return -1;
76 if (_a.tv_sec > _b.tv_sec)
77 return 1;
78 /* now seconds are equal */
79 if (_a.tv_usec < _b.tv_usec)
80 return -1;
81 if (_a.tv_usec > _b.tv_usec)
82 return 1;
83 return 0;
87 /*!
88 * \brief Returns true if the two \c struct \c timeval arguments are equal.
90 AST_INLINE_API(
91 int ast_tveq(struct timeval _a, struct timeval _b),
93 return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
97 /*!
98 * \brief Returns current timeval. Meant to replace calls to gettimeofday().
100 AST_INLINE_API(
101 struct timeval ast_tvnow(void),
103 struct timeval t;
104 gettimeofday(&t, NULL);
105 return t;
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
122 AST_INLINE_API(
123 struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec),
125 struct timeval t;
126 t.tv_sec = sec;
127 t.tv_usec = usec;
128 return t;
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)
137 AST_INLINE_API(
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 */