Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
time.c File Reference

Date/Time utility functions. More...

#include <inttypes.h>
#include <string.h>
#include <time.h>
Include dependency graph for time.c:

Go to the source code of this file.

Data Structures

struct  time_unit_labels
 

Macros

#define MAX_UNIT_LABELS   3
 

Functions

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...
 
ast_suseconds_t ast_time_tv_to_usec (const struct timeval *tv)
 Convert a timeval structure to microseconds. More...
 
static struct timeval normalize_and_create (unsigned long usec)
 Create a timeval first onverting the given microsecond value into seconds and microseconds. More...
 

Variables

const char * day_labels [] = {"d", "", "day"}
 
const char * hour_labels [] = {"h", "hr", "hour"}
 
const char * microsecond_labels [] = {"us", "usec", "microsecond"}
 
const char * millisecond_labels [] = {"ms", "msec", "millisecond"}
 
const char * minute_labels [] = {"m", "min", "minute"}
 
const char * month_labels [] = {"mo", "mth", "month"}
 
const char * nanosecond_labels [] = {"ns", "nsec", "nanosecond"}
 
const char * second_labels [] = {"s", "sec", "second"}
 
static struct time_unit_labels unit_labels []
 
const unsigned int unit_labels_size = sizeof(unit_labels) / sizeof(0[unit_labels])
 
const char * week_labels [] = {"w", "wk", "week"}
 
const char * year_labels [] = {"y", "yr", "year"}
 

Detailed Description

Date/Time utility functions.

Definition in file time.c.

Macro Definition Documentation

◆ MAX_UNIT_LABELS

#define MAX_UNIT_LABELS   3

Definition at line 43 of file time.c.

Referenced by ast_time_str_to_unit().

Function Documentation

◆ ast_time_create()

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 94 of file time.c.

References ast_tv().

Referenced by AST_TEST_DEFINE(), ast_time_create_by_unit(), and normalize_and_create().

95 {
96  return ast_tv(sec, usec);
97 }
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:226

◆ ast_time_create_by_unit()

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 112 of file time.c.

References ast_time_create(), normalize_and_create(), TIME_UNIT_DAY, TIME_UNIT_HOUR, TIME_UNIT_MICROSECOND, TIME_UNIT_MILLISECOND, TIME_UNIT_MINUTE, TIME_UNIT_MONTH, TIME_UNIT_NANOSECOND, TIME_UNIT_SECOND, TIME_UNIT_WEEK, TIME_UNIT_YEAR, and time_unit_labels::unit.

Referenced by AST_TEST_DEFINE(), ast_time_create_by_unit_str(), and drop_packets_data_update().

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

◆ 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.

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

Definition at line 142 of file time.c.

References ast_time_create_by_unit(), ast_time_str_to_unit(), and time_unit_labels::unit.

Referenced by AST_TEST_DEFINE(), and handle_cli_rtp_drop_incoming_packets().

143 {
145 }
Definition: ast_expr2.c:325
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:112
enum TIME_UNIT ast_time_str_to_unit(const char *unit)
Convert a string to a time unit enumeration value.
Definition: time.c:65

◆ 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, miscroseconds, 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 65 of file time.c.

References MAX_UNIT_LABELS, TIME_UNIT_ERROR, time_unit_labels::unit, and unit_labels_size.

Referenced by AST_TEST_DEFINE(), and ast_time_create_by_unit_str().

66 {
67  size_t i, j;
68 
69  if (!unit) {
70  return TIME_UNIT_ERROR;
71  }
72 
73  for (i = 0; i < unit_labels_size; ++i) {
74  for (j = 0; j < MAX_UNIT_LABELS; ++j) {
75  /*
76  * A lazy pluralization check. If the given unit string at least starts
77  * with a label assume a match.
78  */
79  if (*unit_labels[i].values[j] && !strncasecmp(unit, unit_labels[i].values[j],
80  strlen(unit_labels[i].values[j]))) {
81  return unit_labels[i].unit;
82  }
83  }
84  }
85 
86  return TIME_UNIT_ERROR;
87 }
enum TIME_UNIT unit
Definition: time.c:46
#define MAX_UNIT_LABELS
Definition: time.c:43
const unsigned int unit_labels_size
Definition: time.c:63
static struct time_unit_labels unit_labels[]
Definition: time.c:50

◆ ast_time_tv_to_usec()

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 89 of file time.c.

Referenced by AST_TEST_DEFINE(), and drop_packets_data_update().

90 {
91  return tv->tv_sec * 1000000 + tv->tv_usec;
92 }

◆ normalize_and_create()

static struct timeval normalize_and_create ( unsigned long  usec)
static

Create a timeval first onverting the given microsecond value into seconds and microseconds.

Parameters
usecmicrosecond value
Returns
A timeval structure

Definition at line 107 of file time.c.

References ast_time_create().

Referenced by ast_time_create_by_unit().

108 {
109  return ast_time_create(usec / 1000000, usec % 1000000);
110 }
struct timeval ast_time_create(ast_time_t sec, ast_suseconds_t usec)
Create a timeval object initialized to given values.
Definition: time.c:94

Variable Documentation

◆ day_labels

const char* day_labels[] = {"d", "", "day"}

Definition at line 38 of file time.c.

◆ hour_labels

const char* hour_labels[] = {"h", "hr", "hour"}

Definition at line 37 of file time.c.

◆ microsecond_labels

const char* microsecond_labels[] = {"us", "usec", "microsecond"}

Definition at line 33 of file time.c.

◆ millisecond_labels

const char* millisecond_labels[] = {"ms", "msec", "millisecond"}

Definition at line 34 of file time.c.

◆ minute_labels

const char* minute_labels[] = {"m", "min", "minute"}

Definition at line 36 of file time.c.

◆ month_labels

const char* month_labels[] = {"mo", "mth", "month"}

Definition at line 40 of file time.c.

◆ nanosecond_labels

const char* nanosecond_labels[] = {"ns", "nsec", "nanosecond"}

Definition at line 32 of file time.c.

◆ second_labels

const char* second_labels[] = {"s", "sec", "second"}

Definition at line 35 of file time.c.

◆ unit_labels

struct time_unit_labels unit_labels[]
static

Definition at line 50 of file time.c.

◆ unit_labels_size

const unsigned int unit_labels_size = sizeof(unit_labels) / sizeof(0[unit_labels])

Definition at line 63 of file time.c.

Referenced by ast_time_str_to_unit().

◆ week_labels

const char* week_labels[] = {"w", "wk", "week"}

Definition at line 39 of file time.c.

◆ year_labels

const char* year_labels[] = {"y", "yr", "year"}

Definition at line 41 of file time.c.