mktime.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /* Convert a 'struct tm' to a time_t value.
  2. Copyright (C) 1993-2017 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Paul Eggert <eggert@twinsun.com>.
  5. The GNU C 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. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* Define this to 1 to have a standalone program to test this implementation of
  17. mktime. */
  18. #ifndef DEBUG_MKTIME
  19. # define DEBUG_MKTIME 0
  20. #endif
  21. #if !defined _LIBC && !DEBUG_MKTIME
  22. # include <config.h>
  23. #endif
  24. /* Assume that leap seconds are possible, unless told otherwise.
  25. If the host has a 'zic' command with a '-L leapsecondfilename' option,
  26. then it supports leap seconds; otherwise it probably doesn't. */
  27. #ifndef LEAP_SECONDS_POSSIBLE
  28. # define LEAP_SECONDS_POSSIBLE 1
  29. #endif
  30. #include <time.h>
  31. #include <limits.h>
  32. #include <stdbool.h>
  33. #include <intprops.h>
  34. #include <verify.h>
  35. #if DEBUG_MKTIME
  36. # include <stdio.h>
  37. # include <stdlib.h>
  38. # include <string.h>
  39. /* Make it work even if the system's libc has its own mktime routine. */
  40. # undef mktime
  41. # define mktime my_mktime
  42. #endif
  43. /* A signed type that can represent an integer number of years
  44. multiplied by three times the number of seconds in a year. It is
  45. needed when converting a tm_year value times the number of seconds
  46. in a year. The factor of three comes because these products need
  47. to be subtracted from each other, and sometimes with an offset
  48. added to them, without worrying about overflow.
  49. Much of the code uses long_int to represent time_t values, to
  50. lessen the hassle of dealing with platforms where time_t is
  51. unsigned, and because long_int should suffice to represent all
  52. time_t values that mktime can generate even on platforms where
  53. time_t is excessively wide. */
  54. #if INT_MAX <= LONG_MAX / 3 / 366 / 24 / 60 / 60
  55. typedef long int long_int;
  56. #else
  57. typedef long long int long_int;
  58. #endif
  59. verify (INT_MAX <= TYPE_MAXIMUM (long_int) / 3 / 366 / 24 / 60 / 60);
  60. /* Shift A right by B bits portably, by dividing A by 2**B and
  61. truncating towards minus infinity. B should be in the range 0 <= B
  62. <= LONG_INT_BITS - 2, where LONG_INT_BITS is the number of useful
  63. bits in a long_int. LONG_INT_BITS is at least 32.
  64. ISO C99 says that A >> B is implementation-defined if A < 0. Some
  65. implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift
  66. right in the usual way when A < 0, so SHR falls back on division if
  67. ordinary A >> B doesn't seem to be the usual signed shift. */
  68. static long_int
  69. shr (long_int a, int b)
  70. {
  71. long_int one = 1;
  72. return (-one >> 1 == -1
  73. ? a >> b
  74. : a / (one << b) - (a % (one << b) < 0));
  75. }
  76. /* Bounds for the intersection of time_t and long_int. */
  77. static long_int const mktime_min
  78. = ((TYPE_SIGNED (time_t) && TYPE_MINIMUM (time_t) < TYPE_MINIMUM (long_int))
  79. ? TYPE_MINIMUM (long_int) : TYPE_MINIMUM (time_t));
  80. static long_int const mktime_max
  81. = (TYPE_MAXIMUM (long_int) < TYPE_MAXIMUM (time_t)
  82. ? TYPE_MAXIMUM (long_int) : TYPE_MAXIMUM (time_t));
  83. verify (TYPE_IS_INTEGER (time_t));
  84. #define EPOCH_YEAR 1970
  85. #define TM_YEAR_BASE 1900
  86. verify (TM_YEAR_BASE % 100 == 0);
  87. /* Is YEAR + TM_YEAR_BASE a leap year? */
  88. static bool
  89. leapyear (long_int year)
  90. {
  91. /* Don't add YEAR to TM_YEAR_BASE, as that might overflow.
  92. Also, work even if YEAR is negative. */
  93. return
  94. ((year & 3) == 0
  95. && (year % 100 != 0
  96. || ((year / 100) & 3) == (- (TM_YEAR_BASE / 100) & 3)));
  97. }
  98. /* How many days come before each month (0-12). */
  99. #ifndef _LIBC
  100. static
  101. #endif
  102. const unsigned short int __mon_yday[2][13] =
  103. {
  104. /* Normal years. */
  105. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  106. /* Leap years. */
  107. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  108. };
  109. #ifdef _LIBC
  110. typedef time_t mktime_offset_t;
  111. #else
  112. /* Portable standalone applications should supply a <time.h> that
  113. declares a POSIX-compliant localtime_r, for the benefit of older
  114. implementations that lack localtime_r or have a nonstandard one.
  115. See the gnulib time_r module for one way to implement this. */
  116. # undef __localtime_r
  117. # define __localtime_r localtime_r
  118. # define __mktime_internal mktime_internal
  119. # include "mktime-internal.h"
  120. #endif
  121. /* Do the values A and B differ according to the rules for tm_isdst?
  122. A and B differ if one is zero and the other positive. */
  123. static bool
  124. isdst_differ (int a, int b)
  125. {
  126. return (!a != !b) && (0 <= a) && (0 <= b);
  127. }
  128. /* Return an integer value measuring (YEAR1-YDAY1 HOUR1:MIN1:SEC1) -
  129. (YEAR0-YDAY0 HOUR0:MIN0:SEC0) in seconds, assuming that the clocks
  130. were not adjusted between the timestamps.
  131. The YEAR values uses the same numbering as TP->tm_year. Values
  132. need not be in the usual range. However, YEAR1 must not overflow
  133. when multiplied by three times the number of seconds in a year, and
  134. likewise for YDAY1 and three times the number of seconds in a day. */
  135. static long_int
  136. ydhms_diff (long_int year1, long_int yday1, int hour1, int min1, int sec1,
  137. int year0, int yday0, int hour0, int min0, int sec0)
  138. {
  139. verify (-1 / 2 == 0);
  140. /* Compute intervening leap days correctly even if year is negative.
  141. Take care to avoid integer overflow here. */
  142. int a4 = shr (year1, 2) + shr (TM_YEAR_BASE, 2) - ! (year1 & 3);
  143. int b4 = shr (year0, 2) + shr (TM_YEAR_BASE, 2) - ! (year0 & 3);
  144. int a100 = a4 / 25 - (a4 % 25 < 0);
  145. int b100 = b4 / 25 - (b4 % 25 < 0);
  146. int a400 = shr (a100, 2);
  147. int b400 = shr (b100, 2);
  148. int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
  149. /* Compute the desired time without overflowing. */
  150. long_int years = year1 - year0;
  151. long_int days = 365 * years + yday1 - yday0 + intervening_leap_days;
  152. long_int hours = 24 * days + hour1 - hour0;
  153. long_int minutes = 60 * hours + min1 - min0;
  154. long_int seconds = 60 * minutes + sec1 - sec0;
  155. return seconds;
  156. }
  157. /* Return the average of A and B, even if A + B would overflow.
  158. Round toward positive infinity. */
  159. static long_int
  160. long_int_avg (long_int a, long_int b)
  161. {
  162. return shr (a, 1) + shr (b, 1) + ((a | b) & 1);
  163. }
  164. /* Return a time_t value corresponding to (YEAR-YDAY HOUR:MIN:SEC),
  165. assuming that T corresponds to *TP and that no clock adjustments
  166. occurred between *TP and the desired time.
  167. Although T and the returned value are of type long_int,
  168. they represent time_t values and must be in time_t range.
  169. If TP is null, return a value not equal to T; this avoids false matches.
  170. YEAR and YDAY must not be so large that multiplying them by three times the
  171. number of seconds in a year (or day, respectively) would overflow long_int.
  172. If the returned value would be out of range, yield the minimal or
  173. maximal in-range value, except do not yield a value equal to T. */
  174. static long_int
  175. guess_time_tm (long_int year, long_int yday, int hour, int min, int sec,
  176. long_int t, const struct tm *tp)
  177. {
  178. if (tp)
  179. {
  180. long_int result;
  181. long_int d = ydhms_diff (year, yday, hour, min, sec,
  182. tp->tm_year, tp->tm_yday,
  183. tp->tm_hour, tp->tm_min, tp->tm_sec);
  184. if (! INT_ADD_WRAPV (t, d, &result))
  185. return result;
  186. }
  187. /* Overflow occurred one way or another. Return the nearest result
  188. that is actually in range, except don't report a zero difference
  189. if the actual difference is nonzero, as that would cause a false
  190. match; and don't oscillate between two values, as that would
  191. confuse the spring-forward gap detector. */
  192. return (t < long_int_avg (mktime_min, mktime_max)
  193. ? (t <= mktime_min + 1 ? t + 1 : mktime_min)
  194. : (mktime_max - 1 <= t ? t - 1 : mktime_max));
  195. }
  196. /* Use CONVERT to convert T to a struct tm value in *TM. T must be in
  197. range for time_t. Return TM if successful, NULL if T is out of
  198. range for CONVERT. */
  199. static struct tm *
  200. convert_time (struct tm *(*convert) (const time_t *, struct tm *),
  201. long_int t, struct tm *tm)
  202. {
  203. time_t x = t;
  204. return convert (&x, tm);
  205. }
  206. /* Use CONVERT to convert *T to a broken down time in *TP.
  207. If *T is out of range for conversion, adjust it so that
  208. it is the nearest in-range value and then convert that.
  209. A value is in range if it fits in both time_t and long_int. */
  210. static struct tm *
  211. ranged_convert (struct tm *(*convert) (const time_t *, struct tm *),
  212. long_int *t, struct tm *tp)
  213. {
  214. struct tm *r;
  215. if (*t < mktime_min)
  216. *t = mktime_min;
  217. else if (mktime_max < *t)
  218. *t = mktime_max;
  219. r = convert_time (convert, *t, tp);
  220. if (!r && *t)
  221. {
  222. long_int bad = *t;
  223. long_int ok = 0;
  224. /* BAD is a known unconvertible value, and OK is a known good one.
  225. Use binary search to narrow the range between BAD and OK until
  226. they differ by 1. */
  227. while (true)
  228. {
  229. long_int mid = long_int_avg (ok, bad);
  230. if (mid != ok && mid != bad)
  231. break;
  232. r = convert_time (convert, mid, tp);
  233. if (r)
  234. ok = mid;
  235. else
  236. bad = mid;
  237. }
  238. if (!r && ok)
  239. {
  240. /* The last conversion attempt failed;
  241. revert to the most recent successful attempt. */
  242. r = convert_time (convert, ok, tp);
  243. }
  244. }
  245. return r;
  246. }
  247. /* Convert *TP to a time_t value, inverting
  248. the monotonic and mostly-unit-linear conversion function CONVERT.
  249. Use *OFFSET to keep track of a guess at the offset of the result,
  250. compared to what the result would be for UTC without leap seconds.
  251. If *OFFSET's guess is correct, only one CONVERT call is needed.
  252. This function is external because it is used also by timegm.c. */
  253. time_t
  254. __mktime_internal (struct tm *tp,
  255. struct tm *(*convert) (const time_t *, struct tm *),
  256. mktime_offset_t *offset)
  257. {
  258. long_int t, gt, t0, t1, t2, dt;
  259. struct tm tm;
  260. /* The maximum number of probes (calls to CONVERT) should be enough
  261. to handle any combinations of time zone rule changes, solar time,
  262. leap seconds, and oscillations around a spring-forward gap.
  263. POSIX.1 prohibits leap seconds, but some hosts have them anyway. */
  264. int remaining_probes = 6;
  265. /* Time requested. Copy it in case CONVERT modifies *TP; this can
  266. occur if TP is localtime's returned value and CONVERT is localtime. */
  267. int sec = tp->tm_sec;
  268. int min = tp->tm_min;
  269. int hour = tp->tm_hour;
  270. int mday = tp->tm_mday;
  271. int mon = tp->tm_mon;
  272. int year_requested = tp->tm_year;
  273. int isdst = tp->tm_isdst;
  274. /* 1 if the previous probe was DST. */
  275. int dst2;
  276. /* Ensure that mon is in range, and set year accordingly. */
  277. int mon_remainder = mon % 12;
  278. int negative_mon_remainder = mon_remainder < 0;
  279. int mon_years = mon / 12 - negative_mon_remainder;
  280. long_int lyear_requested = year_requested;
  281. long_int year = lyear_requested + mon_years;
  282. /* The other values need not be in range:
  283. the remaining code handles overflows correctly. */
  284. /* Calculate day of year from year, month, and day of month.
  285. The result need not be in range. */
  286. int mon_yday = ((__mon_yday[leapyear (year)]
  287. [mon_remainder + 12 * negative_mon_remainder])
  288. - 1);
  289. long_int lmday = mday;
  290. long_int yday = mon_yday + lmday;
  291. int negative_offset_guess;
  292. int sec_requested = sec;
  293. if (LEAP_SECONDS_POSSIBLE)
  294. {
  295. /* Handle out-of-range seconds specially,
  296. since ydhms_tm_diff assumes every minute has 60 seconds. */
  297. if (sec < 0)
  298. sec = 0;
  299. if (59 < sec)
  300. sec = 59;
  301. }
  302. /* Invert CONVERT by probing. First assume the same offset as last
  303. time. */
  304. INT_SUBTRACT_WRAPV (0, *offset, &negative_offset_guess);
  305. t0 = ydhms_diff (year, yday, hour, min, sec,
  306. EPOCH_YEAR - TM_YEAR_BASE, 0, 0, 0, negative_offset_guess);
  307. /* Repeatedly use the error to improve the guess. */
  308. for (t = t1 = t2 = t0, dst2 = 0;
  309. (gt = guess_time_tm (year, yday, hour, min, sec, t,
  310. ranged_convert (convert, &t, &tm)),
  311. t != gt);
  312. t1 = t2, t2 = t, t = gt, dst2 = tm.tm_isdst != 0)
  313. if (t == t1 && t != t2
  314. && (tm.tm_isdst < 0
  315. || (isdst < 0
  316. ? dst2 <= (tm.tm_isdst != 0)
  317. : (isdst != 0) != (tm.tm_isdst != 0))))
  318. /* We can't possibly find a match, as we are oscillating
  319. between two values. The requested time probably falls
  320. within a spring-forward gap of size GT - T. Follow the common
  321. practice in this case, which is to return a time that is GT - T
  322. away from the requested time, preferring a time whose
  323. tm_isdst differs from the requested value. (If no tm_isdst
  324. was requested and only one of the two values has a nonzero
  325. tm_isdst, prefer that value.) In practice, this is more
  326. useful than returning -1. */
  327. goto offset_found;
  328. else if (--remaining_probes == 0)
  329. return -1;
  330. /* We have a match. Check whether tm.tm_isdst has the requested
  331. value, if any. */
  332. if (isdst_differ (isdst, tm.tm_isdst))
  333. {
  334. /* tm.tm_isdst has the wrong value. Look for a neighboring
  335. time with the right value, and use its UTC offset.
  336. Heuristic: probe the adjacent timestamps in both directions,
  337. looking for the desired isdst. This should work for all real
  338. time zone histories in the tz database. */
  339. /* Distance between probes when looking for a DST boundary. In
  340. tzdata2003a, the shortest period of DST is 601200 seconds
  341. (e.g., America/Recife starting 2000-10-08 01:00), and the
  342. shortest period of non-DST surrounded by DST is 694800
  343. seconds (Africa/Tunis starting 1943-04-17 01:00). Use the
  344. minimum of these two values, so we don't miss these short
  345. periods when probing. */
  346. int stride = 601200;
  347. /* The longest period of DST in tzdata2003a is 536454000 seconds
  348. (e.g., America/Jujuy starting 1946-10-01 01:00). The longest
  349. period of non-DST is much longer, but it makes no real sense
  350. to search for more than a year of non-DST, so use the DST
  351. max. */
  352. int duration_max = 536454000;
  353. /* Search in both directions, so the maximum distance is half
  354. the duration; add the stride to avoid off-by-1 problems. */
  355. int delta_bound = duration_max / 2 + stride;
  356. int delta, direction;
  357. for (delta = stride; delta < delta_bound; delta += stride)
  358. for (direction = -1; direction <= 1; direction += 2)
  359. {
  360. long_int ot;
  361. if (! INT_ADD_WRAPV (t, delta * direction, &ot))
  362. {
  363. struct tm otm;
  364. ranged_convert (convert, &ot, &otm);
  365. if (! isdst_differ (isdst, otm.tm_isdst))
  366. {
  367. /* We found the desired tm_isdst.
  368. Extrapolate back to the desired time. */
  369. t = guess_time_tm (year, yday, hour, min, sec, ot, &otm);
  370. ranged_convert (convert, &t, &tm);
  371. goto offset_found;
  372. }
  373. }
  374. }
  375. }
  376. offset_found:
  377. /* Set *OFFSET to the low-order bits of T - T0 - NEGATIVE_OFFSET_GUESS.
  378. This is just a heuristic to speed up the next mktime call, and
  379. correctness is unaffected if integer overflow occurs here. */
  380. INT_SUBTRACT_WRAPV (t, t0, &dt);
  381. INT_SUBTRACT_WRAPV (dt, negative_offset_guess, offset);
  382. if (LEAP_SECONDS_POSSIBLE && sec_requested != tm.tm_sec)
  383. {
  384. /* Adjust time to reflect the tm_sec requested, not the normalized value.
  385. Also, repair any damage from a false match due to a leap second. */
  386. long_int sec_adjustment = sec == 0 && tm.tm_sec == 60;
  387. sec_adjustment -= sec;
  388. sec_adjustment += sec_requested;
  389. if (INT_ADD_WRAPV (t, sec_adjustment, &t)
  390. || ! (mktime_min <= t && t <= mktime_max)
  391. || ! convert_time (convert, t, &tm))
  392. return -1;
  393. }
  394. *tp = tm;
  395. return t;
  396. }
  397. static mktime_offset_t localtime_offset;
  398. /* Convert *TP to a time_t value. */
  399. time_t
  400. mktime (struct tm *tp)
  401. {
  402. #ifdef _LIBC
  403. /* POSIX.1 8.1.1 requires that whenever mktime() is called, the
  404. time zone names contained in the external variable 'tzname' shall
  405. be set as if the tzset() function had been called. */
  406. __tzset ();
  407. #elif HAVE_TZSET
  408. tzset ();
  409. #endif
  410. return __mktime_internal (tp, __localtime_r, &localtime_offset);
  411. }
  412. #ifdef weak_alias
  413. weak_alias (mktime, timelocal)
  414. #endif
  415. #ifdef _LIBC
  416. libc_hidden_def (mktime)
  417. libc_hidden_weak (timelocal)
  418. #endif
  419. #if DEBUG_MKTIME
  420. static int
  421. not_equal_tm (const struct tm *a, const struct tm *b)
  422. {
  423. return ((a->tm_sec ^ b->tm_sec)
  424. | (a->tm_min ^ b->tm_min)
  425. | (a->tm_hour ^ b->tm_hour)
  426. | (a->tm_mday ^ b->tm_mday)
  427. | (a->tm_mon ^ b->tm_mon)
  428. | (a->tm_year ^ b->tm_year)
  429. | (a->tm_yday ^ b->tm_yday)
  430. | isdst_differ (a->tm_isdst, b->tm_isdst));
  431. }
  432. static void
  433. print_tm (const struct tm *tp)
  434. {
  435. if (tp)
  436. printf ("%04d-%02d-%02d %02d:%02d:%02d yday %03d wday %d isdst %d",
  437. tp->tm_year + TM_YEAR_BASE, tp->tm_mon + 1, tp->tm_mday,
  438. tp->tm_hour, tp->tm_min, tp->tm_sec,
  439. tp->tm_yday, tp->tm_wday, tp->tm_isdst);
  440. else
  441. printf ("0");
  442. }
  443. static int
  444. check_result (time_t tk, struct tm tmk, time_t tl, const struct tm *lt)
  445. {
  446. if (tk != tl || !lt || not_equal_tm (&tmk, lt))
  447. {
  448. printf ("mktime (");
  449. print_tm (lt);
  450. printf (")\nyields (");
  451. print_tm (&tmk);
  452. printf (") == %ld, should be %ld\n", (long int) tk, (long int) tl);
  453. return 1;
  454. }
  455. return 0;
  456. }
  457. int
  458. main (int argc, char **argv)
  459. {
  460. int status = 0;
  461. struct tm tm, tmk, tml;
  462. struct tm *lt;
  463. time_t tk, tl, tl1;
  464. char trailer;
  465. /* Sanity check, plus call tzset. */
  466. tl = 0;
  467. if (! localtime (&tl))
  468. {
  469. printf ("localtime (0) fails\n");
  470. status = 1;
  471. }
  472. if ((argc == 3 || argc == 4)
  473. && (sscanf (argv[1], "%d-%d-%d%c",
  474. &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &trailer)
  475. == 3)
  476. && (sscanf (argv[2], "%d:%d:%d%c",
  477. &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &trailer)
  478. == 3))
  479. {
  480. tm.tm_year -= TM_YEAR_BASE;
  481. tm.tm_mon--;
  482. tm.tm_isdst = argc == 3 ? -1 : atoi (argv[3]);
  483. tmk = tm;
  484. tl = mktime (&tmk);
  485. lt = localtime_r (&tl, &tml);
  486. printf ("mktime returns %ld == ", (long int) tl);
  487. print_tm (&tmk);
  488. printf ("\n");
  489. status = check_result (tl, tmk, tl, lt);
  490. }
  491. else if (argc == 4 || (argc == 5 && strcmp (argv[4], "-") == 0))
  492. {
  493. time_t from = atol (argv[1]);
  494. time_t by = atol (argv[2]);
  495. time_t to = atol (argv[3]);
  496. if (argc == 4)
  497. for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1)
  498. {
  499. lt = localtime_r (&tl, &tml);
  500. if (lt)
  501. {
  502. tmk = tml;
  503. tk = mktime (&tmk);
  504. status |= check_result (tk, tmk, tl, &tml);
  505. }
  506. else
  507. {
  508. printf ("localtime_r (%ld) yields 0\n", (long int) tl);
  509. status = 1;
  510. }
  511. tl1 = tl + by;
  512. if ((tl1 < tl) != (by < 0))
  513. break;
  514. }
  515. else
  516. for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1)
  517. {
  518. /* Null benchmark. */
  519. lt = localtime_r (&tl, &tml);
  520. if (lt)
  521. {
  522. tmk = tml;
  523. tk = tl;
  524. status |= check_result (tk, tmk, tl, &tml);
  525. }
  526. else
  527. {
  528. printf ("localtime_r (%ld) yields 0\n", (long int) tl);
  529. status = 1;
  530. }
  531. tl1 = tl + by;
  532. if ((tl1 < tl) != (by < 0))
  533. break;
  534. }
  535. }
  536. else
  537. printf ("Usage:\
  538. \t%s YYYY-MM-DD HH:MM:SS [ISDST] # Test given time.\n\
  539. \t%s FROM BY TO # Test values FROM, FROM+BY, ..., TO.\n\
  540. \t%s FROM BY TO - # Do not test those values (for benchmark).\n",
  541. argv[0], argv[0], argv[0]);
  542. return status;
  543. }
  544. #endif /* DEBUG_MKTIME */
  545. /*
  546. Local Variables:
  547. compile-command: "gcc -DDEBUG_MKTIME -I. -Wall -W -O2 -g mktime.c -o mktime"
  548. End:
  549. */