OpenCoverage

mktime.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/lib/mktime.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* Convert a 'struct tm' to a time_t value.-
2 Copyright (C) 1993-2018 Free Software Foundation, Inc.-
3 This file is part of the GNU C Library.-
4 Contributed by Paul Eggert <eggert@twinsun.com>.-
5-
6 The GNU C Library is free software; you can redistribute it and/or-
7 modify it under the terms of the GNU General Public-
8 License as published by the Free Software Foundation; either-
9 version 3 of the License, or (at your option) any later version.-
10-
11 The GNU C Library is distributed in the hope that it will be useful,-
12 but WITHOUT ANY WARRANTY; without even the implied warranty of-
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU-
14 General Public License for more details.-
15-
16 You should have received a copy of the GNU General Public-
17 License along with the GNU C Library; if not, see-
18 <https://www.gnu.org/licenses/>. */-
19-
20/* Define this to 1 to have a standalone program to test this implementation of-
21 mktime. */-
22#ifndef DEBUG_MKTIME-
23# define DEBUG_MKTIME 0-
24#endif-
25-
26/* The following macros influence what gets defined when this file is compiled:-
27-
28 Macro/expression Which gnulib module This compilation unit-
29 should define-
30-
31 NEED_MKTIME_WORKING mktime rpl_mktime-
32 || NEED_MKTIME_WINDOWS-
33-
34 NEED_MKTIME_INTERNAL mktime-internal mktime_internal-
35-
36 DEBUG_MKTIME (defined manually) my_mktime, main-
37 */-
38-
39#if !defined _LIBC && !DEBUG_MKTIME-
40# include <config.h>-
41#endif-
42-
43/* Assume that leap seconds are possible, unless told otherwise.-
44 If the host has a 'zic' command with a '-L leapsecondfilename' option,-
45 then it supports leap seconds; otherwise it probably doesn't. */-
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/* Make it work even if the system's libc has its own mktime routine. */-
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/* A signed type that can represent an integer number of years-
75 multiplied by three times the number of seconds in a year. It is-
76 needed when converting a tm_year value times the number of seconds-
77 in a year. The factor of three comes because these products need-
78 to be subtracted from each other, and sometimes with an offset-
79 added to them, without worrying about overflow.-
80-
81 Much of the code uses long_int to represent time_t values, to-
82 lessen the hassle of dealing with platforms where time_t is-
83 unsigned, and because long_int should suffice to represent all-
84 time_t values that mktime can generate even on platforms where-
85 time_t is excessively wide. */-
86-
87#if INT_MAX <= LONG_MAX / 3 / 366 / 24 / 60 / 60-
88typedef long int long_int;-
89#else-
90typedef long long int long_int;-
91#endif-
92verify (INT_MAX <= TYPE_MAXIMUM (long_int) / 3 / 366 / 24 / 60 / 60);-
93-
94/* Shift A right by B bits portably, by dividing A by 2**B and-
95 truncating towards minus infinity. B should be in the range 0 <= B-
96 <= LONG_INT_BITS - 2, where LONG_INT_BITS is the number of useful-
97 bits in a long_int. LONG_INT_BITS is at least 32.-
98-
99 ISO C99 says that A >> B is implementation-defined if A < 0. Some-
100 implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift-
101 right in the usual way when A < 0, so SHR falls back on division if-
102 ordinary A >> B doesn't seem to be the usual signed shift. */-
103-
104static long_int-
105shr (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/* Bounds for the intersection of time_t and long_int. */-
114-
115static 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));-
118static long_int const mktime_max-
119 = (TYPE_MAXIMUM (long_int) < TYPE_MAXIMUM (time_t)-
120 ? TYPE_MAXIMUM (long_int) : TYPE_MAXIMUM (time_t));-
121-
122verify (TYPE_IS_INTEGER (time_t));-
123-
124#define EPOCH_YEAR 1970-
125#define TM_YEAR_BASE 1900-
126verify (TM_YEAR_BASE % 100 == 0);-
127-
128/* Is YEAR + TM_YEAR_BASE a leap year? */-
129static bool-
130leapyear (long_int year)-
131{-
132 /* Don't add YEAR to TM_YEAR_BASE, as that might overflow.-
133 Also, work even if YEAR is negative. */-
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/* How many days come before each month (0-12). */-
141#ifndef _LIBC-
142static-
143#endif-
144const unsigned short int __mon_yday[2][13] =-
145 {-
146 /* Normal years. */-
147 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },-
148 /* Leap years. */-
149 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }-
150 };-
151-
152-
153#ifdef _LIBC-
154typedef time_t mktime_offset_t;-
155#else-
156/* Portable standalone applications should supply a <time.h> that-
157 declares a POSIX-compliant localtime_r, for the benefit of older-
158 implementations that lack localtime_r or have a nonstandard one.-
159 See the gnulib time_r module for one way to implement this. */-
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/* Do the values A and B differ according to the rules for tm_isdst?-
167 A and B differ if one is zero and the other positive. */-
168static bool-
169isdst_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/* Return an integer value measuring (YEAR1-YDAY1 HOUR1:MIN1:SEC1) --
175 (YEAR0-YDAY0 HOUR0:MIN0:SEC0) in seconds, assuming that the clocks-
176 were not adjusted between the timestamps.-
177-
178 The YEAR values uses the same numbering as TP->tm_year. Values-
179 need not be in the usual range. However, YEAR1 must not overflow-
180 when multiplied by three times the number of seconds in a year, and-
181 likewise for YDAY1 and three times the number of seconds in a day. */-
182-
183static long_int-
184ydhms_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 /* Compute intervening leap days correctly even if year is negative.-
190 Take care to avoid integer overflow here. */-
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 /* Compute the desired time without overflowing. */-
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/* Return the average of A and B, even if A + B would overflow.-
209 Round toward positive infinity. */-
210static long_int-
211long_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/* Return a time_t value corresponding to (YEAR-YDAY HOUR:MIN:SEC),-
217 assuming that T corresponds to *TP and that no clock adjustments-
218 occurred between *TP and the desired time.-
219 Although T and the returned value are of type long_int,-
220 they represent time_t values and must be in time_t range.-
221 If TP is null, return a value not equal to T; this avoids false matches.-
222 YEAR and YDAY must not be so large that multiplying them by three times the-
223 number of seconds in a year (or day, respectively) would overflow long_int.-
224 If the returned value would be out of range, yield the minimal or-
225 maximal in-range value, except do not yield a value equal to T. */-
226static long_int-
227guess_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)
tpDescription
TRUEnever evaluated
FALSEnever 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))
! __builtin_ad...t, d, &result)Description
TRUEnever evaluated
FALSEnever evaluated
0
237 return result;
never executed: return result;
0
238 }
never executed: end of block
0
239-
240 /* Overflow occurred one way or another. Return the nearest result-
241 that is actually in range, except don't report a zero difference-
242 if the actual difference is nonzero, as that would cause a false-
243 match; and don't oscillate between two values, as that would-
244 confuse the spring-forward gap detector. */-
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/* Use CONVERT to convert T to a struct tm value in *TM. T must be in-
251 range for time_t. Return TM if successful, NULL if T is out of-
252 range for CONVERT. */-
253static struct tm *-
254convert_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/* Use CONVERT to convert *T to a broken down time in *TP.-
262 If *T is out of range for conversion, adjust it so that-
263 it is the nearest in-range value and then convert that.-
264 A value is in range if it fits in both time_t and long_int. */-
265static struct tm *-
266ranged_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)
*t < mktime_minDescription
TRUEnever evaluated
FALSEnever evaluated
0
271 *t = mktime_min;
never executed: *t = mktime_min;
0
272 else if (mktime_max < *t)
mktime_max < *tDescription
TRUEnever evaluated
FALSEnever evaluated
0
273 *t = mktime_max;
never executed: *t = mktime_max;
0
274 r = convert_time (convert, *t, tp);-
275-
276 if (!r && *t)
!rDescription
TRUEnever evaluated
FALSEnever evaluated
*tDescription
TRUEnever evaluated
FALSEnever evaluated
0
277 {-
278 long_int bad = *t;-
279 long_int ok = 0;-
280-
281 /* BAD is a known unconvertible value, and OK is a known good one.-
282 Use binary search to narrow the range between BAD and OK until-
283 they differ by 1. */-
284 while (true)-
285 {-
286 long_int mid = long_int_avg (ok, bad);-
287 if (mid != ok && mid != bad)
mid != okDescription
TRUEnever evaluated
FALSEnever evaluated
mid != badDescription
TRUEnever evaluated
FALSEnever evaluated
0
288 break;
never executed: break;
0
289 r = convert_time (convert, mid, tp);-
290 if (r)
rDescription
TRUEnever evaluated
FALSEnever 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)
!rDescription
TRUEnever evaluated
FALSEnever evaluated
okDescription
TRUEnever evaluated
FALSEnever evaluated
0
297 {-
298 /* The last conversion attempt failed;-
299 revert to the most recent successful attempt. */-
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/* Convert *TP to a time_t value, inverting-
308 the monotonic and mostly-unit-linear conversion function CONVERT.-
309 Use *OFFSET to keep track of a guess at the offset of the result,-
310 compared to what the result would be for UTC without leap seconds.-
311 If *OFFSET's guess is correct, only one CONVERT call is needed.-
312 This function is external because it is used also by timegm.c. */-
313time_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 /* The maximum number of probes (calls to CONVERT) should be enough-
322 to handle any combinations of time zone rule changes, solar time,-
323 leap seconds, and oscillations around a spring-forward gap.-
324 POSIX.1 prohibits leap seconds, but some hosts have them anyway. */-
325 int remaining_probes = 6;-
326-
327 /* Time requested. Copy it in case CONVERT modifies *TP; this can-
328 occur if TP is localtime's returned value and CONVERT is localtime. */-
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 /* 1 if the previous probe was DST. */-
338 int dst2;-
339-
340 /* Ensure that mon is in range, and set year accordingly. */-
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 /* The other values need not be in range:-
348 the remaining code handles overflows correctly. */-
349-
350 /* Calculate day of year from year, month, and day of month.-
351 The result need not be in range. */-
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 /* Handle out-of-range seconds specially,-
365 since ydhms_tm_diff assumes every minute has 60 seconds. */-
366 if (sec < 0)
sec < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
367 sec = 0;
never executed: sec = 0;
0
368 if (59 < sec)
59 < secDescription
TRUEnever evaluated
FALSEnever evaluated
0
369 sec = 59;
never executed: sec = 59;
0
370 }
never executed: end of block
0
371-
372 /* Invert CONVERT by probing. First assume the same offset as last-
373 time. */-
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 /* Repeatedly use the error to improve the guess. */-
380-
381 for (t = t1 = t2 = t0, dst2 = 0;-
382 (gt = guess_time_tm (year, yday, hour, min, sec, t,
(gt = guess_ti...tm)), t != gt)Description
TRUEnever evaluated
FALSEnever evaluated
0
383 ranged_convert (convert, &t, &tm)),
(gt = guess_ti...tm)), t != gt)Description
TRUEnever evaluated
FALSEnever evaluated
0
384 t != gt);
(gt = guess_ti...tm)), t != gt)Description
TRUEnever evaluated
FALSEnever evaluated
0
385 t1 = t2, t2 = t, t = gt, dst2 = tm.tm_isdst != 0)-
386 if (t == t1 && t != t2
t == t1Description
TRUEnever evaluated
FALSEnever evaluated
t != t2Description
TRUEnever evaluated
FALSEnever evaluated
0
387 && (tm.tm_isdst < 0
tm.tm_isdst < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
388 || (isdst < 0
isdst < 0Description
TRUEnever evaluated
FALSEnever evaluated
(isdst < 0 ? d...m_isdst != 0))Description
TRUEnever evaluated
FALSEnever evaluated
0
389 ? dst2 <= (tm.tm_isdst != 0)
(isdst < 0 ? d...m_isdst != 0))Description
TRUEnever evaluated
FALSEnever evaluated
0
390 : (isdst != 0) != (tm.tm_isdst != 0))))
(isdst < 0 ? d...m_isdst != 0))Description
TRUEnever evaluated
FALSEnever evaluated
0
391 /* We can't possibly find a match, as we are oscillating-
392 between two values. The requested time probably falls-
393 within a spring-forward gap of size GT - T. Follow the common-
394 practice in this case, which is to return a time that is GT - T-
395 away from the requested time, preferring a time whose-
396 tm_isdst differs from the requested value. (If no tm_isdst-
397 was requested and only one of the two values has a nonzero-
398 tm_isdst, prefer that value.) In practice, this is more-
399 useful than returning -1. */-
400 goto offset_found;
never executed: goto offset_found;
0
401 else if (--remaining_probes == 0)
--remaining_probes == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
402 return -1;
never executed: return -1;
0
403-
404 /* We have a match. Check whether tm.tm_isdst has the requested-
405 value, if any. */-
406 if (isdst_differ (isdst, tm.tm_isdst))
isdst_differ (..., tm.tm_isdst)Description
TRUEnever evaluated
FALSEnever evaluated
0
407 {-
408 /* tm.tm_isdst has the wrong value. Look for a neighboring-
409 time with the right value, and use its UTC offset.-
410-
411 Heuristic: probe the adjacent timestamps in both directions,-
412 looking for the desired isdst. This should work for all real-
413 time zone histories in the tz database. */-
414-
415 /* Distance between probes when looking for a DST boundary. In-
416 tzdata2003a, the shortest period of DST is 601200 seconds-
417 (e.g., America/Recife starting 2000-10-08 01:00), and the-
418 shortest period of non-DST surrounded by DST is 694800-
419 seconds (Africa/Tunis starting 1943-04-17 01:00). Use the-
420 minimum of these two values, so we don't miss these short-
421 periods when probing. */-
422 int stride = 601200;-
423-
424 /* The longest period of DST in tzdata2003a is 536454000 seconds-
425 (e.g., America/Jujuy starting 1946-10-01 01:00). The longest-
426 period of non-DST is much longer, but it makes no real sense-
427 to search for more than a year of non-DST, so use the DST-
428 max. */-
429 int duration_max = 536454000;-
430-
431 /* Search in both directions, so the maximum distance is half-
432 the duration; add the stride to avoid off-by-1 problems. */-
433 int delta_bound = duration_max / 2 + stride;-
434-
435 int delta, direction;-
436-
437 for (delta = stride; delta < delta_bound; delta += stride)
delta < delta_boundDescription
TRUEnever evaluated
FALSEnever evaluated
0
438 for (direction = -1; direction <= 1; direction += 2)
direction <= 1Description
TRUEnever evaluated
FALSEnever evaluated
0
439 {-
440 long_int ot;-
441 if (! INT_ADD_WRAPV (t, delta * direction, &ot))
! __builtin_ad...irection, &ot)Description
TRUEnever evaluated
FALSEnever evaluated
0
442 {-
443 struct tm otm;-
444 ranged_convert (convert, &ot, &otm);-
445 if (! isdst_differ (isdst, otm.tm_isdst))
! isdst_differ... otm.tm_isdst)Description
TRUEnever evaluated
FALSEnever evaluated
0
446 {-
447 /* We found the desired tm_isdst.-
448 Extrapolate back to the desired time. */-
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 /* Set *OFFSET to the low-order bits of T - T0 - NEGATIVE_OFFSET_GUESS.-
459 This is just a heuristic to speed up the next mktime call, and-
460 correctness is unaffected if integer overflow occurs here. */-
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)
sec_requested != tm.tm_secDescription
TRUEnever evaluated
FALSEnever evaluated
0
465 {-
466 /* Adjust time to reflect the tm_sec requested, not the normalized value.-
467 Also, repair any damage from a false match due to a leap second. */-
468 long_int sec_adjustment = sec == 0 && tm.tm_sec == 60;
sec == 0Description
TRUEnever evaluated
FALSEnever evaluated
tm.tm_sec == 60Description
TRUEnever evaluated
FALSEnever evaluated
0
469 sec_adjustment -= sec;-
470 sec_adjustment += sec_requested;-
471 if (INT_ADD_WRAPV (t, sec_adjustment, &t)
__builtin_add_...djustment, &t)Description
TRUEnever evaluated
FALSEnever evaluated
0
472 || ! (mktime_min <= t && t <= mktime_max)
mktime_min <= tDescription
TRUEnever evaluated
FALSEnever evaluated
t <= mktime_maxDescription
TRUEnever evaluated
FALSEnever evaluated
0
473 || ! convert_time (convert, t, &tm))
! convert_time...nvert, t, &tm)Description
TRUEnever evaluated
FALSEnever 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-
486static mktime_offset_t localtime_offset;-
487# endif-
488-
489/* Convert *TP to a time_t value. */-
490time_t-
491mktime (struct tm *tp)-
492{-
493# if NEED_MKTIME_WINDOWS-
494 /* Rectify the value of the environment variable TZ.-
495 There are four possible kinds of such values:-
496 - Traditional US time zone names, e.g. "PST8PDT". Syntax: see-
497 <https://msdn.microsoft.com/en-us/library/90s5c885.aspx>-
498 - Time zone names based on geography, that contain one or more-
499 slashes, e.g. "Europe/Moscow".-
500 - Time zone names based on geography, without slashes, e.g.-
501 "Singapore".-
502 - Time zone names that contain explicit DST rules. Syntax: see-
503 <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03>-
504 The Microsoft CRT understands only the first kind. It produces incorrect-
505 results if the value of TZ is of the other kinds.-
506 But in a Cygwin environment, /etc/profile.d/tzset.sh sets TZ to a value-
507 of the second kind for most geographies, or of the first kind in a few-
508 other geographies. If it is of the second kind, neutralize it. For the-
509 Microsoft CRT, an absent or empty TZ means the time zone that the user-
510 has set in the Windows Control Panel.-
511 If the value of TZ is of the third or fourth kind -- Cygwin programs-
512 understand these syntaxes as well --, it does not matter whether we-
513 neutralize it or not, since these values occur only when a Cygwin user-
514 has set TZ explicitly; this case is 1. rare and 2. under the user's-
515 responsibility. */-
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 /* POSIX.1 8.1.1 requires that whenever mktime() is called, the-
524 time zone names contained in the external variable 'tzname' shall-
525 be set as if the tzset() function had been called. */-
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-
541weak_alias (mktime, timelocal)-
542#endif-
543-
544#ifdef _LIBC-
545libc_hidden_def (mktime)-
546libc_hidden_weak (timelocal)-
547#endif-
548 -
549#if DEBUG_MKTIME-
550-
551static int-
552not_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-
564static void-
565print_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-
576static int-
577check_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-
592int-
593main (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 /* Sanity check, plus call tzset. */-
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 /* Null benchmark. */-
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/*-
687Local Variables:-
688compile-command: "gcc -DDEBUG_MKTIME -I. -Wall -W -O2 -g mktime.c -o mktime"-
689End:-
690*/-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2