Asterisk - The Open Source Telephony Project
21.4.1
|
Time-related functions and macros. More...
#include "asterisk/autoconfig.h"
#include <sys/time.h>
#include <unistd.h>
#include <math.h>
#include "asterisk/inline_api.h"
Go to the source code of this file.
Macros | |
#define | AST_TIME_T_LEN 21 |
Enumerations | |
enum | TIME_UNIT { TIME_UNIT_ERROR = -1, TIME_UNIT_NANOSECOND, TIME_UNIT_MICROSECOND, TIME_UNIT_MILLISECOND, TIME_UNIT_SECOND, TIME_UNIT_MINUTE, TIME_UNIT_HOUR, TIME_UNIT_DAY, TIME_UNIT_WEEK, TIME_UNIT_MONTH, TIME_UNIT_YEAR } |
Time units enumeration. | |
Functions | |
struct timeval | ast_double2tv (double _td) |
Returns a timeval structure corresponding to the number of seconds in the double _td. More... | |
void | ast_format_duration_hh_mm_ss (int duration, char *buf, size_t length) |
Formats a duration into HH:MM:SS. More... | |
int | ast_remaining_ms (struct timeval start, int max_ms) |
Calculate remaining milliseconds given a starting timestamp and upper bound. More... | |
double | ast_samp2sec (unsigned int _nsamp, unsigned int _rate) |
Returns the duration in seconds of _nsamp samples at rate _rate. More... | |
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 timevals, or even milliseconds to timevals in the form ast_samp2tv(milliseconds, 1000) | |
unsigned int | ast_sec2samp (double _seconds, int _rate) |
Returns the number of samples at _rate in the duration in _seconds. More... | |
time_t | ast_string_to_time_t (const char *str) |
Returns a time_t from a string containing seconds since the epoch. | |
struct timeval | ast_time_create (ast_time_t sec, ast_suseconds_t usec) |
Create a timeval object initialized to given values. More... | |
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. More... | |
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. More... | |
enum TIME_UNIT | ast_time_str_to_unit (const char *unit) |
Convert a string to a time unit enumeration value. More... | |
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. More... | |
ast_suseconds_t | ast_time_tv_to_usec (const struct timeval *tv) |
Convert a timeval structure to microseconds. More... | |
struct timespec | ast_tsnow (void) |
Returns current timespec. Meant to avoid calling ast_tvnow() just to create a timespec from the timeval it returns. | |
struct timeval | ast_tv (ast_time_t sec, ast_suseconds_t usec) |
Returns a timeval from sec, usec. | |
double | ast_tv2double (const struct timeval *tv) |
Returns a double corresponding to the number of seconds in the timeval tv . More... | |
time_t | ast_tv2samp (const struct timeval *_tv, int _rate) |
Returns the number of samples at rate _rate in the duration specified by _tv. More... | |
struct timeval | ast_tvadd (struct timeval a, struct timeval b) |
Returns the sum of two timevals a + b. | |
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. | |
int64_t | ast_tvdiff_ms (struct timeval end, struct timeval start) |
Computes the difference (in milliseconds) between two struct timeval instances. More... | |
int64_t | ast_tvdiff_sec (struct timeval end, struct timeval start) |
Computes the difference (in seconds) between two struct timeval instances. More... | |
int64_t | ast_tvdiff_us (struct timeval end, struct timeval start) |
Computes the difference (in microseconds) between two struct timeval instances. More... | |
int | ast_tveq (struct timeval _a, struct timeval _b) |
Returns true if the two struct timeval arguments are equal. | |
struct timeval | ast_tvnow (void) |
Returns current timeval. Meant to replace calls to gettimeofday(). | |
struct timeval | ast_tvsub (struct timeval a, struct timeval b) |
Returns the difference of two timevals a - b. | |
int | ast_tvzero (const struct timeval t) |
Returns true if the argument is 0,0. | |
typedef | typeof (dummy_tv_var_for_types.tv_sec) ast_time_t |
Variables | |
struct timeval | dummy_tv_var_for_types |
Time-related functions and macros.
Definition in file time.h.
struct timeval ast_double2tv | ( | double | _td | ) |
Returns a timeval structure corresponding to the number of seconds in the double _td.
_td | The number of seconds. |
This is the inverse of ast_tv2double().
void ast_format_duration_hh_mm_ss | ( | int | duration, |
char * | buf, | ||
size_t | length | ||
) |
Formats a duration into HH:MM:SS.
duration | The time (in seconds) to format |
buf | A buffer to hold the formatted string' |
length | The size of the buffer |
int ast_remaining_ms | ( | struct timeval | start, |
int | max_ms | ||
) |
Calculate remaining milliseconds given a starting timestamp and upper bound.
If the upper bound is negative then this indicates that there is no upper bound on the amount of time to wait. This will result in a negative return.
start | When timing started being calculated |
max_ms | The maximum number of milliseconds to wait from start. May be negative. |
Definition at line 2281 of file utils.c.
References ast_tvdiff_ms(), and ast_tvnow().
Referenced by __ast_answer(), __ast_request_and_dial(), ast_iostream_write(), ast_recvtext(), ast_stun_request(), ast_waitfordigit_full(), generic_fax_exec(), parking_set_duration(), read_mf_digits(), read_sf_digits(), safe_sleep_conditional(), and wait_for_answer().
|
inline |
Returns the duration in seconds of _nsamp samples at rate _rate.
_nsamp | The number of samples |
_rate | The sample rate in Hz. |
This is the inverse of ast_sec2samp().
Definition at line 316 of file time.h.
Referenced by ast_rtcp_interpret().
|
inline |
Returns the number of samples at _rate in the duration in _seconds.
_seconds | The time interval in seconds. |
_rate | The sample rate in Hz. |
This is the inverse of ast_samp2sec().
struct timeval ast_time_create | ( | ast_time_t | sec, |
ast_suseconds_t | usec | ||
) |
Create a timeval object initialized to given values.
sec | The timeval seconds value |
usec | The timeval microseconds value |
Definition at line 95 of file time.c.
References ast_tv().
Referenced by ast_time_create_by_unit(), and normalize_and_create().
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.
val | The value to convert to a timeval |
unit | The time unit type of val |
Definition at line 113 of file time.c.
References ast_time_create(), and normalize_and_create().
Referenced by ast_time_create_by_unit_str().
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.
This will first attempt to convert the unit from a string to a TIME_UNIT enumeration. If that conversion fails then a zeroed out timeval object is returned.
val | The value to convert to a timeval |
unit | The time unit type of val |
Definition at line 143 of file time.c.
References ast_time_create_by_unit(), and ast_time_str_to_unit().
enum TIME_UNIT ast_time_str_to_unit | ( | const char * | unit | ) |
Convert a string to a time unit enumeration value.
This method attempts to be as flexible, and forgiving as possible when converting. In most cases the algorithm will match on the beginning of up to three strings (short, medium, long form). So that means if the given string at least starts with one of the form values it will match.
For example: us, usec, microsecond will all map to TIME_UNIT_MICROSECOND. So will uss, usecs, microseconds, or even microsecondvals
Matching is also not case sensitive.
unit | The string to map to an enumeration |
Definition at line 66 of file time.c.
Referenced by ast_time_create_by_unit_str().
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.
The buffer should be at least 22 bytes long.
Converts to a string representation of a time_t as decimal seconds since the epoch. Returns -1 on failure, zero otherwise.
Definition at line 152 of file time.c.
Referenced by display_single_entry(), and sprint_list_entry().
ast_suseconds_t ast_time_tv_to_usec | ( | const struct timeval * | tv | ) |
|
inline |
Returns a double corresponding to the number of seconds in the timeval tv
.
tv | A pointer to a timeval structure. |
This is the inverse of ast_double2tv().
Definition at line 270 of file time.h.
References ast_tv().
Referenced by ast_samp2tv().
|
inline |
Returns the number of samples at rate _rate in the duration specified by _tv.
_tv | A pointer to a timeval structure. |
_rate | The sample rate in Hz. |
This is the inverse of ast_samp2tv().
|
inline |
Computes the difference (in milliseconds) between two struct
timeval
instances.
end | end of the time period |
start | beginning of the time period |
Definition at line 107 of file time.h.
Referenced by __analog_handle_event(), __ast_read(), action_coreshowchannels(), ast_audiohook_write_frame(), ast_carefulwrite(), ast_cdr_format_var(), ast_channel_cmpwhentohangup_tv(), ast_channel_end_dtmf(), ast_channel_get_duration_ms(), ast_channel_get_up_time_ms(), ast_check_hangup(), ast_get_enum(), ast_odbc_direct_execute(), ast_odbc_prepare_and_execute(), ast_remaining_ms(), ast_rtp_read(), ast_sched_wait(), ast_sip_sched_task_get_times2(), ast_waitfor_nandfds(), cdr_object_finalize(), cdr_object_get_billsec(), cdr_object_get_duration(), check_timer(), fax_detect_framehook(), fax_gateway_framehook(), handle_cli_file_convert(), handle_timeout_trip(), receive_ademco_event(), receive_dtmf_digits(), set_interval_hook(), should_skip_dtmf(), should_trigger_dtmf_emulating(), sla_calc_station_timeouts(), sla_calc_trunk_timeouts(), sla_check_failed_station(), sla_check_station_delay(), softmix_mixing_loop(), speech_background(), subscription_invoke(), and wait_for_answer().
|
inline |
Computes the difference (in seconds) between two struct
timeval
instances.
end | the end of the time period |
start | the beginning of the time period |
Definition at line 73 of file time.h.
Referenced by purge_events().
|
inline |
Computes the difference (in microseconds) between two struct
timeval
instances.
end | the end of the time period |
start | the beginning of the time period |
Definition at line 87 of file time.h.
Referenced by ast_media_index_update_for_file(), ast_merge_contexts_and_delete(), and load_modules().