Debian Codegraph @

lib/time.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2016 Brazil
3 Copyright (C) 2020-2022 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn_time.h"cxx . . $ `<file>/lib/grn_time.h`/
21 #include "grn_ctx.h"cxx . . $ `<file>/lib/grn_ctx.h`/
22 #include "grn_str.h"cxx . . $ `<file>/lib/grn_str.h`/
23
24 #include <math.h>
25 #include <stdio.h>
26 #include <time.h>
27
28 #if defined(HAVE__LOCALTIME64_S) && defined(__GNUC__cxx . . $ `<built-in>:8:9`!)
29 # ifdef _WIN64
30 # define localtime_s(tm, time) _localtime64_s(tm, time)
31 # else /* _WIN64 */
32 # define localtime_s(tm, time) _localtime32_s(tm, time)
33 # endif /* _WIN64 */
34 #endif /* defined(HAVE__LOCALTIME64_S) && defined(__GNUC__) */
35
36 /* fixme by 2038 */
37
38 grn_rccxx . . $ grn_rc#
39 grn_timeval_nowcxx . . $ grn_timeval_now(b116eeadcb699bd1).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_timevalcxx . . $ grn_timeval# *tv)
40 {
41 #ifdef WIN32
42 struct __timeb64 tb;
43 _ftime64_s(&tb);
44 tv->tv_sec = tb.time;
45 tv->tv_nsec = tb.millitm * (GRN_TIME_NSEC_PER_SEC / 1000);
46 return GRN_SUCCESS;
47 #else /* WIN32 */
48 # ifdef HAVE_CLOCK_GETTIMEcxx . . $ `build/config.h:166:9`!
49 struct timespeccxx . . $ timespec# t;
50 if (clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(CLOCK_REALTIMEcxx . . $ `<external>/89459239771508533/time.h:46:10`!, &t)) {
51 SERRcxx . . $ `lib/grn_error.h:411:11`!("clock_gettime");
52 } else {
53 tv->tv_seccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_sec. = t.tv_seccxx . . $ timespec#tv_sec.;
54 tv->tv_nseccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_nsec. = (int32_tcxx . . $ int32_t#)(t.tv_nseccxx . . $ timespec#tv_nsec.);
55 }
56 return ctx->rccxx . . $ _grn_ctx#rc.;
57 # else /* HAVE_CLOCK_GETTIME */
58 struct timeval t;
59 if (gettimeofday(&t, NULL)) {
60 SERR("gettimeofday");
61 } else {
62 tv->tv_sec = t.tv_sec;
63 tv->tv_nsec = GRN_TIME_USEC_TO_NSEC(t.tv_usec);
64 }
65 return ctx->rc;
66 # endif /* HAVE_CLOCK_GETTIME */
67 #endif /* WIN32 */
68 }
69
70 grn_timevalcxx . . $ grn_timeval#
71 grn_timeval_from_doublecxx . . $ grn_timeval_from_double(ff8331a80aa3c8d7).(grn_ctxcxx . . $ grn_ctx# *ctx, double value)
72 {
73 grn_timevalcxx . . $ grn_timeval# timeval;
74 double sec = trunc(value);
75 timeval.tv_seccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_sec. = (int64_tcxx . . $ int64_t#)sec;
76 timeval.tv_nseccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_nsec. =
77 (int32_tcxx . . $ int32_t#)(
78 ((int64_tcxx . . $ int64_t#)((value - (double)(timeval.tv_seccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_sec.)) * GRN_TIME_NSEC_PER_SECcxx . . $ `include/groonga/time.h:35:9`!) %
79 GRN_TIME_NSEC_PER_SECcxx . . $ `include/groonga/time.h:35:9`!));
80 return timeval;
81 }
82
83 void
84 grn_time_nowcxx . . $ grn_time_now(cda3a5f3c2e53c6b).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_objcxx . . $ grn_obj# *obj)
85 {
86 grn_timevalcxx . . $ grn_timeval# tv;
87 grn_timeval_nowcxx . . $ grn_timeval_now(b116eeadcb699bd1).(ctx, &tv);
88 GRN_TIME_SETcxx . . $ `include/groonga/groonga.h:2939:9`!(ctx, obj, GRN_TIME_PACKcxx . . $ `include/groonga/time.h:51:9`!(tv.tv_sec,
89 GRN_TIME_NSEC_TO_USECcxx . . $ `include/groonga/time.h:39:9`!(tv.tv_nsec)));
90 }
91
92 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
93 grn_time_t_to_tmcxx . . $ grn_time_t_to_tm(f8a68ae4da1a7edf).(grn_ctxcxx . . $ grn_ctx# *ctx, const grn_time_tcxx . . $ `include/groonga/portability.h:208:11`! time, struct tmcxx . . $ tm# *tm)
94 {
95 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! success;
96 const char *function_name;
97 #ifdef HAVE__LOCALTIME64_S
98 function_name = "localtime_s";
99 success = (_localtime64_s(tm, &time) == 0);
100 #else /* HAVE__LOCALTIME64_S */
101 # ifdef HAVE_LOCALTIME_Rcxx . . $ `build/config.h:174:9`!
102 function_name = "localtime_r";
103 success = (localtime_rcxx . . $ localtime_r(92e3d173686db852).(&time, tm) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
104 # else /* HAVE_LOCALTIME_R */
105 function_name = "localtime";
106 {
107 struct tm *local_tm;
108 local_tm = localtime(&time);
109 if (local_tm) {
110 success = true;
111 memcpy(tm, local_tm, sizeof(struct tm));
112 } else {
113 success = false;
114 }
115 }
116 # endif /* HAVE_LOCALTIME_R */
117 #endif /* HAVE__LOCALTIME64_S */
118 if (!success) {
119 SERRcxx . . $ `lib/grn_error.h:411:11`!("%s: failed to convert time_t to struct tm: <%" GRN_FMT_INT64Dcxx . . $ `lib/grn.h:369:11`! ">",
120 function_name,
121 (int64_t)time);
122 }
123 return success;
124 }
125
126 struct tmcxx . . $ tm# *
127 grn_timeval2tmcxx . . $ grn_timeval2tm(17aa9e83661c23e4).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_timevalcxx . . $ grn_timeval# *tv, struct tmcxx . . $ tm# *tm)
128 {
129 if (grn_time_t_to_tmcxx . . $ grn_time_t_to_tm(f8a68ae4da1a7edf).(ctx, tv->tv_seccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_sec., tm)) {
130 return tm;
131 } else {
132 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
133 }
134 }
135
136 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
137 grn_time_to_tmcxx . . $ grn_time_to_tm(f8a68ae4da1a7edf).(grn_ctxcxx . . $ grn_ctx# *ctx, int64_tcxx . . $ int64_t# time, struct tmcxx . . $ tm# *tm)
138 {
139 int64_tcxx . . $ int64_t# sec;
140 int32_tcxx . . $ int32_t# usec;
141
142 GRN_TIME_UNPACKcxx . . $ `include/groonga/time.h:52:9`!(time, sec, usec);
143 return grn_time_t_to_tmcxx . . $ grn_time_t_to_tm(f8a68ae4da1a7edf).(ctx, sec, tm);
144 }
145
146 static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
147 grn_time_t_from_tmcxx . . $ grn_time_t_from_tm(b8ef19f68bf75a24).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_time_tcxx . . $ `include/groonga/portability.h:208:11`! *time, struct tmcxx . . $ tm# *tm)
148 {
149 tm->tm_ydaycxx . . $ tm#tm_yday. = -1;
150 *time = grn_mktimecxx . . $ `include/groonga/portability.h:210:11`!(tm);
151 /* We can't use (*time != -1) because -1 is a valid UNIX time
152 * (1969-12-31T23:59:59Z). */
153 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! success = (tm->tm_ydaycxx . . $ tm#tm_yday. != -1);
154 if (!success) {
155 ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT,
156 "mktime: failed to convert struct tm to time_t: "
157 "<%04d-%02d-%02dT%02d:%02d:%02d>(%d)",
158 1900 + tm->tm_year,
159 tm->tm_mon + 1,
160 tm->tm_mday,
161 tm->tm_hour,
162 tm->tm_min,
163 tm->tm_sec,
164 tm->tm_isdst);
165 }
166 return success;
167 }
168
169 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`!
170 grn_time_from_tmcxx . . $ grn_time_from_tm(b8ef19f68bf75a24).(grn_ctxcxx . . $ grn_ctx# *ctx, int64_tcxx . . $ int64_t# *time, struct tmcxx . . $ tm# *tm)
171 {
172 grn_time_tcxx . . $ `include/groonga/portability.h:208:11`! sec_time_t;
173 int64_tcxx . . $ int64_t# sec;
174 int32_tcxx . . $ int32_t# usec = 0;
175
176 if (!grn_time_t_from_tmcxx . . $ grn_time_t_from_tm(b8ef19f68bf75a24).(ctx, &sec_time_t, tm)) {
177 return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
178 }
179
180 sec = sec_time_t;
181 *time = GRN_TIME_PACKcxx . . $ `include/groonga/time.h:51:9`!(sec, usec);
182 return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
183 }
184
185 grn_rccxx . . $ grn_rc#
186 grn_timeval2strcxx . . $ grn_timeval2str(52f9bd5a37f66b1d).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_timevalcxx . . $ grn_timeval# *tv, char *buf, size_t buf_size)
187 {
188 struct tmcxx . . $ tm# tm;
189 struct tmcxx . . $ tm# *ltm;
190 ltm = grn_timeval2tmcxx . . $ grn_timeval2tm(17aa9e83661c23e4).(ctx, tv, &tm);
191 grn_snprintfcxx . . $ `include/groonga/portability.h:137:11`!(buf, buf_size, GRN_TIMEVAL_STR_SIZEcxx . . $ `lib/grn_time.h:28:9`!,
192 GRN_TIMEVAL_STR_FORMATcxx . . $ `lib/grn_time.h:31:9`!,
193 ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday,
194 ltm->tm_hour, ltm->tm_min, ltm->tm_sec,
195 (int)(GRN_TIME_NSEC_TO_USECcxx . . $ `include/groonga/time.h:39:9`!(tv->tv_nsec)));
196 if (buf_size > GRN_TIMEVAL_STR_SIZEcxx . . $ `lib/grn_time.h:28:9`!) {
197 buf[GRN_TIMEVAL_STR_SIZEcxx . . $ `lib/grn_time.h:28:9`! - 1] = '\0';
198 } else {
199 buf[buf_size - 1] = '\0';
200 }
201 return ctx->rccxx . . $ _grn_ctx#rc.;
202 }
203
204 static grn_rccxx . . $ grn_rc#
205 grn_str2timeval_offset_seccxx . . $ grn_str2timeval_offset_sec(563aaa1fa740afd0).(const char *start, const char *end, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! *have_offset, int32_tcxx . . $ int32_t# *gm_offset_sec)
206 {
207 const char *position = start;
208 int32_tcxx . . $ int32_t# hours, minutes = 0, sign = 1;
209
210 *have_offset = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
211 *gm_offset_sec = 0;
212
213 if (position >= end) { return GRN_SUCCESScxx . . $ GRN_SUCCESS.; }
214
215 switch (*position) {
216 case '+' :
217 sign = 1;
218 *have_offset = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
219 break;
220 case '-' :
221 sign = -1;
222 *have_offset = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
223 break;
224 case 'Z' :
225 case 'z' :
226 *have_offset = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!;
227 return GRN_SUCCESScxx . . $ GRN_SUCCESS.;
228 default :
229 return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.;
230 }
231
232 position++;
233 if (position >= end) { return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; }
234
235 hours = grn_atoicxx . . $ grn_atoi(adf3ff2c2b114929).(position, end, &position);
236 if (hours < 0 || hours >= 24) { return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; }
237
238 if (position < end && *position == ':') {
239 position++;
240 if (position == end) { return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; }
241
242 minutes = grn_atoicxx . . $ grn_atoi(adf3ff2c2b114929).(position, end, &position);
243 if (minutes < 0 || minutes >= 60) { return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; }
244 }
245
246 *gm_offset_sec = sign * (hours * 3600 + minutes * 60);
247
248 return GRN_SUCCESScxx . . $ GRN_SUCCESS.;
249 }
250
251 grn_rccxx . . $ grn_rc#
252 grn_str2timevalcxx . . $ grn_str2timeval(3ebc43a0f745c958).(const char *str, uint32_tcxx . . $ uint32_t# str_len, grn_timevalcxx . . $ grn_timeval# *tv)
253 {
254 struct tmcxx . . $ tm# tm;
255 const char *r1, *r2, *rend = str + str_len;
256 uint32_tcxx . . $ uint32_t# uv = 0;
257
258 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&tm, 0, sizeof(struct tmcxx . . $ tm#));
259
260 tm.tm_yearcxx . . $ tm#tm_year. = (int)grn_atouicxx . . $ grn_atoui(2da3fe788a18ce64).(str, rend, &r1) - 1900;
261 if ((r1 + 1) >= rend || (*r1 != '/' && *r1 != '-')) {
262 return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.;
263 }
264 r1++;
265 tm.tm_moncxx . . $ tm#tm_mon. = (int)grn_atouicxx . . $ grn_atoui(2da3fe788a18ce64).(r1, rend, &r1) - 1;
266 if ((r1 + 1) >= rend || (*r1 != '/' && *r1 != '-') ||
267 tm.tm_moncxx . . $ tm#tm_mon. < 0 || tm.tm_moncxx . . $ tm#tm_mon. >= 12) { return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; }
268 r1++;
269 tm.tm_mdaycxx . . $ tm#tm_mday. = (int)grn_atouicxx . . $ grn_atoui(2da3fe788a18ce64).(r1, rend, &r1);
270 if (tm.tm_mdaycxx . . $ tm#tm_mday. < 1 || tm.tm_mdaycxx . . $ tm#tm_mday. > 31) {
271 return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.;
272 }
273 if (r1 == rend) {
274 /* YYYY-MM-DD */
275 r2 = r1;
276 } else {
277 /* YYYY-MM-DD[ Tt]hh:mm:ss */
278 if ((r1 + 1) >= rend || (*r1 != ' ' && *r1 != 'T' && *r1 != 't')) {
279 return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.;
280 }
281
282 tm.tm_hourcxx . . $ tm#tm_hour. = (int)grn_atouicxx . . $ grn_atoui(2da3fe788a18ce64).(++r1, rend, &r2);
283 if ((r2 + 1) >= rend || r1 == r2 || *r2 != ':' ||
284 tm.tm_hourcxx . . $ tm#tm_hour. < 0 || tm.tm_hourcxx . . $ tm#tm_hour. >= 24) {
285 return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.;
286 }
287 r1 = r2 + 1;
288 tm.tm_mincxx . . $ tm#tm_min. = (int)grn_atouicxx . . $ grn_atoui(2da3fe788a18ce64).(r1, rend, &r2);
289 if ((r2 + 1) >= rend || r1 == r2 || *r2 != ':' ||
290 tm.tm_mincxx . . $ tm#tm_min. < 0 || tm.tm_mincxx . . $ tm#tm_min. >= 60) {
291 return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.;
292 }
293 r1 = r2 + 1;
294 tm.tm_seccxx . . $ tm#tm_sec. = (int)grn_atouicxx . . $ grn_atoui(2da3fe788a18ce64).(r1, rend, &r2);
295 if (r1 == r2 ||
296 tm.tm_seccxx . . $ tm#tm_sec. < 0 || tm.tm_seccxx . . $ tm#tm_sec. > 61 /* leap 2sec */) {
297 return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.;
298 }
299 r1 = r2;
300
301 if ((r1 + 1) < rend && *r1 == '.') {
302 /* YYYY-MM-DD[ Tt]hh:mm:ss.UUUUUU */
303 uv = grn_atouicxx . . $ grn_atoui(2da3fe788a18ce64).(++r1, rend, &r2);
304
305 for (int i = 0; r2 + i < r1 + 6; i++) {
306 uv *= 10;
307 }
308 if (uv >= GRN_TIME_USEC_PER_SECcxx . . $ `include/groonga/time.h:44:9`!) { return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; }
309 }
310 }
311
312 tm.tm_ydaycxx . . $ tm#tm_yday. = -1;
313 tm.tm_isdstcxx . . $ tm#tm_isdst. = -1;
314
315 {
316 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! have_offset = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!;
317 int32_tcxx . . $ int32_t# gm_offset_sec = 0;
318 grn_rccxx . . $ grn_rc# rc = GRN_SUCCESScxx . . $ GRN_SUCCESS.;
319
320 rc = grn_str2timeval_offset_seccxx . . $ grn_str2timeval_offset_sec(563aaa1fa740afd0).(r2, rend, &have_offset, &gm_offset_sec);
321
322 if (rc != GRN_SUCCESScxx . . $ GRN_SUCCESS.) { return rc; }
323
324 if (have_offset) {
325 tv->tv_seccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_sec. = grn_timegmcxx . . $ `include/groonga/portability.h:211:11`!(&tm);
326 tv->tv_seccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_sec. -= gm_offset_sec;
327 } else {
328 tv->tv_seccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_sec. = grn_mktimecxx . . $ `include/groonga/portability.h:210:11`!(&tm);
329 }
330 /* tm_yday is set appropriately (0-365) on successful completion. */
331 if (tm.tm_ydaycxx . . $ tm#tm_yday. == -1) { return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; }
332 }
333
334 tv->tv_nseccxx . . $ $anonymous_type_1555d7cc1d218aa4_15#tv_nsec. = (int32_tcxx . . $ int32_t#)GRN_TIME_USEC_TO_NSECcxx . . $ `include/groonga/time.h:40:9`!(uv);
335 return GRN_SUCCESScxx . . $ GRN_SUCCESS.;
336 }