23#ifndef _ASTERISK_TIME_H
24#define _ASTERISK_TIME_H
45#define AST_TIME_T_LEN 21
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)
84 return (
end.tv_sec - start.tv_sec) * (int64_t) 1000000 +
85 end.tv_usec - start.tv_usec;
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);
124int 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)
143int 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;
192struct timeval
ast_tvadd(
struct timeval
a,
struct timeval
b);
197struct timeval
ast_tvsub(struct timeval
a, struct timeval
b);
228struct 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));
278struct timeval
ast_samp2tv(
unsigned int _nsamp,
unsigned int _rate),
280 return ast_tv(_nsamp / _rate, (_nsamp % _rate) * (1000000 / (
float) _rate));
295time_t
ast_tv2samp(
const struct timeval *_tv,
int _rate),
312double ast_samp2sec(
unsigned int _nsamp,
unsigned int _rate),
314 return ((
double)_nsamp) / ((
double)_rate);
331 return (
unsigned int)(_seconds * _rate);
Inlinable API function macro.
#define AST_INLINE_API(hdr, body)
enum TIME_UNIT ast_time_str_to_unit(const char *unit)
Convert a string to a time unit enumeration value.
typedef typeof(dummy_tv_var_for_types.tv_sec) ast_time_t
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
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....
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 ...
struct timespec ast_tsnow(void)
Returns current timespec. Meant to avoid calling ast_tvnow() just to create a timespec from the timev...
int64_t ast_tvdiff_sec(struct timeval end, struct timeval start)
Computes the difference (in seconds) between two struct timeval instances.
int ast_tveq(struct timeval _a, struct timeval _b)
Returns true if the two struct timeval arguments are equal.
time_t ast_string_to_time_t(const char *str)
Returns a time_t from a string containing seconds since the epoch.
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
TIME_UNIT
Time units enumeration.
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(ast_time_t sec, ast_suseconds_t usec)
Create a timeval object initialized to given values.
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compress two struct timeval instances returning -1, 0, 1 if the first arg is smaller,...
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.
double ast_samp2sec(unsigned int _nsamp, unsigned int _rate)
Returns the duration in seconds of _nsamp samples at rate _rate.
int ast_remaining_ms(struct timeval start, int max_ms)
Calculate remaining milliseconds given a starting timestamp and upper bound.
unsigned int ast_sec2samp(double _seconds, int _rate)
Returns the number of samples at _rate in the duration in _seconds.
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
struct timeval dummy_tv_var_for_types
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.
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
double ast_tv2double(const struct timeval *tv)
Returns a double corresponding to the number of seconds in the timeval tv.
ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv)
Convert a timeval structure to microseconds.
struct timeval ast_double2tv(double _td)
Returns a timeval structure corresponding to the number of seconds in the double _td.
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
time_t ast_tv2samp(const struct timeval *_tv, int _rate)
Returns the number of samples at rate _rate in the duration specified by _tv.