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 #include <sys/time.h>
00027 #include <stdlib.h>
00028
00029 #include "asterisk/inline_api.h"
00030
00031
00032
00033
00034 extern struct timeval tv;
00035 typedef typeof(tv.tv_sec) ast_time_t;
00036 typedef typeof(tv.tv_usec) ast_suseconds_t;
00037
00038
00039
00040
00041
00042
00043
00044 AST_INLINE_API(
00045 int ast_tvdiff_ms(struct timeval end, struct timeval start),
00046 {
00047
00048
00049
00050
00051
00052 return ((end.tv_sec - start.tv_sec) * 1000) +
00053 (((1000000 + end.tv_usec - start.tv_usec) / 1000) - 1000);
00054 }
00055 )
00056
00057
00058
00059
00060 AST_INLINE_API(
00061 int ast_tvzero(const struct timeval t),
00062 {
00063 return (t.tv_sec == 0 && t.tv_usec == 0);
00064 }
00065 )
00066
00067
00068
00069
00070
00071 AST_INLINE_API(
00072 int ast_tvcmp(struct timeval _a, struct timeval _b),
00073 {
00074 if (_a.tv_sec < _b.tv_sec)
00075 return -1;
00076 if (_a.tv_sec > _b.tv_sec)
00077 return 1;
00078
00079 if (_a.tv_usec < _b.tv_usec)
00080 return -1;
00081 if (_a.tv_usec > _b.tv_usec)
00082 return 1;
00083 return 0;
00084 }
00085 )
00086
00087
00088
00089
00090 AST_INLINE_API(
00091 int ast_tveq(struct timeval _a, struct timeval _b),
00092 {
00093 return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
00094 }
00095 )
00096
00097
00098
00099
00100 AST_INLINE_API(
00101 struct timeval ast_tvnow(void),
00102 {
00103 struct timeval t;
00104 gettimeofday(&t, NULL);
00105 return t;
00106 }
00107 )
00108
00109
00110
00111
00112 struct timeval ast_tvadd(struct timeval a, struct timeval b);
00113
00114
00115
00116
00117 struct timeval ast_tvsub(struct timeval a, struct timeval b);
00118
00119
00120
00121
00122 AST_INLINE_API(
00123 struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec),
00124 {
00125 struct timeval t;
00126 t.tv_sec = sec;
00127 t.tv_usec = usec;
00128 return t;
00129 }
00130 )
00131
00132
00133
00134
00135
00136
00137 AST_INLINE_API(
00138 struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate),
00139 {
00140 return ast_tv(_nsamp / _rate, ((_nsamp % _rate) * (4000000 / _rate)) / 4);
00141 }
00142 )
00143
00144 #endif