Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | #ifndef DEBUG_MKTIME | - |
23 | # define DEBUG_MKTIME 0 | - |
24 | #endif | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | #if !defined _LIBC && !DEBUG_MKTIME | - |
40 | # include <config.h> | - |
41 | #endif | - |
42 | | - |
43 | | - |
44 | | - |
45 | | - |
46 | #ifndef LEAP_SECONDS_POSSIBLE | - |
47 | # define LEAP_SECONDS_POSSIBLE 1 | - |
48 | #endif | - |
49 | | - |
50 | #include <time.h> | - |
51 | | - |
52 | #include <limits.h> | - |
53 | #include <stdbool.h> | - |
54 | | - |
55 | #include <intprops.h> | - |
56 | #include <verify.h> | - |
57 | | - |
58 | #if DEBUG_MKTIME | - |
59 | # include <stdio.h> | - |
60 | # include <stdlib.h> | - |
61 | # include <string.h> | - |
62 | | - |
63 | # undef mktime | - |
64 | # define mktime my_mktime | - |
65 | #endif | - |
66 | | - |
67 | #if NEED_MKTIME_WINDOWS /* on native Windows */ | - |
68 | # include <stdlib.h> | - |
69 | # include <string.h> | - |
70 | #endif | - |
71 | | - |
72 | #if NEED_MKTIME_WORKING || NEED_MKTIME_INTERNAL || DEBUG_MKTIME | - |
73 | | - |
74 | | - |
75 | | - |
76 | | - |
77 | | - |
78 | | - |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | | - |
84 | | - |
85 | | - |
86 | | - |
87 | #if INT_MAX <= LONG_MAX / 3 / 366 / 24 / 60 / 60 | - |
88 | typedef long int long_int; | - |
89 | #else | - |
90 | typedef long long int long_int; | - |
91 | #endif | - |
92 | verify (INT_MAX <= TYPE_MAXIMUM (long_int) / 3 / 366 / 24 / 60 / 60); | - |
93 | | - |
94 | | - |
95 | | - |
96 | | - |
97 | | - |
98 | | - |
99 | | - |
100 | | - |
101 | | - |
102 | | - |
103 | | - |
104 | static long_int | - |
105 | shr (long_int a, int b) | - |
106 | { | - |
107 | long_int one = 1; | - |
108 | return (-one >> 1 == -1 never executed: return (-one >> 1 == -1 ? a >> b : a / (one << b) - (a % (one << b) < 0)); | 0 |
109 | ? a >> b never executed: return (-one >> 1 == -1 ? a >> b : a / (one << b) - (a % (one << b) < 0)); | 0 |
110 | : a / (one << b) - (a % (one << b) < 0)); never executed: return (-one >> 1 == -1 ? a >> b : a / (one << b) - (a % (one << b) < 0)); | 0 |
111 | } | - |
112 | | - |
113 | | - |
114 | | - |
115 | static long_int const mktime_min | - |
116 | = ((TYPE_SIGNED (time_t) && TYPE_MINIMUM (time_t) < TYPE_MINIMUM (long_int)) | - |
117 | ? TYPE_MINIMUM (long_int) : TYPE_MINIMUM (time_t)); | - |
118 | static long_int const mktime_max | - |
119 | = (TYPE_MAXIMUM (long_int) < TYPE_MAXIMUM (time_t) | - |
120 | ? TYPE_MAXIMUM (long_int) : TYPE_MAXIMUM (time_t)); | - |
121 | | - |
122 | verify (TYPE_IS_INTEGER (time_t)); | - |
123 | | - |
124 | #define EPOCH_YEAR 1970 | - |
125 | #define TM_YEAR_BASE 1900 | - |
126 | verify (TM_YEAR_BASE % 100 == 0); | - |
127 | | - |
128 | | - |
129 | static bool | - |
130 | leapyear (long_int year) | - |
131 | { | - |
132 | | - |
133 | | - |
134 | return never executed: return ((year & 3) == 0 && (year % 100 != 0 || ((year / 100) & 3) == (- (1900 / 100) & 3))); | 0 |
135 | ((year & 3) == 0 never executed: return ((year & 3) == 0 && (year % 100 != 0 || ((year / 100) & 3) == (- (1900 / 100) & 3))); | 0 |
136 | && (year % 100 != 0 never executed: return ((year & 3) == 0 && (year % 100 != 0 || ((year / 100) & 3) == (- (1900 / 100) & 3))); | 0 |
137 | || ((year / 100) & 3) == (- (TM_YEAR_BASE / 100) & 3))); never executed: return ((year & 3) == 0 && (year % 100 != 0 || ((year / 100) & 3) == (- (1900 / 100) & 3))); | 0 |
138 | } | - |
139 | | - |
140 | | - |
141 | #ifndef _LIBC | - |
142 | static | - |
143 | #endif | - |
144 | const unsigned short int __mon_yday[2][13] = | - |
145 | { | - |
146 | | - |
147 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, | - |
148 | | - |
149 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } | - |
150 | }; | - |
151 | | - |
152 | | - |
153 | #ifdef _LIBC | - |
154 | typedef time_t mktime_offset_t; | - |
155 | #else | - |
156 | | - |
157 | | - |
158 | | - |
159 | | - |
160 | # undef __localtime_r | - |
161 | # define __localtime_r localtime_r | - |
162 | # define __mktime_internal mktime_internal | - |
163 | # include "mktime-internal.h" | - |
164 | #endif | - |
165 | | - |
166 | | - |
167 | | - |
168 | static bool | - |
169 | isdst_differ (int a, int b) | - |
170 | { | - |
171 | return (!a != !b) && (0 <= a) && (0 <= b); never executed: return (!a != !b) && (0 <= a) && (0 <= b); | 0 |
172 | } | - |
173 | | - |
174 | | - |
175 | | - |
176 | | - |
177 | | - |
178 | | - |
179 | | - |
180 | | - |
181 | | - |
182 | | - |
183 | static long_int | - |
184 | ydhms_diff (long_int year1, long_int yday1, int hour1, int min1, int sec1, | - |
185 | int year0, int yday0, int hour0, int min0, int sec0) | - |
186 | { | - |
187 | verify (-1 / 2 == 0); | - |
188 | | - |
189 | | - |
190 | | - |
191 | int a4 = shr (year1, 2) + shr (TM_YEAR_BASE, 2) - ! (year1 & 3); | - |
192 | int b4 = shr (year0, 2) + shr (TM_YEAR_BASE, 2) - ! (year0 & 3); | - |
193 | int a100 = a4 / 25 - (a4 % 25 < 0); | - |
194 | int b100 = b4 / 25 - (b4 % 25 < 0); | - |
195 | int a400 = shr (a100, 2); | - |
196 | int b400 = shr (b100, 2); | - |
197 | int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); | - |
198 | | - |
199 | | - |
200 | long_int years = year1 - year0; | - |
201 | long_int days = 365 * years + yday1 - yday0 + intervening_leap_days; | - |
202 | long_int hours = 24 * days + hour1 - hour0; | - |
203 | long_int minutes = 60 * hours + min1 - min0; | - |
204 | long_int seconds = 60 * minutes + sec1 - sec0; | - |
205 | return seconds; never executed: return seconds; | 0 |
206 | } | - |
207 | | - |
208 | | - |
209 | | - |
210 | static long_int | - |
211 | long_int_avg (long_int a, long_int b) | - |
212 | { | - |
213 | return shr (a, 1) + shr (b, 1) + ((a | b) & 1); never executed: return shr (a, 1) + shr (b, 1) + ((a | b) & 1); | 0 |
214 | } | - |
215 | | - |
216 | | - |
217 | | - |
218 | | - |
219 | | - |
220 | | - |
221 | | - |
222 | | - |
223 | | - |
224 | | - |
225 | | - |
226 | static long_int | - |
227 | guess_time_tm (long_int year, long_int yday, int hour, int min, int sec, | - |
228 | long_int t, const struct tm *tp) | - |
229 | { | - |
230 | if (tp)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
231 | { | - |
232 | long_int result; | - |
233 | long_int d = ydhms_diff (year, yday, hour, min, sec, | - |
234 | tp->tm_year, tp->tm_yday, | - |
235 | tp->tm_hour, tp->tm_min, tp->tm_sec); | - |
236 | if (! INT_ADD_WRAPV (t, d, &result))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
237 | return result; never executed: return result; | 0 |
238 | } never executed: end of block | 0 |
239 | | - |
240 | | - |
241 | | - |
242 | | - |
243 | | - |
244 | | - |
245 | return (t < long_int_avg (mktime_min, mktime_max) never executed: return (t < long_int_avg (mktime_min, mktime_max) ? (t <= mktime_min + 1 ? t + 1 : mktime_min) : (mktime_max - 1 <= t ? t - 1 : mktime_max)); | 0 |
246 | ? (t <= mktime_min + 1 ? t + 1 : mktime_min) never executed: return (t < long_int_avg (mktime_min, mktime_max) ? (t <= mktime_min + 1 ? t + 1 : mktime_min) : (mktime_max - 1 <= t ? t - 1 : mktime_max)); | 0 |
247 | : (mktime_max - 1 <= t ? t - 1 : mktime_max)); never executed: return (t < long_int_avg (mktime_min, mktime_max) ? (t <= mktime_min + 1 ? t + 1 : mktime_min) : (mktime_max - 1 <= t ? t - 1 : mktime_max)); | 0 |
248 | } | - |
249 | | - |
250 | | - |
251 | | - |
252 | | - |
253 | static struct tm * | - |
254 | convert_time (struct tm *(*convert) (const time_t *, struct tm *), | - |
255 | long_int t, struct tm *tm) | - |
256 | { | - |
257 | time_t x = t; | - |
258 | return convert (&x, tm); never executed: return convert (&x, tm); | 0 |
259 | } | - |
260 | | - |
261 | | - |
262 | | - |
263 | | - |
264 | | - |
265 | static struct tm * | - |
266 | ranged_convert (struct tm *(*convert) (const time_t *, struct tm *), | - |
267 | long_int *t, struct tm *tp) | - |
268 | { | - |
269 | struct tm *r; | - |
270 | if (*t < mktime_min)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
271 | *t = mktime_min; never executed: *t = mktime_min; | 0 |
272 | else if (mktime_max < *t)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
273 | *t = mktime_max; never executed: *t = mktime_max; | 0 |
274 | r = convert_time (convert, *t, tp); | - |
275 | | - |
276 | if (!r && *t)TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
277 | { | - |
278 | long_int bad = *t; | - |
279 | long_int ok = 0; | - |
280 | | - |
281 | | - |
282 | | - |
283 | | - |
284 | while (true) | - |
285 | { | - |
286 | long_int mid = long_int_avg (ok, bad); | - |
287 | if (mid != ok && mid != bad)TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
288 | break; never executed: break; | 0 |
289 | r = convert_time (convert, mid, tp); | - |
290 | if (r)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
291 | ok = mid; never executed: ok = mid; | 0 |
292 | else | - |
293 | bad = mid; never executed: bad = mid; | 0 |
294 | } | - |
295 | | - |
296 | if (!r && ok)TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
297 | { | - |
298 | | - |
299 | | - |
300 | r = convert_time (convert, ok, tp); | - |
301 | } never executed: end of block | 0 |
302 | } never executed: end of block | 0 |
303 | | - |
304 | return r; never executed: return r; | 0 |
305 | } | - |
306 | | - |
307 | | - |
308 | | - |
309 | | - |
310 | | - |
311 | | - |
312 | | - |
313 | time_t | - |
314 | __mktime_internal (struct tm *tp, | - |
315 | struct tm *(*convert) (const time_t *, struct tm *), | - |
316 | mktime_offset_t *offset) | - |
317 | { | - |
318 | long_int t, gt, t0, t1, t2, dt; | - |
319 | struct tm tm; | - |
320 | | - |
321 | | - |
322 | | - |
323 | | - |
324 | | - |
325 | int remaining_probes = 6; | - |
326 | | - |
327 | | - |
328 | | - |
329 | int sec = tp->tm_sec; | - |
330 | int min = tp->tm_min; | - |
331 | int hour = tp->tm_hour; | - |
332 | int mday = tp->tm_mday; | - |
333 | int mon = tp->tm_mon; | - |
334 | int year_requested = tp->tm_year; | - |
335 | int isdst = tp->tm_isdst; | - |
336 | | - |
337 | | - |
338 | int dst2; | - |
339 | | - |
340 | | - |
341 | int mon_remainder = mon % 12; | - |
342 | int negative_mon_remainder = mon_remainder < 0; | - |
343 | int mon_years = mon / 12 - negative_mon_remainder; | - |
344 | long_int lyear_requested = year_requested; | - |
345 | long_int year = lyear_requested + mon_years; | - |
346 | | - |
347 | | - |
348 | | - |
349 | | - |
350 | | - |
351 | | - |
352 | int mon_yday = ((__mon_yday[leapyear (year)] | - |
353 | [mon_remainder + 12 * negative_mon_remainder]) | - |
354 | - 1); | - |
355 | long_int lmday = mday; | - |
356 | long_int yday = mon_yday + lmday; | - |
357 | | - |
358 | int negative_offset_guess; | - |
359 | | - |
360 | int sec_requested = sec; | - |
361 | | - |
362 | if (LEAP_SECONDS_POSSIBLE) | - |
363 | { | - |
364 | | - |
365 | | - |
366 | if (sec < 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
367 | sec = 0; never executed: sec = 0; | 0 |
368 | if (59 < sec)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
369 | sec = 59; never executed: sec = 59; | 0 |
370 | } never executed: end of block | 0 |
371 | | - |
372 | | - |
373 | | - |
374 | | - |
375 | INT_SUBTRACT_WRAPV (0, *offset, &negative_offset_guess); | - |
376 | t0 = ydhms_diff (year, yday, hour, min, sec, | - |
377 | EPOCH_YEAR - TM_YEAR_BASE, 0, 0, 0, negative_offset_guess); | - |
378 | | - |
379 | | - |
380 | | - |
381 | for (t = t1 = t2 = t0, dst2 = 0; | - |
382 | (gt = guess_time_tm (year, yday, hour, min, sec, t,TRUE | never evaluated | FALSE | never evaluated |
| 0 |
383 | ranged_convert (convert, &t, &tm)),TRUE | never evaluated | FALSE | never evaluated |
| 0 |
384 | t != gt);TRUE | never evaluated | FALSE | never evaluated |
| 0 |
385 | t1 = t2, t2 = t, t = gt, dst2 = tm.tm_isdst != 0) | - |
386 | if (t == t1 && t != t2TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
387 | && (tm.tm_isdst < 0TRUE | never evaluated | FALSE | never evaluated |
| 0 |
388 | || (isdst < 0TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
389 | ? dst2 <= (tm.tm_isdst != 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
390 | : (isdst != 0) != (tm.tm_isdst != 0))))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
391 | | - |
392 | | - |
393 | | - |
394 | | - |
395 | | - |
396 | | - |
397 | | - |
398 | | - |
399 | | - |
400 | goto offset_found; never executed: goto offset_found; | 0 |
401 | else if (--remaining_probes == 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
402 | return -1; never executed: return -1; | 0 |
403 | | - |
404 | | - |
405 | | - |
406 | if (isdst_differ (isdst, tm.tm_isdst))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
407 | { | - |
408 | | - |
409 | | - |
410 | | - |
411 | | - |
412 | | - |
413 | | - |
414 | | - |
415 | | - |
416 | | - |
417 | | - |
418 | | - |
419 | | - |
420 | | - |
421 | | - |
422 | int stride = 601200; | - |
423 | | - |
424 | | - |
425 | | - |
426 | | - |
427 | | - |
428 | | - |
429 | int duration_max = 536454000; | - |
430 | | - |
431 | | - |
432 | | - |
433 | int delta_bound = duration_max / 2 + stride; | - |
434 | | - |
435 | int delta, direction; | - |
436 | | - |
437 | for (delta = stride; delta < delta_bound; delta += stride)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
438 | for (direction = -1; direction <= 1; direction += 2)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
439 | { | - |
440 | long_int ot; | - |
441 | if (! INT_ADD_WRAPV (t, delta * direction, &ot))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
442 | { | - |
443 | struct tm otm; | - |
444 | ranged_convert (convert, &ot, &otm); | - |
445 | if (! isdst_differ (isdst, otm.tm_isdst))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
446 | { | - |
447 | | - |
448 | | - |
449 | t = guess_time_tm (year, yday, hour, min, sec, ot, &otm); | - |
450 | ranged_convert (convert, &t, &tm); | - |
451 | goto offset_found; never executed: goto offset_found; | 0 |
452 | } | - |
453 | } never executed: end of block | 0 |
454 | } never executed: end of block | 0 |
455 | } never executed: end of block | 0 |
456 | | - |
457 | offset_found: code before this statement never executed: offset_found: | 0 |
458 | | - |
459 | | - |
460 | | - |
461 | INT_SUBTRACT_WRAPV (t, t0, &dt); | - |
462 | INT_SUBTRACT_WRAPV (dt, negative_offset_guess, offset); | - |
463 | | - |
464 | if (LEAP_SECONDS_POSSIBLE && sec_requested != tm.tm_sec)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
465 | { | - |
466 | | - |
467 | | - |
468 | long_int sec_adjustment = sec == 0 && tm.tm_sec == 60;TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
469 | sec_adjustment -= sec; | - |
470 | sec_adjustment += sec_requested; | - |
471 | if (INT_ADD_WRAPV (t, sec_adjustment, &t)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
472 | || ! (mktime_min <= t && t <= mktime_max)TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
473 | || ! convert_time (convert, t, &tm))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
474 | return -1; never executed: return -1; | 0 |
475 | } never executed: end of block | 0 |
476 | | - |
477 | *tp = tm; | - |
478 | return t; never executed: return t; | 0 |
479 | } | - |
480 | | - |
481 | #endif /* NEED_MKTIME_WORKING || NEED_MKTIME_INTERNAL || DEBUG_MKTIME */ | - |
482 | | - |
483 | #if NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS || DEBUG_MKTIME | - |
484 | | - |
485 | # if NEED_MKTIME_WORKING || DEBUG_MKTIME | - |
486 | static mktime_offset_t localtime_offset; | - |
487 | # endif | - |
488 | | - |
489 | | - |
490 | time_t | - |
491 | mktime (struct tm *tp) | - |
492 | { | - |
493 | # if NEED_MKTIME_WINDOWS | - |
494 | | - |
495 | | - |
496 | | - |
497 | | - |
498 | | - |
499 | | - |
500 | | - |
501 | | - |
502 | | - |
503 | | - |
504 | | - |
505 | | - |
506 | | - |
507 | | - |
508 | | - |
509 | | - |
510 | | - |
511 | | - |
512 | | - |
513 | | - |
514 | | - |
515 | | - |
516 | const char *tz = getenv ("TZ"); | - |
517 | if (tz != NULL && strchr (tz, '/') != NULL) | - |
518 | _putenv ("TZ="); | - |
519 | # endif | - |
520 | | - |
521 | # if NEED_MKTIME_WORKING || DEBUG_MKTIME | - |
522 | # ifdef _LIBC | - |
523 | | - |
524 | | - |
525 | | - |
526 | __tzset (); | - |
527 | # elif HAVE_TZSET | - |
528 | tzset (); | - |
529 | # endif | - |
530 | | - |
531 | return __mktime_internal (tp, __localtime_r, &localtime_offset); | - |
532 | # else | - |
533 | # undef mktime | - |
534 | return mktime (tp); | - |
535 | # endif | - |
536 | } | - |
537 | | - |
538 | #endif /* NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS || DEBUG_MKTIME */ | - |
539 | | - |
540 | #ifdef weak_alias | - |
541 | weak_alias (mktime, timelocal) | - |
542 | #endif | - |
543 | | - |
544 | #ifdef _LIBC | - |
545 | libc_hidden_def (mktime) | - |
546 | libc_hidden_weak (timelocal) | - |
547 | #endif | - |
548 | | - |
549 | #if DEBUG_MKTIME | - |
550 | | - |
551 | static int | - |
552 | not_equal_tm (const struct tm *a, const struct tm *b) | - |
553 | { | - |
554 | return ((a->tm_sec ^ b->tm_sec) | - |
555 | | (a->tm_min ^ b->tm_min) | - |
556 | | (a->tm_hour ^ b->tm_hour) | - |
557 | | (a->tm_mday ^ b->tm_mday) | - |
558 | | (a->tm_mon ^ b->tm_mon) | - |
559 | | (a->tm_year ^ b->tm_year) | - |
560 | | (a->tm_yday ^ b->tm_yday) | - |
561 | | isdst_differ (a->tm_isdst, b->tm_isdst)); | - |
562 | } | - |
563 | | - |
564 | static void | - |
565 | print_tm (const struct tm *tp) | - |
566 | { | - |
567 | if (tp) | - |
568 | printf ("%04d-%02d-%02d %02d:%02d:%02d yday %03d wday %d isdst %d", | - |
569 | tp->tm_year + TM_YEAR_BASE, tp->tm_mon + 1, tp->tm_mday, | - |
570 | tp->tm_hour, tp->tm_min, tp->tm_sec, | - |
571 | tp->tm_yday, tp->tm_wday, tp->tm_isdst); | - |
572 | else | - |
573 | printf ("0"); | - |
574 | } | - |
575 | | - |
576 | static int | - |
577 | check_result (time_t tk, struct tm tmk, time_t tl, const struct tm *lt) | - |
578 | { | - |
579 | if (tk != tl || !lt || not_equal_tm (&tmk, lt)) | - |
580 | { | - |
581 | printf ("mktime ("); | - |
582 | print_tm (lt); | - |
583 | printf (")\nyields ("); | - |
584 | print_tm (&tmk); | - |
585 | printf (") == %ld, should be %ld\n", (long int) tk, (long int) tl); | - |
586 | return 1; | - |
587 | } | - |
588 | | - |
589 | return 0; | - |
590 | } | - |
591 | | - |
592 | int | - |
593 | main (int argc, char **argv) | - |
594 | { | - |
595 | int status = 0; | - |
596 | struct tm tm, tmk, tml; | - |
597 | struct tm *lt; | - |
598 | time_t tk, tl, tl1; | - |
599 | char trailer; | - |
600 | | - |
601 | | - |
602 | tl = 0; | - |
603 | if (! localtime (&tl)) | - |
604 | { | - |
605 | printf ("localtime (0) fails\n"); | - |
606 | status = 1; | - |
607 | } | - |
608 | | - |
609 | if ((argc == 3 || argc == 4) | - |
610 | && (sscanf (argv[1], "%d-%d-%d%c", | - |
611 | &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &trailer) | - |
612 | == 3) | - |
613 | && (sscanf (argv[2], "%d:%d:%d%c", | - |
614 | &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &trailer) | - |
615 | == 3)) | - |
616 | { | - |
617 | tm.tm_year -= TM_YEAR_BASE; | - |
618 | tm.tm_mon--; | - |
619 | tm.tm_isdst = argc == 3 ? -1 : atoi (argv[3]); | - |
620 | tmk = tm; | - |
621 | tl = mktime (&tmk); | - |
622 | lt = localtime_r (&tl, &tml); | - |
623 | printf ("mktime returns %ld == ", (long int) tl); | - |
624 | print_tm (&tmk); | - |
625 | printf ("\n"); | - |
626 | status = check_result (tl, tmk, tl, lt); | - |
627 | } | - |
628 | else if (argc == 4 || (argc == 5 && strcmp (argv[4], "-") == 0)) | - |
629 | { | - |
630 | time_t from = atol (argv[1]); | - |
631 | time_t by = atol (argv[2]); | - |
632 | time_t to = atol (argv[3]); | - |
633 | | - |
634 | if (argc == 4) | - |
635 | for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1) | - |
636 | { | - |
637 | lt = localtime_r (&tl, &tml); | - |
638 | if (lt) | - |
639 | { | - |
640 | tmk = tml; | - |
641 | tk = mktime (&tmk); | - |
642 | status |= check_result (tk, tmk, tl, &tml); | - |
643 | } | - |
644 | else | - |
645 | { | - |
646 | printf ("localtime_r (%ld) yields 0\n", (long int) tl); | - |
647 | status = 1; | - |
648 | } | - |
649 | tl1 = tl + by; | - |
650 | if ((tl1 < tl) != (by < 0)) | - |
651 | break; | - |
652 | } | - |
653 | else | - |
654 | for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1) | - |
655 | { | - |
656 | | - |
657 | lt = localtime_r (&tl, &tml); | - |
658 | if (lt) | - |
659 | { | - |
660 | tmk = tml; | - |
661 | tk = tl; | - |
662 | status |= check_result (tk, tmk, tl, &tml); | - |
663 | } | - |
664 | else | - |
665 | { | - |
666 | printf ("localtime_r (%ld) yields 0\n", (long int) tl); | - |
667 | status = 1; | - |
668 | } | - |
669 | tl1 = tl + by; | - |
670 | if ((tl1 < tl) != (by < 0)) | - |
671 | break; | - |
672 | } | - |
673 | } | - |
674 | else | - |
675 | printf ("Usage:\ | - |
676 | \t%s YYYY-MM-DD HH:MM:SS [ISDST] # Test given time.\n\ | - |
677 | \t%s FROM BY TO # Test values FROM, FROM+BY, ..., TO.\n\ | - |
678 | \t%s FROM BY TO - # Do not test those values (for benchmark).\n", | - |
679 | argv[0], argv[0], argv[0]); | - |
680 | | - |
681 | return status; | - |
682 | } | - |
683 | | - |
684 | #endif /* DEBUG_MKTIME */ | - |
685 | | - |
686 | | - |
687 | | - |
688 | | - |
689 | | - |
690 | | - |
| | |