summaryrefslogtreecommitdiffstats
path: root/gl/mktime.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/mktime.c')
-rw-r--r--gl/mktime.c378
1 files changed, 191 insertions, 187 deletions
diff --git a/gl/mktime.c b/gl/mktime.c
index 0f30d43..b0324b8 100644
--- a/gl/mktime.c
+++ b/gl/mktime.c
@@ -1,5 +1,5 @@
1/* Convert a `struct tm' to a time_t value. 1/* Convert a `struct tm' to a time_t value.
2 Copyright (C) 1993-1999, 2002-2005, 2006, 2007 Free Software Foundation, Inc. 2 Copyright (C) 1993-1999, 2002-2007, 2009-2010 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 Contributed by Paul Eggert <eggert@twinsun.com>. 4 Contributed by Paul Eggert <eggert@twinsun.com>.
5 5
@@ -36,7 +36,7 @@
36 36
37#include <limits.h> 37#include <limits.h>
38 38
39#include <string.h> /* For the real memcpy prototype. */ 39#include <string.h> /* For the real memcpy prototype. */
40 40
41#if DEBUG 41#if DEBUG
42# include <stdio.h> 42# include <stdio.h>
@@ -55,9 +55,9 @@
55 implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift 55 implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift
56 right in the usual way when A < 0, so SHR falls back on division if 56 right in the usual way when A < 0, so SHR falls back on division if
57 ordinary A >> B doesn't seem to be the usual signed shift. */ 57 ordinary A >> B doesn't seem to be the usual signed shift. */
58#define SHR(a, b) \ 58#define SHR(a, b) \
59 (-1 >> 1 == -1 \ 59 (-1 >> 1 == -1 \
60 ? (a) >> (b) \ 60 ? (a) >> (b) \
61 : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0)) 61 : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
62 62
63/* The extra casts in the following macros work around compiler bugs, 63/* The extra casts in the following macros work around compiler bugs,
@@ -84,14 +84,14 @@
84 your host. */ 84 your host. */
85#define TYPE_MINIMUM(t) \ 85#define TYPE_MINIMUM(t) \
86 ((t) (! TYPE_SIGNED (t) \ 86 ((t) (! TYPE_SIGNED (t) \
87 ? (t) 0 \ 87 ? (t) 0 \
88 : TYPE_SIGNED_MAGNITUDE (t) \ 88 : TYPE_SIGNED_MAGNITUDE (t) \
89 ? ~ (t) 0 \ 89 ? ~ (t) 0 \
90 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))) 90 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))
91#define TYPE_MAXIMUM(t) \ 91#define TYPE_MAXIMUM(t) \
92 ((t) (! TYPE_SIGNED (t) \ 92 ((t) (! TYPE_SIGNED (t) \
93 ? (t) -1 \ 93 ? (t) -1 \
94 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))) 94 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
95 95
96#ifndef TIME_T_MIN 96#ifndef TIME_T_MIN
97# define TIME_T_MIN TYPE_MINIMUM (time_t) 97# define TIME_T_MIN TYPE_MINIMUM (time_t)
@@ -123,7 +123,7 @@ leapyear (long int year)
123 return 123 return
124 ((year & 3) == 0 124 ((year & 3) == 0
125 && (year % 100 != 0 125 && (year % 100 != 0
126 || ((year / 100) & 3) == (- (TM_YEAR_BASE / 100) & 3))); 126 || ((year / 100) & 3) == (- (TM_YEAR_BASE / 100) & 3)));
127} 127}
128 128
129/* How many days come before each month (0-12). */ 129/* How many days come before each month (0-12). */
@@ -147,6 +147,7 @@ const unsigned short int __mon_yday[2][13] =
147# undef __localtime_r 147# undef __localtime_r
148# define __localtime_r localtime_r 148# define __localtime_r localtime_r
149# define __mktime_internal mktime_internal 149# define __mktime_internal mktime_internal
150# include "mktime-internal.h"
150#endif 151#endif
151 152
152/* Return an integer value measuring (YEAR1-YDAY1 HOUR1:MIN1:SEC1) - 153/* Return an integer value measuring (YEAR1-YDAY1 HOUR1:MIN1:SEC1) -
@@ -162,11 +163,14 @@ const unsigned short int __mon_yday[2][13] =
162 163
163static inline time_t 164static inline time_t
164ydhms_diff (long int year1, long int yday1, int hour1, int min1, int sec1, 165ydhms_diff (long int year1, long int yday1, int hour1, int min1, int sec1,
165 int year0, int yday0, int hour0, int min0, int sec0) 166 int year0, int yday0, int hour0, int min0, int sec0)
166{ 167{
167 verify (C99_integer_division, -1 / 2 == 0); 168 verify (C99_integer_division, -1 / 2 == 0);
169#if 0 /* This assertion fails on 32-bit systems with 64-bit time_t, such as
170 NetBSD 5 on i386. */
168 verify (long_int_year_and_yday_are_wide_enough, 171 verify (long_int_year_and_yday_are_wide_enough,
169 INT_MAX <= LONG_MAX / 2 || TIME_T_MAX <= UINT_MAX); 172 INT_MAX <= LONG_MAX / 2 || TIME_T_MAX <= UINT_MAX);
173#endif
170 174
171 /* Compute intervening leap days correctly even if year is negative. 175 /* Compute intervening leap days correctly even if year is negative.
172 Take care to avoid integer overflow here. */ 176 Take care to avoid integer overflow here. */
@@ -198,16 +202,16 @@ ydhms_diff (long int year1, long int yday1, int hour1, int min1, int sec1,
198 yield a value equal to *T. */ 202 yield a value equal to *T. */
199static time_t 203static time_t
200guess_time_tm (long int year, long int yday, int hour, int min, int sec, 204guess_time_tm (long int year, long int yday, int hour, int min, int sec,
201 const time_t *t, const struct tm *tp) 205 const time_t *t, const struct tm *tp)
202{ 206{
203 if (tp) 207 if (tp)
204 { 208 {
205 time_t d = ydhms_diff (year, yday, hour, min, sec, 209 time_t d = ydhms_diff (year, yday, hour, min, sec,
206 tp->tm_year, tp->tm_yday, 210 tp->tm_year, tp->tm_yday,
207 tp->tm_hour, tp->tm_min, tp->tm_sec); 211 tp->tm_hour, tp->tm_min, tp->tm_sec);
208 time_t t1 = *t + d; 212 time_t t1 = *t + d;
209 if ((t1 < *t) == (TYPE_SIGNED (time_t) ? d < 0 : TIME_T_MAX / 2 < d)) 213 if ((t1 < *t) == (TYPE_SIGNED (time_t) ? d < 0 : TIME_T_MAX / 2 < d))
210 return t1; 214 return t1;
211 } 215 }
212 216
213 /* Overflow occurred one way or another. Return the nearest result 217 /* Overflow occurred one way or another. Return the nearest result
@@ -216,8 +220,8 @@ guess_time_tm (long int year, long int yday, int hour, int min, int sec,
216 match; and don't oscillate between two values, as that would 220 match; and don't oscillate between two values, as that would
217 confuse the spring-forward gap detector. */ 221 confuse the spring-forward gap detector. */
218 return (*t < TIME_T_MIDPOINT 222 return (*t < TIME_T_MIDPOINT
219 ? (*t <= TIME_T_MIN + 1 ? *t + 1 : TIME_T_MIN) 223 ? (*t <= TIME_T_MIN + 1 ? *t + 1 : TIME_T_MIN)
220 : (TIME_T_MAX - 1 <= *t ? *t - 1 : TIME_T_MAX)); 224 : (TIME_T_MAX - 1 <= *t ? *t - 1 : TIME_T_MAX));
221} 225}
222 226
223/* Use CONVERT to convert *T to a broken down time in *TP. 227/* Use CONVERT to convert *T to a broken down time in *TP.
@@ -225,7 +229,7 @@ guess_time_tm (long int year, long int yday, int hour, int min, int sec,
225 it is the nearest in-range value and then convert that. */ 229 it is the nearest in-range value and then convert that. */
226static struct tm * 230static struct tm *
227ranged_convert (struct tm *(*convert) (const time_t *, struct tm *), 231ranged_convert (struct tm *(*convert) (const time_t *, struct tm *),
228 time_t *t, struct tm *tp) 232 time_t *t, struct tm *tp)
229{ 233{
230 struct tm *r = convert (t, tp); 234 struct tm *r = convert (t, tp);
231 235
@@ -235,27 +239,27 @@ ranged_convert (struct tm *(*convert) (const time_t *, struct tm *),
235 time_t ok = 0; 239 time_t ok = 0;
236 240
237 /* BAD is a known unconvertible time_t, and OK is a known good one. 241 /* BAD is a known unconvertible time_t, and OK is a known good one.
238 Use binary search to narrow the range between BAD and OK until 242 Use binary search to narrow the range between BAD and OK until
239 they differ by 1. */ 243 they differ by 1. */
240 while (bad != ok + (bad < 0 ? -1 : 1)) 244 while (bad != ok + (bad < 0 ? -1 : 1))
241 { 245 {
242 time_t mid = *t = (bad < 0 246 time_t mid = *t = (bad < 0
243 ? bad + ((ok - bad) >> 1) 247 ? bad + ((ok - bad) >> 1)
244 : ok + ((bad - ok) >> 1)); 248 : ok + ((bad - ok) >> 1));
245 r = convert (t, tp); 249 r = convert (t, tp);
246 if (r) 250 if (r)
247 ok = mid; 251 ok = mid;
248 else 252 else
249 bad = mid; 253 bad = mid;
250 } 254 }
251 255
252 if (!r && ok) 256 if (!r && ok)
253 { 257 {
254 /* The last conversion attempt failed; 258 /* The last conversion attempt failed;
255 revert to the most recent successful attempt. */ 259 revert to the most recent successful attempt. */
256 *t = ok; 260 *t = ok;
257 r = convert (t, tp); 261 r = convert (t, tp);
258 } 262 }
259 } 263 }
260 264
261 return r; 265 return r;
@@ -270,8 +274,8 @@ ranged_convert (struct tm *(*convert) (const time_t *, struct tm *),
270 This function is external because it is used also by timegm.c. */ 274 This function is external because it is used also by timegm.c. */
271time_t 275time_t
272__mktime_internal (struct tm *tp, 276__mktime_internal (struct tm *tp,
273 struct tm *(*convert) (const time_t *, struct tm *), 277 struct tm *(*convert) (const time_t *, struct tm *),
274 time_t *offset) 278 time_t *offset)
275{ 279{
276 time_t t, gt, t0, t1, t2; 280 time_t t, gt, t0, t1, t2;
277 struct tm tm; 281 struct tm tm;
@@ -292,7 +296,7 @@ __mktime_internal (struct tm *tp,
292 int year_requested = tp->tm_year; 296 int year_requested = tp->tm_year;
293 /* Normalize the value. */ 297 /* Normalize the value. */
294 int isdst = ((tp->tm_isdst >> (8 * sizeof (tp->tm_isdst) - 1)) 298 int isdst = ((tp->tm_isdst >> (8 * sizeof (tp->tm_isdst) - 1))
295 | (tp->tm_isdst != 0)); 299 | (tp->tm_isdst != 0));
296 300
297 /* 1 if the previous probe was DST. */ 301 /* 1 if the previous probe was DST. */
298 int dst2; 302 int dst2;
@@ -312,8 +316,8 @@ __mktime_internal (struct tm *tp,
312 /* Calculate day of year from year, month, and day of month. 316 /* Calculate day of year from year, month, and day of month.
313 The result need not be in range. */ 317 The result need not be in range. */
314 int mon_yday = ((__mon_yday[leapyear (year)] 318 int mon_yday = ((__mon_yday[leapyear (year)]
315 [mon_remainder + 12 * negative_mon_remainder]) 319 [mon_remainder + 12 * negative_mon_remainder])
316 - 1); 320 - 1);
317 long int lmday = mday; 321 long int lmday = mday;
318 long int yday = mon_yday + lmday; 322 long int yday = mon_yday + lmday;
319 323
@@ -324,33 +328,33 @@ __mktime_internal (struct tm *tp,
324 if (LEAP_SECONDS_POSSIBLE) 328 if (LEAP_SECONDS_POSSIBLE)
325 { 329 {
326 /* Handle out-of-range seconds specially, 330 /* Handle out-of-range seconds specially,
327 since ydhms_tm_diff assumes every minute has 60 seconds. */ 331 since ydhms_tm_diff assumes every minute has 60 seconds. */
328 if (sec < 0) 332 if (sec < 0)
329 sec = 0; 333 sec = 0;
330 if (59 < sec) 334 if (59 < sec)
331 sec = 59; 335 sec = 59;
332 } 336 }
333 337
334 /* Invert CONVERT by probing. First assume the same offset as last 338 /* Invert CONVERT by probing. First assume the same offset as last
335 time. */ 339 time. */
336 340
337 t0 = ydhms_diff (year, yday, hour, min, sec, 341 t0 = ydhms_diff (year, yday, hour, min, sec,
338 EPOCH_YEAR - TM_YEAR_BASE, 0, 0, 0, - guessed_offset); 342 EPOCH_YEAR - TM_YEAR_BASE, 0, 0, 0, - guessed_offset);
339 343
340 if (TIME_T_MAX / INT_MAX / 366 / 24 / 60 / 60 < 3) 344 if (TIME_T_MAX / INT_MAX / 366 / 24 / 60 / 60 < 3)
341 { 345 {
342 /* time_t isn't large enough to rule out overflows, so check 346 /* time_t isn't large enough to rule out overflows, so check
343 for major overflows. A gross check suffices, since if t0 347 for major overflows. A gross check suffices, since if t0
344 has overflowed, it is off by a multiple of TIME_T_MAX - 348 has overflowed, it is off by a multiple of TIME_T_MAX -
345 TIME_T_MIN + 1. So ignore any component of the difference 349 TIME_T_MIN + 1. So ignore any component of the difference
346 that is bounded by a small value. */ 350 that is bounded by a small value. */
347 351
348 /* Approximate log base 2 of the number of time units per 352 /* Approximate log base 2 of the number of time units per
349 biennium. A biennium is 2 years; use this unit instead of 353 biennium. A biennium is 2 years; use this unit instead of
350 years to avoid integer overflow. For example, 2 average 354 years to avoid integer overflow. For example, 2 average
351 Gregorian years are 2 * 365.2425 * 24 * 60 * 60 seconds, 355 Gregorian years are 2 * 365.2425 * 24 * 60 * 60 seconds,
352 which is 63113904 seconds, and rint (log2 (63113904)) is 356 which is 63113904 seconds, and rint (log2 (63113904)) is
353 26. */ 357 26. */
354 int ALOG2_SECONDS_PER_BIENNIUM = 26; 358 int ALOG2_SECONDS_PER_BIENNIUM = 26;
355 int ALOG2_MINUTES_PER_BIENNIUM = 20; 359 int ALOG2_MINUTES_PER_BIENNIUM = 20;
356 int ALOG2_HOURS_PER_BIENNIUM = 14; 360 int ALOG2_HOURS_PER_BIENNIUM = 14;
@@ -358,64 +362,64 @@ __mktime_internal (struct tm *tp,
358 int LOG2_YEARS_PER_BIENNIUM = 1; 362 int LOG2_YEARS_PER_BIENNIUM = 1;
359 363
360 int approx_requested_biennia = 364 int approx_requested_biennia =
361 (SHR (year_requested, LOG2_YEARS_PER_BIENNIUM) 365 (SHR (year_requested, LOG2_YEARS_PER_BIENNIUM)
362 - SHR (EPOCH_YEAR - TM_YEAR_BASE, LOG2_YEARS_PER_BIENNIUM) 366 - SHR (EPOCH_YEAR - TM_YEAR_BASE, LOG2_YEARS_PER_BIENNIUM)
363 + SHR (mday, ALOG2_DAYS_PER_BIENNIUM) 367 + SHR (mday, ALOG2_DAYS_PER_BIENNIUM)
364 + SHR (hour, ALOG2_HOURS_PER_BIENNIUM) 368 + SHR (hour, ALOG2_HOURS_PER_BIENNIUM)
365 + SHR (min, ALOG2_MINUTES_PER_BIENNIUM) 369 + SHR (min, ALOG2_MINUTES_PER_BIENNIUM)
366 + (LEAP_SECONDS_POSSIBLE 370 + (LEAP_SECONDS_POSSIBLE
367 ? 0 371 ? 0
368 : SHR (sec, ALOG2_SECONDS_PER_BIENNIUM))); 372 : SHR (sec, ALOG2_SECONDS_PER_BIENNIUM)));
369 373
370 int approx_biennia = SHR (t0, ALOG2_SECONDS_PER_BIENNIUM); 374 int approx_biennia = SHR (t0, ALOG2_SECONDS_PER_BIENNIUM);
371 int diff = approx_biennia - approx_requested_biennia; 375 int diff = approx_biennia - approx_requested_biennia;
372 int abs_diff = diff < 0 ? - diff : diff; 376 int abs_diff = diff < 0 ? - diff : diff;
373 377
374 /* IRIX 4.0.5 cc miscaculates TIME_T_MIN / 3: it erroneously 378 /* IRIX 4.0.5 cc miscaculates TIME_T_MIN / 3: it erroneously
375 gives a positive value of 715827882. Setting a variable 379 gives a positive value of 715827882. Setting a variable
376 first then doing math on it seems to work. 380 first then doing math on it seems to work.
377 (ghazi@caip.rutgers.edu) */ 381 (ghazi@caip.rutgers.edu) */
378 time_t time_t_max = TIME_T_MAX; 382 time_t time_t_max = TIME_T_MAX;
379 time_t time_t_min = TIME_T_MIN; 383 time_t time_t_min = TIME_T_MIN;
380 time_t overflow_threshold = 384 time_t overflow_threshold =
381 (time_t_max / 3 - time_t_min / 3) >> ALOG2_SECONDS_PER_BIENNIUM; 385 (time_t_max / 3 - time_t_min / 3) >> ALOG2_SECONDS_PER_BIENNIUM;
382 386
383 if (overflow_threshold < abs_diff) 387 if (overflow_threshold < abs_diff)
384 { 388 {
385 /* Overflow occurred. Try repairing it; this might work if 389 /* Overflow occurred. Try repairing it; this might work if
386 the time zone offset is enough to undo the overflow. */ 390 the time zone offset is enough to undo the overflow. */
387 time_t repaired_t0 = -1 - t0; 391 time_t repaired_t0 = -1 - t0;
388 approx_biennia = SHR (repaired_t0, ALOG2_SECONDS_PER_BIENNIUM); 392 approx_biennia = SHR (repaired_t0, ALOG2_SECONDS_PER_BIENNIUM);
389 diff = approx_biennia - approx_requested_biennia; 393 diff = approx_biennia - approx_requested_biennia;
390 abs_diff = diff < 0 ? - diff : diff; 394 abs_diff = diff < 0 ? - diff : diff;
391 if (overflow_threshold < abs_diff) 395 if (overflow_threshold < abs_diff)
392 return -1; 396 return -1;
393 guessed_offset += repaired_t0 - t0; 397 guessed_offset += repaired_t0 - t0;
394 t0 = repaired_t0; 398 t0 = repaired_t0;
395 } 399 }
396 } 400 }
397 401
398 /* Repeatedly use the error to improve the guess. */ 402 /* Repeatedly use the error to improve the guess. */
399 403
400 for (t = t1 = t2 = t0, dst2 = 0; 404 for (t = t1 = t2 = t0, dst2 = 0;
401 (gt = guess_time_tm (year, yday, hour, min, sec, &t, 405 (gt = guess_time_tm (year, yday, hour, min, sec, &t,
402 ranged_convert (convert, &t, &tm)), 406 ranged_convert (convert, &t, &tm)),
403 t != gt); 407 t != gt);
404 t1 = t2, t2 = t, t = gt, dst2 = tm.tm_isdst != 0) 408 t1 = t2, t2 = t, t = gt, dst2 = tm.tm_isdst != 0)
405 if (t == t1 && t != t2 409 if (t == t1 && t != t2
406 && (tm.tm_isdst < 0 410 && (tm.tm_isdst < 0
407 || (isdst < 0 411 || (isdst < 0
408 ? dst2 <= (tm.tm_isdst != 0) 412 ? dst2 <= (tm.tm_isdst != 0)
409 : (isdst != 0) != (tm.tm_isdst != 0)))) 413 : (isdst != 0) != (tm.tm_isdst != 0))))
410 /* We can't possibly find a match, as we are oscillating 414 /* We can't possibly find a match, as we are oscillating
411 between two values. The requested time probably falls 415 between two values. The requested time probably falls
412 within a spring-forward gap of size GT - T. Follow the common 416 within a spring-forward gap of size GT - T. Follow the common
413 practice in this case, which is to return a time that is GT - T 417 practice in this case, which is to return a time that is GT - T
414 away from the requested time, preferring a time whose 418 away from the requested time, preferring a time whose
415 tm_isdst differs from the requested value. (If no tm_isdst 419 tm_isdst differs from the requested value. (If no tm_isdst
416 was requested and only one of the two values has a nonzero 420 was requested and only one of the two values has a nonzero
417 tm_isdst, prefer that value.) In practice, this is more 421 tm_isdst, prefer that value.) In practice, this is more
418 useful than returning -1. */ 422 useful than returning -1. */
419 goto offset_found; 423 goto offset_found;
420 else if (--remaining_probes == 0) 424 else if (--remaining_probes == 0)
421 return -1; 425 return -1;
@@ -425,52 +429,52 @@ __mktime_internal (struct tm *tp,
425 if (isdst != tm.tm_isdst && 0 <= isdst && 0 <= tm.tm_isdst) 429 if (isdst != tm.tm_isdst && 0 <= isdst && 0 <= tm.tm_isdst)
426 { 430 {
427 /* tm.tm_isdst has the wrong value. Look for a neighboring 431 /* tm.tm_isdst has the wrong value. Look for a neighboring
428 time with the right value, and use its UTC offset. 432 time with the right value, and use its UTC offset.
429 433
430 Heuristic: probe the adjacent timestamps in both directions, 434 Heuristic: probe the adjacent timestamps in both directions,
431 looking for the desired isdst. This should work for all real 435 looking for the desired isdst. This should work for all real
432 time zone histories in the tz database. */ 436 time zone histories in the tz database. */
433 437
434 /* Distance between probes when looking for a DST boundary. In 438 /* Distance between probes when looking for a DST boundary. In
435 tzdata2003a, the shortest period of DST is 601200 seconds 439 tzdata2003a, the shortest period of DST is 601200 seconds
436 (e.g., America/Recife starting 2000-10-08 01:00), and the 440 (e.g., America/Recife starting 2000-10-08 01:00), and the
437 shortest period of non-DST surrounded by DST is 694800 441 shortest period of non-DST surrounded by DST is 694800
438 seconds (Africa/Tunis starting 1943-04-17 01:00). Use the 442 seconds (Africa/Tunis starting 1943-04-17 01:00). Use the
439 minimum of these two values, so we don't miss these short 443 minimum of these two values, so we don't miss these short
440 periods when probing. */ 444 periods when probing. */
441 int stride = 601200; 445 int stride = 601200;
442 446
443 /* The longest period of DST in tzdata2003a is 536454000 seconds 447 /* The longest period of DST in tzdata2003a is 536454000 seconds
444 (e.g., America/Jujuy starting 1946-10-01 01:00). The longest 448 (e.g., America/Jujuy starting 1946-10-01 01:00). The longest
445 period of non-DST is much longer, but it makes no real sense 449 period of non-DST is much longer, but it makes no real sense
446 to search for more than a year of non-DST, so use the DST 450 to search for more than a year of non-DST, so use the DST
447 max. */ 451 max. */
448 int duration_max = 536454000; 452 int duration_max = 536454000;
449 453
450 /* Search in both directions, so the maximum distance is half 454 /* Search in both directions, so the maximum distance is half
451 the duration; add the stride to avoid off-by-1 problems. */ 455 the duration; add the stride to avoid off-by-1 problems. */
452 int delta_bound = duration_max / 2 + stride; 456 int delta_bound = duration_max / 2 + stride;
453 457
454 int delta, direction; 458 int delta, direction;
455 459
456 for (delta = stride; delta < delta_bound; delta += stride) 460 for (delta = stride; delta < delta_bound; delta += stride)
457 for (direction = -1; direction <= 1; direction += 2) 461 for (direction = -1; direction <= 1; direction += 2)
458 { 462 {
459 time_t ot = t + delta * direction; 463 time_t ot = t + delta * direction;
460 if ((ot < t) == (direction < 0)) 464 if ((ot < t) == (direction < 0))
461 { 465 {
462 struct tm otm; 466 struct tm otm;
463 ranged_convert (convert, &ot, &otm); 467 ranged_convert (convert, &ot, &otm);
464 if (otm.tm_isdst == isdst) 468 if (otm.tm_isdst == isdst)
465 { 469 {
466 /* We found the desired tm_isdst. 470 /* We found the desired tm_isdst.
467 Extrapolate back to the desired time. */ 471 Extrapolate back to the desired time. */
468 t = guess_time_tm (year, yday, hour, min, sec, &ot, &otm); 472 t = guess_time_tm (year, yday, hour, min, sec, &ot, &otm);
469 ranged_convert (convert, &t, &tm); 473 ranged_convert (convert, &t, &tm);
470 goto offset_found; 474 goto offset_found;
471 } 475 }
472 } 476 }
473 } 477 }
474 } 478 }
475 479
476 offset_found: 480 offset_found:
@@ -479,14 +483,14 @@ __mktime_internal (struct tm *tp,
479 if (LEAP_SECONDS_POSSIBLE && sec_requested != tm.tm_sec) 483 if (LEAP_SECONDS_POSSIBLE && sec_requested != tm.tm_sec)
480 { 484 {
481 /* Adjust time to reflect the tm_sec requested, not the normalized value. 485 /* Adjust time to reflect the tm_sec requested, not the normalized value.
482 Also, repair any damage from a false match due to a leap second. */ 486 Also, repair any damage from a false match due to a leap second. */
483 int sec_adjustment = (sec == 0 && tm.tm_sec == 60) - sec; 487 int sec_adjustment = (sec == 0 && tm.tm_sec == 60) - sec;
484 t1 = t + sec_requested; 488 t1 = t + sec_requested;
485 t2 = t1 + sec_adjustment; 489 t2 = t1 + sec_adjustment;
486 if (((t1 < t) != (sec_requested < 0)) 490 if (((t1 < t) != (sec_requested < 0))
487 | ((t2 < t1) != (sec_adjustment < 0)) 491 | ((t2 < t1) != (sec_adjustment < 0))
488 | ! convert (&t2, &tm)) 492 | ! convert (&t2, &tm))
489 return -1; 493 return -1;
490 t = t2; 494 t = t2;
491 } 495 }
492 496
@@ -530,13 +534,13 @@ static int
530not_equal_tm (const struct tm *a, const struct tm *b) 534not_equal_tm (const struct tm *a, const struct tm *b)
531{ 535{
532 return ((a->tm_sec ^ b->tm_sec) 536 return ((a->tm_sec ^ b->tm_sec)
533 | (a->tm_min ^ b->tm_min) 537 | (a->tm_min ^ b->tm_min)
534 | (a->tm_hour ^ b->tm_hour) 538 | (a->tm_hour ^ b->tm_hour)
535 | (a->tm_mday ^ b->tm_mday) 539 | (a->tm_mday ^ b->tm_mday)
536 | (a->tm_mon ^ b->tm_mon) 540 | (a->tm_mon ^ b->tm_mon)
537 | (a->tm_year ^ b->tm_year) 541 | (a->tm_year ^ b->tm_year)
538 | (a->tm_yday ^ b->tm_yday) 542 | (a->tm_yday ^ b->tm_yday)
539 | (a->tm_isdst ^ b->tm_isdst)); 543 | (a->tm_isdst ^ b->tm_isdst));
540} 544}
541 545
542static void 546static void
@@ -544,9 +548,9 @@ print_tm (const struct tm *tp)
544{ 548{
545 if (tp) 549 if (tp)
546 printf ("%04d-%02d-%02d %02d:%02d:%02d yday %03d wday %d isdst %d", 550 printf ("%04d-%02d-%02d %02d:%02d:%02d yday %03d wday %d isdst %d",
547 tp->tm_year + TM_YEAR_BASE, tp->tm_mon + 1, tp->tm_mday, 551 tp->tm_year + TM_YEAR_BASE, tp->tm_mon + 1, tp->tm_mday,
548 tp->tm_hour, tp->tm_min, tp->tm_sec, 552 tp->tm_hour, tp->tm_min, tp->tm_sec,
549 tp->tm_yday, tp->tm_wday, tp->tm_isdst); 553 tp->tm_yday, tp->tm_wday, tp->tm_isdst);
550 else 554 else
551 printf ("0"); 555 printf ("0");
552} 556}
@@ -578,11 +582,11 @@ main (int argc, char **argv)
578 582
579 if ((argc == 3 || argc == 4) 583 if ((argc == 3 || argc == 4)
580 && (sscanf (argv[1], "%d-%d-%d%c", 584 && (sscanf (argv[1], "%d-%d-%d%c",
581 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &trailer) 585 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &trailer)
582 == 3) 586 == 3)
583 && (sscanf (argv[2], "%d:%d:%d%c", 587 && (sscanf (argv[2], "%d:%d:%d%c",
584 &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &trailer) 588 &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &trailer)
585 == 3)) 589 == 3))
586 { 590 {
587 tm.tm_year -= TM_YEAR_BASE; 591 tm.tm_year -= TM_YEAR_BASE;
588 tm.tm_mon--; 592 tm.tm_mon--;
@@ -591,10 +595,10 @@ main (int argc, char **argv)
591 tl = mktime (&tmk); 595 tl = mktime (&tmk);
592 lt = localtime (&tl); 596 lt = localtime (&tl);
593 if (lt) 597 if (lt)
594 { 598 {
595 tml = *lt; 599 tml = *lt;
596 lt = &tml; 600 lt = &tml;
597 } 601 }
598 printf ("mktime returns %ld == ", (long int) tl); 602 printf ("mktime returns %ld == ", (long int) tl);
599 print_tm (&tmk); 603 print_tm (&tmk);
600 printf ("\n"); 604 printf ("\n");
@@ -607,51 +611,51 @@ main (int argc, char **argv)
607 time_t to = atol (argv[3]); 611 time_t to = atol (argv[3]);
608 612
609 if (argc == 4) 613 if (argc == 4)
610 for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1) 614 for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1)
611 { 615 {
612 lt = localtime (&tl); 616 lt = localtime (&tl);
613 if (lt) 617 if (lt)
614 { 618 {
615 tmk = tml = *lt; 619 tmk = tml = *lt;
616 tk = mktime (&tmk); 620 tk = mktime (&tmk);
617 status |= check_result (tk, tmk, tl, &tml); 621 status |= check_result (tk, tmk, tl, &tml);
618 } 622 }
619 else 623 else
620 { 624 {
621 printf ("localtime (%ld) yields 0\n", (long int) tl); 625 printf ("localtime (%ld) yields 0\n", (long int) tl);
622 status = 1; 626 status = 1;
623 } 627 }
624 tl1 = tl + by; 628 tl1 = tl + by;
625 if ((tl1 < tl) != (by < 0)) 629 if ((tl1 < tl) != (by < 0))
626 break; 630 break;
627 } 631 }
628 else 632 else
629 for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1) 633 for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1)
630 { 634 {
631 /* Null benchmark. */ 635 /* Null benchmark. */
632 lt = localtime (&tl); 636 lt = localtime (&tl);
633 if (lt) 637 if (lt)
634 { 638 {
635 tmk = tml = *lt; 639 tmk = tml = *lt;
636 tk = tl; 640 tk = tl;
637 status |= check_result (tk, tmk, tl, &tml); 641 status |= check_result (tk, tmk, tl, &tml);
638 } 642 }
639 else 643 else
640 { 644 {
641 printf ("localtime (%ld) yields 0\n", (long int) tl); 645 printf ("localtime (%ld) yields 0\n", (long int) tl);
642 status = 1; 646 status = 1;
643 } 647 }
644 tl1 = tl + by; 648 tl1 = tl + by;
645 if ((tl1 < tl) != (by < 0)) 649 if ((tl1 < tl) != (by < 0))
646 break; 650 break;
647 } 651 }
648 } 652 }
649 else 653 else
650 printf ("Usage:\ 654 printf ("Usage:\
651\t%s YYYY-MM-DD HH:MM:SS [ISDST] # Test given time.\n\ 655\t%s YYYY-MM-DD HH:MM:SS [ISDST] # Test given time.\n\
652\t%s FROM BY TO # Test values FROM, FROM+BY, ..., TO.\n\ 656\t%s FROM BY TO # Test values FROM, FROM+BY, ..., TO.\n\
653\t%s FROM BY TO - # Do not test those values (for benchmark).\n", 657\t%s FROM BY TO - # Do not test those values (for benchmark).\n",
654 argv[0], argv[0], argv[0]); 658 argv[0], argv[0], argv[0]);
655 659
656 return status; 660 return status;
657} 661}