Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _ASTERISK_TIME_H
00024 #define _ASTERISK_TIME_H
00025
00026 #ifdef HAVE_SYS_TIME_H
00027 #include <sys/time.h>
00028 #endif
00029
00030 #include "asterisk/inline_api.h"
00031
00032
00033
00034
00035 extern struct timeval tv;
00036 typedef typeof(tv.tv_sec) ast_time_t;
00037 typedef typeof(tv.tv_usec) ast_suseconds_t;
00038
00039
00040
00041
00042
00043
00044
00045 AST_INLINE_API(
00046 int ast_tvdiff_sec(struct timeval end, struct timeval start),
00047 {
00048 int result = end.tv_sec - start.tv_sec;
00049 if (result > 0 && end.tv_usec < start.tv_usec)
00050 result--;
00051 else if (result < 0 && end.tv_usec > start.tv_usec)
00052 result++;
00053
00054 return result;
00055 }
00056 )
00057
00058
00059
00060
00061
00062
00063
00064 AST_INLINE_API(
00065 int64_t ast_tvdiff_us(struct timeval end, struct timeval start),
00066 {
00067 return (end.tv_sec - start.tv_sec) * (int64_t) 1000000 +
00068 end.tv_usec - start.tv_usec;
00069 }
00070 )
00071
00072
00073
00074
00075
00076
00077
00078 AST_INLINE_API(
00079 int ast_tvdiff_ms(struct timeval end, struct timeval start),
00080 {
00081
00082
00083
00084
00085
00086 return ((end.tv_sec - start.tv_sec) * 1000) +
00087 (((1000000 + end.tv_usec - start.tv_usec) / 1000) - 1000);
00088 }
00089 )
00090
00091
00092
00093
00094 AST_INLINE_API(
00095 int ast_tvzero(const struct timeval t),
00096 {
00097 return (t.tv_sec == 0 && t.tv_usec == 0);
00098 }
00099 )
00100
00101
00102
00103
00104
00105 AST_INLINE_API(
00106 int ast_tvcmp(struct timeval _a, struct timeval _b),
00107 {
00108 if (_a.tv_sec < _b.tv_sec)
00109 return -1;
00110 if (_a.tv_sec > _b.tv_sec)
00111 return 1;
00112
00113 if (_a.tv_usec < _b.tv_usec)
00114 return -1;
00115 if (_a.tv_usec > _b.tv_usec)
00116 return 1;
00117 return 0;
00118 }
00119 )
00120
00121
00122
00123
00124 AST_INLINE_API(
00125 int ast_tveq(struct timeval _a, struct timeval _b),
00126 {
00127 return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
00128 }
00129 )
00130
00131
00132
00133
00134 AST_INLINE_API(
00135 struct timeval ast_tvnow(void),
00136 {
00137 struct timeval t;
00138 gettimeofday(&t, NULL);
00139 return t;
00140 }
00141 )
00142
00143
00144
00145
00146 struct timeval ast_tvadd(struct timeval a, struct timeval b);
00147
00148
00149
00150
00151 struct timeval ast_tvsub(struct timeval a, struct timeval b);
00152
00153
00154
00155
00156 AST_INLINE_API(
00157 struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec),
00158 {
00159 struct timeval t;
00160 t.tv_sec = sec;
00161 t.tv_usec = usec;
00162 return t;
00163 }
00164 )
00165
00166
00167
00168
00169
00170
00171 AST_INLINE_API(
00172 struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate),
00173 {
00174 return ast_tv(_nsamp / _rate, ((_nsamp % _rate) * (4000000 / _rate)) / 4);
00175 }
00176 )
00177
00178 #endif