Asterisk - The Open Source Telephony Project  GIT-master-e8cda4b
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 <time.h>
29 
30 #include "asterisk/time.h"
31 
32 const char *nanosecond_labels[] = {"ns", "nsec", "nanosecond"};
33 const char *microsecond_labels[] = {"us", "usec", "microsecond"};
34 const char *millisecond_labels[] = {"ms", "msec", "millisecond"};
35 const char *second_labels[] = {"s", "sec", "second"};
36 const char *minute_labels[] = {"m", "min", "minute"};
37 const char *hour_labels[] = {"h", "hr", "hour"};
38 const char *day_labels[] = {"d", "", "day"};
39 const char *week_labels[] = {"w", "wk", "week"};
40 const char *month_labels[] = {"mo", "mth", "month"};
41 const char *year_labels[] = {"y", "yr", "year"};
42 
43 #define MAX_UNIT_LABELS 3
44 
47  const char **values;
48 };
49 
50 static struct time_unit_labels unit_labels[] = {
54  { TIME_UNIT_MONTH, month_labels }, /* Here so "mo" matches before "m" */
61 };
62 
63 const unsigned int unit_labels_size = sizeof(unit_labels) / sizeof(0[unit_labels]);
64 
66 {
67  size_t i, j;
68 
69  if (!unit) {
70  return TIME_UNIT_ERROR;
71  }
72 
73  for (i = 0; i < unit_labels_size; ++i) {
74  for (j = 0; j < MAX_UNIT_LABELS; ++j) {
75  /*
76  * A lazy pluralization check. If the given unit string at least starts
77  * with a label assume a match.
78  */
79  if (*unit_labels[i].values[j] && !strncasecmp(unit, unit_labels[i].values[j],
80  strlen(unit_labels[i].values[j]))) {
81  return unit_labels[i].unit;
82  }
83  }
84  }
85 
86  return TIME_UNIT_ERROR;
87 }
88 
89 ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv)
90 {
91  return tv->tv_sec * 1000000 + tv->tv_usec;
92 }
93 
94 struct timeval ast_time_create(ast_time_t sec, ast_suseconds_t usec)
95 {
96  return ast_tv(sec, usec);
97 }
98 
99 /*!
100  * \brief Create a timeval first onverting the given microsecond value
101  * into seconds and microseconds
102  *
103  * \param usec microsecond value
104  *
105  * \return A timeval structure
106  */
107 static struct timeval normalize_and_create(unsigned long usec)
108 {
109  return ast_time_create(usec / 1000000, usec % 1000000);
110 }
111 
112 struct timeval ast_time_create_by_unit(unsigned long val, enum TIME_UNIT unit)
113 {
114  switch (unit) {
116  return normalize_and_create(val / 1000);
118  return normalize_and_create(val);
120  return normalize_and_create(val * 1000);
121  case TIME_UNIT_SECOND:
122  return ast_time_create(val, 0);
123  case TIME_UNIT_MINUTE:
124  return ast_time_create(val * 60, 0);
125  case TIME_UNIT_HOUR:
126  return ast_time_create(val * 3600, 0);
127  case TIME_UNIT_DAY:
128  return ast_time_create(val * 86400, 0);
129  case TIME_UNIT_WEEK:
130  return ast_time_create(val * 604800, 0);
131  case TIME_UNIT_MONTH:
132  /* Using Gregorian mean month - 30.436875 * 86400 */
133  return ast_time_create(val * 2629746, 0);
134  case TIME_UNIT_YEAR:
135  /* Using Gregorian year - 365.2425 * 86400 */
136  return ast_time_create(val * 31556952, 0);
137  default:
138  return ast_time_create(0, 0);
139  }
140 }
141 
142 struct timeval ast_time_create_by_unit_str(unsigned long val, const char *unit)
143 {
145 }
Definition: ast_expr2.c:325
Time-related functions and macros.
TIME_UNIT
Time units enumeration.
Definition: time.h:243
const char * week_labels[]
Definition: time.c:39
enum TIME_UNIT unit
Definition: time.c:46
ast_suseconds_t ast_time_tv_to_usec(const struct timeval *tv)
Convert a timeval structure to microseconds.
Definition: time.c:89
static struct timeval normalize_and_create(unsigned long usec)
Create a timeval first onverting the given microsecond value into seconds and microseconds.
Definition: time.c:107
#define MAX_UNIT_LABELS
Definition: time.c:43
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:112
const char * day_labels[]
Definition: time.c:38
const char ** values
Definition: time.c:47
enum TIME_UNIT ast_time_str_to_unit(const char *unit)
Convert a string to a time unit enumeration value.
Definition: time.c:65
const char * minute_labels[]
Definition: time.c:36
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:142
const char * year_labels[]
Definition: time.c:41
const char * month_labels[]
Definition: time.c:40
const char * nanosecond_labels[]
Definition: time.c:32
const unsigned int unit_labels_size
Definition: time.c:63
const char * millisecond_labels[]
Definition: time.c:34
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:226
const char * hour_labels[]
Definition: time.c:37
const char * second_labels[]
Definition: time.c:35
struct timeval ast_time_create(ast_time_t sec, ast_suseconds_t usec)
Create a timeval object initialized to given values.
Definition: time.c:94
static struct time_unit_labels unit_labels[]
Definition: time.c:50
const char * microsecond_labels[]
Definition: time.c:33