| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include <inttypes.h> |
| 27 |
#include <string.h> |
| 28 |
#include <strings.h> |
| 29 |
#include <time.h> |
| 30 |
|
| 31 |
#include "asterisk/time.h" |
| 32 |
|
| 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"}; |
| 43 |
|
| 44 |
#define MAX_UNIT_LABELS 3 |
| 45 |
|
| 46 |
struct time_unit_labels { |
| 47 |
enum TIME_UNIT unit; |
| 48 |
const char **values; |
| 49 |
}; |
| 50 |
|
| 51 |
static struct time_unit_labels unit_labels[] = { |
| 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 }, |
| 62 |
}; |
| 63 |
|
| 64 |
const unsigned int unit_labels_size = sizeof(unit_labels) / sizeof(0[unit_labels]); |
| 65 |
|
| 66 |
enum TIME_UNIT ast_time_str_to_unit(const char *unit) |
| 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 |
|
| 78 |
|
| 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 |
} |
| 89 |
|
| 90 |
ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv) |
| 91 |
{ |
| 92 |
return tv->tv_sec * 1000000 + tv->tv_usec; |
| 93 |
} |
| 94 |
|
| 95 |
struct timeval ast_time_create(ast_time_t sec, ast_suseconds_t usec) |
| 96 |
{ |
| 97 |
return ast_tv(sec, usec); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static struct timeval normalize_and_create(unsigned long usec) |
| 109 |
{ |
| 110 |
return ast_time_create(usec / 1000000, usec % 1000000); |
| 111 |
} |
| 112 |
|
| 113 |
struct timeval ast_time_create_by_unit(unsigned long val, enum TIME_UNIT unit) |
| 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 |
|
| 134 |
return ast_time_create(val * 2629746, 0); |
| 135 |
case TIME_UNIT_YEAR: |
| 136 |
|
| 137 |
return ast_time_create(val * 31556952, 0); |
| 138 |
default: |
| 139 |
return ast_time_create(0, 0); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
struct timeval ast_time_create_by_unit_str(unsigned long val, const char *unit) |
| 144 |
{ |
| 145 |
return ast_time_create_by_unit(val, ast_time_str_to_unit(unit)); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
int ast_time_t_to_string(time_t time, char *buf, size_t length) |
| 153 |
{ |
| 154 |
struct tm tm; |
| 155 |
|
| 156 |
localtime_r(&time, &tm); |
| 157 |
return (strftime(buf, length, "%s", &tm) == 0) ? -1 : 0; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
time_t ast_string_to_time_t(const char *str) |
| 164 |
{ |
| 165 |
struct tm tm = { 0, }; |
| 166 |
|
| 167 |
|
| 168 |
if (strptime(str, " %s", &tm) == NULL) { |
| 169 |
return (time_t)-1; |
| 170 |
} |
| 171 |
tm.tm_isdst = -1; |
| 172 |
return mktime(&tm); |
| 173 |
} |
| 174 |
|