Asterisk - The Open Source Telephony Project  21.4.1
Macros | Enumerations | Functions | Variables
time.h File Reference

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
 

Detailed Description

Time-related functions and macros.

Definition in file time.h.

Function Documentation

struct timeval ast_double2tv ( double  _td)

Returns a timeval structure corresponding to the number of seconds in the double _td.

Parameters
_tdThe number of seconds.
Returns
A timeval structure containing the number of seconds.

This is the inverse of ast_tv2double().

Definition at line 254 of file time.h.

267 {
void ast_format_duration_hh_mm_ss ( int  duration,
char *  buf,
size_t  length 
)

Formats a duration into HH:MM:SS.

Since
12
Parameters
durationThe time (in seconds) to format
bufA buffer to hold the formatted string'
lengthThe size of the buffer

Definition at line 2297 of file utils.c.

2298 {
2299  int durh, durm, durs;
2300  durh = duration / 3600;
2301  durm = (duration % 3600) / 60;
2302  durs = duration % 60;
2303  snprintf(buf, length, "%02d:%02d:%02d", durh, durm, durs);
2304 }
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.

Parameters
startWhen timing started being calculated
max_msThe maximum number of milliseconds to wait from start. May be negative.
Returns
The number of milliseconds left to wait for. 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().

2282 {
2283  int ms;
2284 
2285  if (max_ms < 0) {
2286  ms = max_ms;
2287  } else {
2288  ms = max_ms - ast_tvdiff_ms(ast_tvnow(), start);
2289  if (ms < 0) {
2290  ms = 0;
2291  }
2292  }
2293 
2294  return ms;
2295 }
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
double ast_samp2sec ( unsigned int  _nsamp,
unsigned int  _rate 
)
inline

Returns the duration in seconds of _nsamp samples at rate _rate.

Parameters
_nsampThe number of samples
_rateThe sample rate in Hz.
Returns
A double containing the number of seconds.

This is the inverse of ast_sec2samp().

Definition at line 316 of file time.h.

Referenced by ast_rtcp_interpret().

330 {
unsigned int ast_sec2samp ( double  _seconds,
int  _rate 
)
inline

Returns the number of samples at _rate in the duration in _seconds.

Parameters
_secondsThe time interval in seconds.
_rateThe sample rate in Hz.
Returns
The number of samples.

This is the inverse of ast_samp2sec().

Definition at line 333 of file time.h.

338 {
struct timeval ast_time_create ( ast_time_t  sec,
ast_suseconds_t  usec 
)

Create a timeval object initialized to given values.

Parameters
secThe timeval seconds value
usecThe timeval microseconds value
Returns
A timeval object

Definition at line 95 of file time.c.

References ast_tv().

Referenced by ast_time_create_by_unit(), and normalize_and_create().

96 {
97  return ast_tv(sec, usec);
98 }
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:235
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.

Parameters
valThe value to convert to a timeval
unitThe time unit type of val
Returns
A timeval object

Definition at line 113 of file time.c.

References ast_time_create(), and normalize_and_create().

Referenced by ast_time_create_by_unit_str().

114 {
115  switch (unit) {
116  case TIME_UNIT_NANOSECOND:
117  return normalize_and_create(val / 1000);
118  case TIME_UNIT_MICROSECOND:
119  return normalize_and_create(val);
120  case TIME_UNIT_MILLISECOND:
121  return normalize_and_create(val * 1000);
122  case TIME_UNIT_SECOND:
123  return ast_time_create(val, 0);
124  case TIME_UNIT_MINUTE:
125  return ast_time_create(val * 60, 0);
126  case TIME_UNIT_HOUR:
127  return ast_time_create(val * 3600, 0);
128  case TIME_UNIT_DAY:
129  return ast_time_create(val * 86400, 0);
130  case TIME_UNIT_WEEK:
131  return ast_time_create(val * 604800, 0);
132  case TIME_UNIT_MONTH:
133  /* Using Gregorian mean month - 30.436875 * 86400 */
134  return ast_time_create(val * 2629746, 0);
135  case TIME_UNIT_YEAR:
136  /* Using Gregorian year - 365.2425 * 86400 */
137  return ast_time_create(val * 31556952, 0);
138  default:
139  return ast_time_create(0, 0);
140  }
141 }
static struct timeval normalize_and_create(unsigned long usec)
Create a timeval first converting the given microsecond value into seconds and microseconds.
Definition: time.c:108
struct timeval ast_time_create(ast_time_t sec, ast_suseconds_t usec)
Create a timeval object initialized to given values.
Definition: time.c:95
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.

Parameters
valThe value to convert to a timeval
unitThe time unit type of val
Returns
A timeval object

Definition at line 143 of file time.c.

References ast_time_create_by_unit(), and ast_time_str_to_unit().

144 {
146 }
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.
Definition: time.c:113
enum TIME_UNIT ast_time_str_to_unit(const char *unit)
Convert a string to a time unit enumeration value.
Definition: time.c:66
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.

Parameters
unitThe string to map to an enumeration
Returns
A time unit enumeration

Definition at line 66 of file time.c.

Referenced by ast_time_create_by_unit_str().

67 {
68  size_t i, j;
69 
70  if (!unit) {
71  return TIME_UNIT_ERROR;
72  }
73 
74  for (i = 0; i < unit_labels_size; ++i) {
75  for (j = 0; j < MAX_UNIT_LABELS; ++j) {
76  /*
77  * A lazy pluralization check. If the given unit string at least starts
78  * with a label assume a match.
79  */
80  if (*unit_labels[i].values[j] && !strncasecmp(unit, unit_labels[i].values[j],
81  strlen(unit_labels[i].values[j]))) {
82  return unit_labels[i].unit;
83  }
84  }
85  }
86 
87  return TIME_UNIT_ERROR;
88 }
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().

153 {
154  struct tm tm;
155 
156  localtime_r(&time, &tm);
157  return (strftime(buf, length, "%s", &tm) == 0) ? -1 : 0;
158 }
ast_suseconds_t ast_time_tv_to_usec ( const struct timeval *  tv)

Convert a timeval structure to microseconds.

Parameters
tvThe timeval to convert
Returns
The time in microseconds

Definition at line 90 of file time.c.

91 {
92  return tv->tv_sec * 1000000 + tv->tv_usec;
93 }
double ast_tv2double ( const struct timeval *  tv)
inline

Returns a double corresponding to the number of seconds in the timeval tv.

Parameters
tvA pointer to a timeval structure.
Returns
A double containing the number of seconds.

This is the inverse of ast_double2tv().

Definition at line 270 of file time.h.

References ast_tv().

Referenced by ast_samp2tv().

279 {
time_t ast_tv2samp ( const struct timeval *  _tv,
int  _rate 
)
inline

Returns the number of samples at rate _rate in the duration specified by _tv.

Parameters
_tvA pointer to a timeval structure.
_rateThe sample rate in Hz.
Returns
A time_t containing the number of samples.

This is the inverse of ast_samp2tv().

Definition at line 299 of file time.h.

313 {
int64_t ast_tvdiff_ms ( struct timeval  end,
struct timeval  start 
)
inline
int64_t ast_tvdiff_sec ( struct timeval  end,
struct timeval  start 
)
inline

Computes the difference (in seconds) between two struct timeval instances.

Parameters
endthe end of the time period
startthe beginning of the time period
Returns
the difference in seconds

Definition at line 73 of file time.h.

Referenced by purge_events().

83 {
int64_t ast_tvdiff_us ( struct timeval  end,
struct timeval  start 
)
inline

Computes the difference (in microseconds) between two struct timeval instances.

Parameters
endthe end of the time period
startthe beginning of the time period
Returns
the difference in microseconds

Definition at line 87 of file time.h.

Referenced by ast_media_index_update_for_file(), ast_merge_contexts_and_delete(), and load_modules().

97 {