Wed Jan 8 2020 09:49:51

Asterisk developer's documentation


time.h
Go to the documentation of this file.
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 /*! \file
20  * \brief Time-related functions and macros
21  */
22 
23 #ifndef _ASTERISK_TIME_H
24 #define _ASTERISK_TIME_H
25 
26 #ifdef HAVE_SYS_TIME_H
27 #include <sys/time.h>
28 #endif
29 
30 #include "asterisk/inline_api.h"
31 
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;
38 
39 /*!
40  * \brief Computes the difference (in seconds) between two \c struct \c timeval instances.
41  * \param end the end of the time period
42  * \param start the beginning of the time period
43  * \return the difference in seconds
44  */
46 int64_t ast_tvdiff_sec(struct timeval end, struct timeval start),
47 {
48  int64_t result = end.tv_sec - start.tv_sec;
49  if (result > 0 && end.tv_usec < start.tv_usec)
50  result--;
51  else if (result < 0 && end.tv_usec > start.tv_usec)
52  result++;
53 
54  return result;
55 }
56 )
57 
58 /*!
59  * \brief Computes the difference (in microseconds) between two \c struct \c timeval instances.
60  * \param end the end of the time period
61  * \param start the beginning of the time period
62  * \return the difference in microseconds
63  */
65 int64_t ast_tvdiff_us(struct timeval end, struct timeval start),
66 {
67  return (end.tv_sec - start.tv_sec) * (int64_t) 1000000 +
68  end.tv_usec - start.tv_usec;
69 }
70 )
71 
72 /*!
73  * \brief Computes the difference (in milliseconds) between two \c struct \c timeval instances.
74  * \param end end of the time period
75  * \param start beginning of the time period
76  * \return the difference in milliseconds
77  */
79 int64_t ast_tvdiff_ms(struct timeval end, struct timeval start),
80 {
81  /* the offset by 1,000,000 below is intentional...
82  it avoids differences in the way that division
83  is handled for positive and negative numbers, by ensuring
84  that the divisor is always positive
85  */
86  int64_t sec_dif = (int64_t)(end.tv_sec - start.tv_sec) * 1000;
87  int64_t usec_dif = (1000000 + end.tv_usec - start.tv_usec) / 1000 - 1000;
88  return sec_dif + usec_dif;
89 }
90 )
91 
92 /*!
93  * \brief Returns true if the argument is 0,0
94  */
96 int ast_tvzero(const struct timeval t),
97 {
98  return (t.tv_sec == 0 && t.tv_usec == 0);
99 }
100 )
101 
102 /*!
103  * \brief Compres two \c struct \c timeval instances returning
104  * -1, 0, 1 if the first arg is smaller, equal or greater to the second.
105  */
107 int ast_tvcmp(struct timeval _a, struct timeval _b),
108 {
109  if (_a.tv_sec < _b.tv_sec)
110  return -1;
111  if (_a.tv_sec > _b.tv_sec)
112  return 1;
113  /* now seconds are equal */
114  if (_a.tv_usec < _b.tv_usec)
115  return -1;
116  if (_a.tv_usec > _b.tv_usec)
117  return 1;
118  return 0;
119 }
120 )
121 
122 /*!
123  * \brief Returns true if the two \c struct \c timeval arguments are equal.
124  */
126 int ast_tveq(struct timeval _a, struct timeval _b),
127 {
128  return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
129 }
130 )
131 
132 /*!
133  * \brief Returns current timeval. Meant to replace calls to gettimeofday().
134  */
136 struct timeval ast_tvnow(void),
137 {
138  struct timeval t;
139  gettimeofday(&t, NULL);
140  return t;
141 }
142 )
143 
144 /*!
145  * \brief Returns the sum of two timevals a + b
146  */
147 struct timeval ast_tvadd(struct timeval a, struct timeval b);
148 
149 /*!
150  * \brief Returns the difference of two timevals a - b
151  */
152 struct timeval ast_tvsub(struct timeval a, struct timeval b);
153 
154 /*!
155  * \brief Calculate remaining milliseconds given a starting timestamp
156  * and upper bound
157  *
158  * If the upper bound is negative, then this indicates that there is no
159  * upper bound on the amount of time to wait. This will result in a
160  * negative return.
161  *
162  * \param start When timing started being calculated
163  * \param max_ms The maximum number of milliseconds to wait from start. May be negative.
164  * \return The number of milliseconds left to wait for. May be negative.
165  */
166 int ast_remaining_ms(struct timeval start, int max_ms);
167 
168 /*!
169  * \brief Returns a timeval from sec, usec
170  */
172 struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec),
173 {
174  struct timeval t;
175  t.tv_sec = sec;
176  t.tv_usec = usec;
177  return t;
178 }
179 )
180 
181 /*!
182  * \brief Returns a timeval corresponding to the duration of n samples at rate r.
183  * Useful to convert samples to timevals, or even milliseconds to timevals
184  * in the form ast_samp2tv(milliseconds, 1000)
185  */
187 struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate),
188 {
189  return ast_tv(_nsamp / _rate, ((_nsamp % _rate) * (4000000 / _rate)) / 4); /* this calculation is accurate up to 32000Hz. */
190 }
191 )
192 
193 #endif /* _ASTERISK_TIME_H */
typedef typeof(tv.tv_sec) ast_time_t
int ast_tveq(struct timeval _a, struct timeval _b)
Returns true if the two struct timeval arguments are equal.
Definition: time.h:130
int64_t ast_tvdiff_sec(struct timeval end, struct timeval start)
Computes the difference (in seconds) between two struct timeval instances.
Definition: time.h:56
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:100
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:90
Inlinable API function macro.
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:191
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compres two struct timeval instances returning -1, 0, 1 if the first arg is smaller, equal or greater to the second.
Definition: time.h:120
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
Definition: utils.c:1615
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: utils.c:1587
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:179
struct timeval tv
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: utils.c:1601
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
Definition: time.h:70
#define AST_INLINE_API(hdr, body)
Definition: inline_api.h:49