23 #ifndef _ASTERISK_TIME_H
24 #define _ASTERISK_TIME_H
26 #include "asterisk/autoconfig.h"
28 #ifdef HAVE_SYS_TIME_H
45 #define AST_TIME_T_LEN 21
52 extern struct timeval dummy_tv_var_for_types;
53 typedef typeof(dummy_tv_var_for_types.tv_sec) ast_time_t;
54 typedef typeof(dummy_tv_var_for_types.tv_usec) ast_suseconds_t;
65 int64_t result = end.tv_sec - start.tv_sec;
66 if (result > 0 && end.tv_usec < start.tv_usec)
68 else if (result < 0 && end.tv_usec > start.tv_usec)
82 int64_t
ast_tvdiff_us(
struct timeval end,
struct timeval start),
84 return (end.tv_sec - start.tv_sec) * (int64_t) 1000000 +
85 end.tv_usec - start.tv_usec;
96 int64_t
ast_tvdiff_ms(
struct timeval end,
struct timeval start),
103 int64_t sec_dif = (int64_t)(end.tv_sec - start.tv_sec) * 1000;
104 int64_t usec_dif = (1000000 + end.tv_usec - start.tv_usec) / 1000 - 1000;
105 return sec_dif + usec_dif;
115 return (t.tv_sec == 0 && t.tv_usec == 0);
124 int ast_tvcmp(
struct timeval _a,
struct timeval _b),
126 if (_a.tv_sec < _b.tv_sec)
128 if (_a.tv_sec > _b.tv_sec)
131 if (_a.tv_usec < _b.tv_usec)
133 if (_a.tv_usec > _b.tv_usec)
143 int ast_tveq(
struct timeval _a,
struct timeval _b),
145 return (_a.tv_sec == _b.tv_sec && _a.tv_usec == _b.tv_usec);
156 gettimeofday(&t, NULL);
165 #
if defined _POSIX_TIMERS && _POSIX_TIMERS > 0
170 clock_gettime(CLOCK_REALTIME, &ts);
182 ts.tv_sec = tv.tv_sec;
183 ts.tv_nsec = tv.tv_usec * 1000;
192 struct timeval
ast_tvadd(
struct timeval a,
struct timeval b);
197 struct timeval
ast_tvsub(struct timeval a, struct timeval b);
228 struct timeval
ast_tv(ast_time_t sec, ast_suseconds_t usec),
250 t.tv_sec = (typeof(t.tv_sec))floor(_td);
251 t.tv_usec = (typeof(t.tv_usec)) ((_td - t.tv_sec) * 1000000.0);
268 return (((
double)tv->tv_sec) + (((double)tv->tv_usec) / 1000000.0));
278 struct timeval
ast_samp2tv(
unsigned int _nsamp,
unsigned int _rate),
280 return ast_tv(_nsamp / _rate, (_nsamp % _rate) * (1000000 / (
float) _rate));
295 time_t
ast_tv2samp(
const struct timeval *_tv,
int _rate),
312 double ast_samp2sec(
unsigned int _nsamp,
unsigned int _rate),
314 return ((
double)_nsamp) / ((
double)_rate);
331 return (
unsigned int)(_seconds * _rate);
339 TIME_UNIT_ERROR = -1,
340 TIME_UNIT_NANOSECOND,
341 TIME_UNIT_MICROSECOND,
342 TIME_UNIT_MILLISECOND,
time_t ast_string_to_time_t(const char *str)
Returns a time_t from a string containing seconds since the epoch.
ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv)
Convert a timeval structure to microseconds.
unsigned int ast_sec2samp(double _seconds, int _rate)
Returns the number of samples at _rate in the duration in _seconds.
int ast_time_t_to_string(time_t time, char *buf, size_t length)
Converts to a string representation of a time_t as decimal seconds since the epoch. Returns -1 on failure, zero otherwise.
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 ...
double ast_samp2sec(unsigned int _nsamp, unsigned int _rate)
Returns the duration in seconds of _nsamp samples at rate _rate.
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compress two struct timeval instances returning -1, 0, 1 if the first arg is smaller, equal or greater to the second.
struct timeval ast_double2tv(double _td)
Returns a timeval structure corresponding to the number of seconds in the double _td.
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
time_t ast_tv2samp(const struct timeval *_tv, int _rate)
Returns the number of samples at rate _rate in the duration specified by _tv.
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.
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...
double ast_tv2double(const struct timeval *tv)
Returns a double corresponding to the number of seconds in the timeval tv.
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.