major header file cleanup: license, copyrights, descriptions, markers, etc.
[asterisk/asterisk.git] / include / asterisk / time.h
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
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 /*
20  * Time-related functions and macros
21  */
22
23 #ifndef _ASTERISK_TIME_H
24 #define _ASTERISK_TIME_H
25
26 #include <sys/time.h>
27
28 #include "asterisk/inline_api.h"
29
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;
36
37 /*!
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
42  */
43 AST_INLINE_API(
44 int ast_tvdiff_ms(struct timeval end, struct timeval start),
45 {
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
50         */
51         return  ((end.tv_sec - start.tv_sec) * 1000) +
52                 (((1000000 + end.tv_usec - start.tv_usec) / 1000) - 1000);
53 }
54 )
55
56 /*!
57  * \brief Returns true if the argument is 0,0
58  */
59 AST_INLINE_API(
60 int ast_tvzero(const struct timeval t),
61 {
62         return (t.tv_sec == 0 && t.tv_usec == 0);
63 }
64 )
65
66 /*!
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.
69  */
70 AST_INLINE_API(
71 int ast_tvcmp(struct timeval _a, struct timeval _b),
72 {
73         if (_a.tv_sec < _b.tv_sec)
74                 return -1;
75         if (_a.tv_sec > _b.tv_sec)
76                 return 1;
77         /* now seconds are equal */
78         if (_a.tv_usec < _b.tv_usec)
79                 return -1;
80         if (_a.tv_usec > _b.tv_usec)
81                 return 1;
82         return 0;
83 }
84 )
85
86 /*!
87  * \brief Returns true if the two \c struct \c timeval arguments are equal.
88  */
89 AST_INLINE_API(
90 int ast_tveq(struct timeval _a, struct timeval _b),
91 {
92         return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
93 }
94 )
95
96 /*!
97  * \brief Returns current timeval. Meant to replace calls to gettimeofday().
98  */
99 AST_INLINE_API(
100 struct timeval ast_tvnow(void),
101 {
102         struct timeval t;
103         gettimeofday(&t, NULL);
104         return t;
105 }
106 )
107
108 /*!
109  * \brief Returns the sum of two timevals a + b
110  */
111 struct timeval ast_tvadd(struct timeval a, struct timeval b);
112
113 /*!
114  * \brief Returns the difference of two timevals a - b
115  */
116 struct timeval ast_tvsub(struct timeval a, struct timeval b);
117
118 /*!
119  * \brief Returns a timeval from sec, usec
120  */
121 #if 0
122 AST_INLINE_API(
123 struct timeval ast_tv(int sec, int usec),
124 {
125         struct timeval t = { sec, usec};
126         return t;
127 }
128 )
129 #endif
130 AST_INLINE_API(
131 struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec),
132 {
133         struct timeval t;
134         t.tv_sec = sec;
135         t.tv_usec = usec;
136         return t;
137 }
138 )
139
140 /*!
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)
144  */
145 AST_INLINE_API(
146 struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate),
147 {
148         return ast_tv(_nsamp / _rate, (_nsamp % _rate) * (1000000 / _rate));
149 }
150 )
151
152 #endif /* _ASTERISK_TIME_H */