1 /*------------------------------------------------------------------------------
2 *
3 * Copyright (c) 2011-2021, EURid vzw. All rights reserved.
4 * The YADIFA TM software product is provided under the BSD 3-clause license:
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of EURid nor the names of its contributors may be
16 * used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 *------------------------------------------------------------------------------
32 *
33 */
34
35 /** @defgroup dnscoretools Generic Tools
36 * @ingroup dnscore
37 * @brief
38 *
39 *
40 *
41 * @{
42 *
43 *----------------------------------------------------------------------------*/
44 #ifndef _TIMEMS_H
45 #define _TIMEMS_H
46
47 #include <unistd.h>
48 #include <time.h>
49 #include <dnscore/sys_types.h>
50
51 #ifdef __cplusplus
52 extern "C"
53 {
54 #endif
55
56 #define ONE_SECOND_US 1000000LL
57 #define ONE_SECOND_US_F 1000000.0
58
59 /**
60 * A local implementation of struct tm *t
61 *
62 * @param tv
63 * @return
64 */
65
66 time_t timegm_internal(struct tm *tv);
67
68 /*
69 * Returns the time in us
70 */
71
72 s64 timeus();
73
74 /*
75 * Returns the time in us and sets the pointed s32 to the time in s
76 */
77
78 s64 timeus_and_s(s32 *seconds_ptr);
79
80
81
82 /*
83 * Returns the time in ms
84 */
85
86 s64 timems();
87
88 /*
89 * Waits until the ms is incremented, then returns the time in ms
90 */
91
92 s64 timems_new();
93
94 /**
95 * usleep only support a limited range of time (sometimes 2^32 us, sometimes < 1 s)
96 * This wrapper ensures time supported is up to 4294967295.000000 seconds
97 *
98 * @param us the number of microseconds to wait for, can range from 0 to 4294967295000000 micro seconds
99 */
100
101 void usleep_ex(u64 us_);
102
103 void usleep_until(s64 epoch_us);
104
105 time_t mkgmtime(const struct tm *tm);
106
107 bool time_is_leap_year(int y);
108
109 int time_days_in_month(int y, int m);
110
111 /**
112 * Retrieves the first day of the month.
113 *
114 * 0 is Sunday
115 *
116 * @param year 0-based
117 * @param month 0-based
118 * @return the number of the day of the month or an error code
119 */
120
121 int time_first_day_of_month(int year, int month);
122
123 /**
124 * Sun to Sat
125 *
126 * @param day
127 *
128 * @return A 3 letters name followed by a zero
129 */
130
131 const char * time_get_day_of_week_name(int day);
132
133 /**
134 * Jan to Dec
135 *
136 * @param month
137 * @return A 3 letters name followed by a zero
138 */
139
140 const char * time_get_month_of_year_name(int month);
141
142 /**
143 * Convert time structure into the text format defined by RFC5322 (GMT)
144 * Does put a '\0' at the end of the buffer.
145 * Requires a buffer of at least 29 bytes.
146 *
147 * @param epoch
148 * @param buffer
149 * @param buffer_size
150 *
151 * @return the number of chars written or an error
152 */
153
154 ya_result
155 time_tm_as_rfc5322(const struct tm *t, char *buffer, size_t buffer_size);
156
157 /**
158 * Convert epoch into the text format defined by RFC5322 (GMT)
159 * Does put a '\0' at the end of the buffer.
160 * Requires a buffer of at least 29 bytes.
161 *
162 * @param epoch
163 * @param buffer
164 * @param buffer_size
165 *
166 * @return the number of chars written or an error
167 */
168
169 ya_result time_epoch_as_rfc5322(time_t epoch, char *buffer, size_t buffer_size);
170
171 /**
172 * Returns timeus() - offset
173 * Used to fake the current time.
174 */
175
176 s64 timeus_with_offset();
177
178 /**
179 * Sets the offset of the time returned by timeus_with_offset()
180 */
181
182 void timeus_set_offset(s64 us);
183
184 /**
185 * Parses a text as a date/time and converts it to an epoch in microseconds.
186 *
187 * yesterday
188 * now
189 * tomorrow
190 * +1y +1year +1years (months,weeks,days,seconds)
191 * -1y -1year -1years (months,weeks,days,seconds)
192 * 2019-04-16
193 * 2019-04-16_12:00:00.123456
194 * 20190416
195 * 20190416120000123456
196 *
197 */
198
199 s64 timeus_from_smarttime_ex(const char *text, s64 now);
200
201 /**
202 * Parses a text as a date/time and converts it to an epoch in microseconds.
203 *
204 * yesterday
205 * now
206 * tomorrow
207 * +1y +1year +1years (months,weeks,days,seconds)
208 * -1y -1year -1years (months,weeks,days,seconds)
209 * 2019-04-16
210 * 2019-04-16_12:00:00.123456
211 * 20190416
212 * 20190416120000123456
213 *
214 */
215
216 s64 timeus_from_smarttime(const char *text);
217
timeus_diff_seconds_double(s64 from,s64 to)218 static inline double timeus_diff_seconds_double(s64 from, s64 to)
219 {
220 double ret = (double)(to - from);
221 ret /= ONE_SECOND_US_F;
222 return ret;
223 }
224
timeus_diff_ms_double(s64 from,s64 to)225 static inline double timeus_diff_ms_double(s64 from, s64 to)
226 {
227 double ret = (double)(to - from);
228 ret /= 1000.0;
229 return ret;
230 }
231
timeus_diff_ms(s64 from,s64 to)232 static inline s64 timeus_diff_ms(s64 from, s64 to)
233 {
234 s64 ret = (to - from);
235 ret /= 1000LL;
236 return ret;
237 }
238
time_to_timeus(time_t t)239 static inline s64 time_to_timeus(time_t t)
240 {
241 return (ONE_SECOND_US * t);
242 }
243
244 #ifdef __cplusplus
245 }
246 #endif
247
248 #endif /* _TIMEMS_H */
249 /** @} */
250