33 const char *nanosecond_labels[] = {
"ns",
"nsec",
"nanosecond"};
34 const char *microsecond_labels[] = {
"us",
"usec",
"microsecond"};
35 const char *millisecond_labels[] = {
"ms",
"msec",
"millisecond"};
36 const char *second_labels[] = {
"s",
"sec",
"second"};
37 const char *minute_labels[] = {
"m",
"min",
"minute"};
38 const char *hour_labels[] = {
"h",
"hr",
"hour"};
39 const char *day_labels[] = {
"d",
"",
"day"};
40 const char *week_labels[] = {
"w",
"wk",
"week"};
41 const char *month_labels[] = {
"mo",
"mth",
"month"};
42 const char *year_labels[] = {
"y",
"yr",
"year"};
44 #define MAX_UNIT_LABELS 3
52 { TIME_UNIT_NANOSECOND, nanosecond_labels },
53 { TIME_UNIT_MICROSECOND, microsecond_labels },
54 { TIME_UNIT_MILLISECOND, millisecond_labels },
55 { TIME_UNIT_MONTH, month_labels },
56 { TIME_UNIT_SECOND, second_labels },
57 { TIME_UNIT_MINUTE, minute_labels },
58 { TIME_UNIT_HOUR, hour_labels },
59 { TIME_UNIT_DAY, day_labels },
60 { TIME_UNIT_WEEK, week_labels },
61 { TIME_UNIT_YEAR, year_labels },
64 const unsigned int unit_labels_size =
sizeof(unit_labels) /
sizeof(0[unit_labels]);
71 return TIME_UNIT_ERROR;
74 for (i = 0; i < unit_labels_size; ++i) {
75 for (j = 0; j < MAX_UNIT_LABELS; ++j) {
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;
87 return TIME_UNIT_ERROR;
92 return tv->tv_sec * 1000000 + tv->tv_usec;
116 case TIME_UNIT_NANOSECOND:
118 case TIME_UNIT_MICROSECOND:
120 case TIME_UNIT_MILLISECOND:
122 case TIME_UNIT_SECOND:
124 case TIME_UNIT_MINUTE:
132 case TIME_UNIT_MONTH:
156 localtime_r(&time, &tm);
157 return (strftime(buf, length,
"%s", &tm) == 0) ? -1 : 0;
165 struct tm tm = { 0, };
168 if (strptime(str,
" %s", &tm) == NULL) {
String manipulation functions.
Time-related functions and macros.
TIME_UNIT
Time units enumeration.
time_t ast_string_to_time_t(const char *str)
Returns a time_t from a string containing seconds since the epoch.
ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv)
Convert a timeval structure to microseconds.
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.
static struct timeval normalize_and_create(unsigned long usec)
Create a timeval first converting the given microsecond value into seconds and microseconds.
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.
enum TIME_UNIT ast_time_str_to_unit(const char *unit)
Convert a string to a time unit enumeration value.
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.
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
struct timeval ast_time_create(ast_time_t sec, ast_suseconds_t usec)
Create a timeval object initialized to given values.