Asterisk - The Open Source Telephony Project GIT-master-85241bd
time.c
Go to the documentation of this file.
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
33const char *nanosecond_labels[] = {"ns", "nsec", "nanosecond"};
34const char *microsecond_labels[] = {"us", "usec", "microsecond"};
35const char *millisecond_labels[] = {"ms", "msec", "millisecond"};
36const char *second_labels[] = {"s", "sec", "second"};
37const char *minute_labels[] = {"m", "min", "minute"};
38const char *hour_labels[] = {"h", "hr", "hour"};
39const char *day_labels[] = {"d", "", "day"};
40const char *week_labels[] = {"w", "wk", "week"};
41const char *month_labels[] = {"mo", "mth", "month"};
42const char *year_labels[] = {"y", "yr", "year"};
43
44#define MAX_UNIT_LABELS 3
45
48 const char **values;
49};
50
51static struct time_unit_labels unit_labels[] = {
55 { TIME_UNIT_MONTH, month_labels }, /* Here so "mo" matches before "m" */
62};
63
64const unsigned int unit_labels_size = sizeof(unit_labels) / sizeof(0[unit_labels]);
65
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
90ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv)
91{
92 return tv->tv_sec * 1000000 + tv->tv_usec;
93}
94
95struct 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 */
108static struct timeval normalize_and_create(unsigned long usec)
109{
110 return ast_time_create(usec / 1000000, usec % 1000000);
111}
112
113struct timeval ast_time_create_by_unit(unsigned long val, enum TIME_UNIT unit)
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}
142
143struct timeval ast_time_create_by_unit_str(unsigned long val, const char *unit)
144{
146}
147
148/*!
149 * \brief Returns a string representation of a time_t as decimal seconds
150 * since the epoch.
151 */
152int 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 */
163time_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
const char * str
Definition: app_jack.c:147
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define NULL
Definition: resample.c:96
String manipulation functions.
const char ** values
Definition: time.c:48
enum TIME_UNIT unit
Definition: time.c:47
Definition: ast_expr2.c:325
enum TIME_UNIT ast_time_str_to_unit(const char *unit)
Convert a string to a time unit enumeration value.
Definition: time.c:66
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.
Definition: time.c:152
static struct time_unit_labels unit_labels[]
Definition: time.c:51
time_t ast_string_to_time_t(const char *str)
Returns a time_t from a string containing seconds since the epoch.
Definition: time.c:163
const char * second_labels[]
Definition: time.c:36
#define MAX_UNIT_LABELS
Definition: time.c:44
const char * day_labels[]
Definition: time.c:39
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(unsigned long val, enum TIME_UNIT unit)
Convert the given unit value, and create a timeval object from it.
Definition: time.c:113
const char * hour_labels[]
Definition: time.c:38
const char * minute_labels[]
Definition: time.c:37
const char * nanosecond_labels[]
Definition: time.c:33
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.
Definition: time.c:143
const char * millisecond_labels[]
Definition: time.c:35
ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv)
Convert a timeval structure to microseconds.
Definition: time.c:90
const char * year_labels[]
Definition: time.c:42
const char * week_labels[]
Definition: time.c:40
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
const unsigned int unit_labels_size
Definition: time.c:64
const char * month_labels[]
Definition: time.c:41
const char * microsecond_labels[]
Definition: time.c:34
Time-related functions and macros.
TIME_UNIT
Time units enumeration.
Definition: time.h:338
@ 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_ERROR
Definition: time.h:339
@ 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
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:235
#define localtime_r
Definition: utils.h:521