23 #ifndef _ASTERISK_TIME_H 24 #define _ASTERISK_TIME_H 28 #ifdef HAVE_SYS_TIME_H 56 int64_t
result = end.tv_sec - start.tv_sec;
57 if (result > 0 && end.tv_usec < start.tv_usec)
59 else if (result < 0 && end.tv_usec > start.tv_usec)
73 int64_t
ast_tvdiff_us(
struct timeval end,
struct timeval start),
75 return (end.tv_sec - start.tv_sec) * (int64_t) 1000000 +
76 end.tv_usec - start.tv_usec;
87 int64_t
ast_tvdiff_ms(
struct timeval end,
struct timeval start),
94 int64_t sec_dif = (int64_t)(end.tv_sec - start.tv_sec) * 1000;
95 int64_t usec_dif = (1000000 + end.tv_usec - start.tv_usec) / 1000 - 1000;
96 return sec_dif + usec_dif;
106 return (t.tv_sec == 0 && t.tv_usec == 0);
115 int ast_tvcmp(
struct timeval _a,
struct timeval _b),
117 if (_a.tv_sec < _b.tv_sec)
119 if (_a.tv_sec > _b.tv_sec)
122 if (_a.tv_usec < _b.tv_usec)
124 if (_a.tv_usec > _b.tv_usec)
134 int ast_tveq(
struct timeval _a,
struct timeval _b),
136 return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
147 gettimeofday(&t,
NULL);
156 #
if defined _POSIX_TIMERS && _POSIX_TIMERS > 0
161 clock_gettime(CLOCK_REALTIME, &ts);
173 ts.tv_sec = tv.tv_sec;
174 ts.tv_nsec = tv.tv_usec * 1000;
183 struct timeval
ast_tvadd(
struct timeval
a,
struct timeval
b);
188 struct timeval
ast_tvsub(struct timeval
a, struct timeval
b);
219 struct timeval
ast_tv(ast_time_t sec, ast_suseconds_t usec),
234 struct timeval
ast_samp2tv(
unsigned int _nsamp,
unsigned int _rate),
236 return ast_tv(_nsamp / _rate, (_nsamp % _rate) * (1000000 / (
float) _rate));
typedef typeof(dummy_tv_var_for_types.tv_sec) ast_time_t
ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv)
Convert a timeval structure to microseconds.
int ast_tveq(struct timeval _a, struct timeval _b)
Returns true if the two struct timeval arguments are equal.
int64_t ast_tvdiff_sec(struct timeval end, struct timeval start)
Computes the difference (in seconds) between two struct timeval instances.
TIME_UNIT
Time units enumeration.
struct timeval ast_time_create_by_unit_str(unsigned long val, const char *unit)
Convert the given unit value, and create a timeval object from it.
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
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 ...
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.
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
struct timeval ast_time_create(ast_time_t sec, ast_suseconds_t usec)
Create a timeval object initialized to given values.
struct timeval dummy_tv_var_for_types
enum TIME_UNIT ast_time_str_to_unit(const char *unit)
Convert a string to a time unit enumeration value.
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
struct timespec ast_tsnow(void)
Returns current timespec. Meant to avoid calling ast_tvnow() just to create a timespec from the timev...
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
void ast_format_duration_hh_mm_ss(int duration, char *buf, size_t length)
Formats a duration into HH:MM:SS.
struct timeval ast_time_create_by_unit(unsigned long val, enum TIME_UNIT unit)
Convert the given unit value, and create a timeval object from it.
#define AST_INLINE_API(hdr, body)