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

Date/Time utility functions. More...

#include <inttypes.h>
#include <string.h>
#include <strings.h>
#include <time.h>

Go to the source code of this file.

Data Structures

struct  time_unit_labels
 

Macros

#define MAX_UNIT_LABELS   3
 

Functions

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)
 Returns a string representation of a time_t as decimal seconds since the epoch. 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 converting 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.

Function Documentation

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 
)

Returns a string representation of a time_t as decimal seconds since the epoch.

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 }
static struct timeval normalize_and_create ( unsigned long  usec)
static

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

Parameters
usecmicrosecond value
Returns
A timeval structure

Definition at line 108 of file time.c.

References ast_time_create().

Referenced by ast_time_create_by_unit().

109 {
110  return ast_time_create(usec / 1000000, usec % 1000000);
111 }
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