Asterisk - The Open Source Telephony Project GIT-master-f36a736
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>
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

time_t ast_string_to_time_t (const char *str)
 Returns a time_t from a string containing seconds since the epoch. More...
 
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.

Macro Definition Documentation

◆ MAX_UNIT_LABELS

#define MAX_UNIT_LABELS   3

Definition at line 44 of file time.c.

Function Documentation

◆ ast_string_to_time_t()

time_t ast_string_to_time_t ( const char *  str)

Returns a time_t from a string containing seconds since the epoch.

Definition at line 163 of file time.c.

164{
165 struct tm tm = { 0, };
166
167 /* handle leading spaces */
168 if (strptime(str, " %s", &tm) == NULL) {
169 return (time_t)-1;
170 }
171 tm.tm_isdst = -1;
172 return mktime(&tm);
173}
const char * str
Definition: app_jack.c:147
#define NULL
Definition: resample.c:96

References NULL, and str.

Referenced by bucket_file_expired(), evaluate_equal(), evaluate_greater_than(), and evaluate_less_than().

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

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

References ast_tv().

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

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

114{
115 switch (unit) {
117 return normalize_and_create(val / 1000);
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}
Definition: ast_expr2.c:325
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
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
@ TIME_UNIT_MONTH
Definition: time.h:348
@ TIME_UNIT_MICROSECOND
Definition: time.h:341
@ TIME_UNIT_WEEK
Definition: time.h:347
@ TIME_UNIT_MINUTE
Definition: time.h:344
@ TIME_UNIT_SECOND
Definition: time.h:343
@ TIME_UNIT_YEAR
Definition: time.h:349
@ TIME_UNIT_MILLISECOND
Definition: time.h:342
@ TIME_UNIT_HOUR
Definition: time.h:345
@ TIME_UNIT_DAY
Definition: time.h:346
@ TIME_UNIT_NANOSECOND
Definition: time.h:340

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, and TIME_UNIT_YEAR.

Referenced by AST_TEST_DEFINE(), and ast_time_create_by_unit_str().

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

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

References ast_time_create_by_unit(), and ast_time_str_to_unit().

Referenced by AST_TEST_DEFINE().

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

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

Definition at line 66 of file time.c.

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}
enum TIME_UNIT unit
Definition: time.c:47
static struct time_unit_labels unit_labels[]
Definition: time.c:51
#define MAX_UNIT_LABELS
Definition: time.c:44
const unsigned int unit_labels_size
Definition: time.c:64
@ TIME_UNIT_ERROR
Definition: time.h:339

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

Referenced by AST_TEST_DEFINE(), and ast_time_create_by_unit_str().

◆ ast_time_t_to_string()

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.

153{
154 struct tm tm;
155
156 localtime_r(&time, &tm);
157 return (strftime(buf, length, "%s", &tm) == 0) ? -1 : 0;
158}
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define localtime_r
Definition: utils.h:521

References buf, and localtime_r.

Referenced by ast_sip_format_contact_ami(), bucket_file_set_expiration(), caldav_add_event(), check_expiration_thread(), display_single_entry(), expiration_struct2str(), icalendar_add_event(), odbc_obj_connect(), persistence_expires_struct2str(), and sprint_list_entry().

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

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

Referenced by AST_TEST_DEFINE().

◆ normalize_and_create()

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.

109{
110 return ast_time_create(usec / 1000000, usec % 1000000);
111}

References ast_time_create().

Referenced by ast_time_create_by_unit().

Variable Documentation

◆ day_labels

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

Definition at line 39 of file time.c.

◆ hour_labels

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

Definition at line 38 of file time.c.

◆ microsecond_labels

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

Definition at line 34 of file time.c.

◆ millisecond_labels

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

Definition at line 35 of file time.c.

◆ minute_labels

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

Definition at line 37 of file time.c.

◆ month_labels

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

Definition at line 41 of file time.c.

◆ nanosecond_labels

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

Definition at line 33 of file time.c.

◆ second_labels

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

Definition at line 36 of file time.c.

◆ unit_labels

struct time_unit_labels unit_labels[]
static

Definition at line 51 of file time.c.

Referenced by ast_time_str_to_unit().

◆ unit_labels_size

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

Definition at line 64 of file time.c.

Referenced by ast_time_str_to_unit().

◆ week_labels

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

Definition at line 40 of file time.c.

◆ year_labels

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

Definition at line 42 of file time.c.