Debian Codegraph @

main/time.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2021, Sangoma Technologies Corporation
5 *
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
11 *
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
15 */
16
17 /*! \file
18 *
19 * \brief Date/Time utility functions
20 */
21
22 /*** MODULEINFO
23 <support_level>core</support_level>
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 }, /* Here so "mo" matches before "m" */
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 * 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 }
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 * \brief Create a timeval first converting the given microsecond value
102 * into seconds and microseconds
103 *
104 * \param usec microsecond value
105 *
106 * \return A timeval structure
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 /* 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 }
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 * \brief Returns a string representation of a time_t as decimal seconds
150 * since the epoch.
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 * \brief Returns a time_t from a string containing seconds since the epoch.
162 */
163 time_t ast_string_to_time_t(const char *str)
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 }
174