mktime.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /* Convert a 'struct tm' to a time_t value.
  2. Copyright (C) 1993-2022 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. <https://www.gnu.org/licenses/>. */
  16. /* The following macros influence what gets defined when this file is compiled:
  17. Macro/expression Which gnulib module This compilation unit
  18. should define
  19. _LIBC (glibc proper) mktime
  20. NEED_MKTIME_WORKING mktime rpl_mktime
  21. || NEED_MKTIME_WINDOWS
  22. NEED_MKTIME_INTERNAL mktime-internal mktime_internal
  23. */
  24. #ifndef _LIBC
  25. # include <libc-config.h>
  26. #endif
  27. /* Assume that leap seconds are possible, unless told otherwise.
  28. If the host has a 'zic' command with a '-L leapsecondfilename' option,
  29. then it supports leap seconds; otherwise it probably doesn't. */
  30. #ifndef LEAP_SECONDS_POSSIBLE
  31. # define LEAP_SECONDS_POSSIBLE 1
  32. #endif
  33. #include <time.h>
  34. #include <errno.h>
  35. #include <limits.h>
  36. #include <stdbool.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <intprops.h>
  40. #include <verify.h>
  41. #ifndef NEED_MKTIME_INTERNAL
  42. # define NEED_MKTIME_INTERNAL 0
  43. #endif
  44. #ifndef NEED_MKTIME_WINDOWS
  45. # define NEED_MKTIME_WINDOWS 0
  46. #endif
  47. #ifndef NEED_MKTIME_WORKING
  48. # define NEED_MKTIME_WORKING 0
  49. #endif
  50. #include "mktime-internal.h"
  51. #if !defined _LIBC && (NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS)
  52. static void
  53. my_tzset (void)
  54. {
  55. # if NEED_MKTIME_WINDOWS
  56. /* Rectify the value of the environment variable TZ.
  57. There are four possible kinds of such values:
  58. - Traditional US time zone names, e.g. "PST8PDT". Syntax: see
  59. <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tzset>
  60. - Time zone names based on geography, that contain one or more
  61. slashes, e.g. "Europe/Moscow".
  62. - Time zone names based on geography, without slashes, e.g.
  63. "Singapore".
  64. - Time zone names that contain explicit DST rules. Syntax: see
  65. <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03>
  66. The Microsoft CRT understands only the first kind. It produces incorrect
  67. results if the value of TZ is of the other kinds.
  68. But in a Cygwin environment, /etc/profile.d/tzset.sh sets TZ to a value
  69. of the second kind for most geographies, or of the first kind in a few
  70. other geographies. If it is of the second kind, neutralize it. For the
  71. Microsoft CRT, an absent or empty TZ means the time zone that the user
  72. has set in the Windows Control Panel.
  73. If the value of TZ is of the third or fourth kind -- Cygwin programs
  74. understand these syntaxes as well --, it does not matter whether we
  75. neutralize it or not, since these values occur only when a Cygwin user
  76. has set TZ explicitly; this case is 1. rare and 2. under the user's
  77. responsibility. */
  78. const char *tz = getenv ("TZ");
  79. if (tz != NULL && strchr (tz, '/') != NULL)
  80. _putenv ("TZ=");
  81. # else
  82. tzset ();
  83. # endif
  84. }
  85. # undef __tzset
  86. # define __tzset() my_tzset ()
  87. #endif
  88. #if defined _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_INTERNAL
  89. /* A signed type that can represent an integer number of years
  90. multiplied by four times the number of seconds in a year. It is
  91. needed when converting a tm_year value times the number of seconds
  92. in a year. The factor of four comes because these products need
  93. to be subtracted from each other, and sometimes with an offset
  94. added to them, and then with another timestamp added, without
  95. worrying about overflow.
  96. Much of the code uses long_int to represent __time64_t values, to
  97. lessen the hassle of dealing with platforms where __time64_t is
  98. unsigned, and because long_int should suffice to represent all
  99. __time64_t values that mktime can generate even on platforms where
  100. __time64_t is wider than the int components of struct tm. */
  101. #if INT_MAX <= LONG_MAX / 4 / 366 / 24 / 60 / 60
  102. typedef long int long_int;
  103. #else
  104. typedef long long int long_int;
  105. #endif
  106. verify (INT_MAX <= TYPE_MAXIMUM (long_int) / 4 / 366 / 24 / 60 / 60);
  107. /* Shift A right by B bits portably, by dividing A by 2**B and
  108. truncating towards minus infinity. B should be in the range 0 <= B
  109. <= LONG_INT_BITS - 2, where LONG_INT_BITS is the number of useful
  110. bits in a long_int. LONG_INT_BITS is at least 32.
  111. ISO C99 says that A >> B is implementation-defined if A < 0. Some
  112. implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift
  113. right in the usual way when A < 0, so SHR falls back on division if
  114. ordinary A >> B doesn't seem to be the usual signed shift. */
  115. static long_int
  116. shr (long_int a, int b)
  117. {
  118. long_int one = 1;
  119. return (-one >> 1 == -1
  120. ? a >> b
  121. : (a + (a < 0)) / (one << b) - (a < 0));
  122. }
  123. /* Bounds for the intersection of __time64_t and long_int. */
  124. static long_int const mktime_min
  125. = ((TYPE_SIGNED (__time64_t)
  126. && TYPE_MINIMUM (__time64_t) < TYPE_MINIMUM (long_int))
  127. ? TYPE_MINIMUM (long_int) : TYPE_MINIMUM (__time64_t));
  128. static long_int const mktime_max
  129. = (TYPE_MAXIMUM (long_int) < TYPE_MAXIMUM (__time64_t)
  130. ? TYPE_MAXIMUM (long_int) : TYPE_MAXIMUM (__time64_t));
  131. #define EPOCH_YEAR 1970
  132. #define TM_YEAR_BASE 1900
  133. verify (TM_YEAR_BASE % 100 == 0);
  134. /* Is YEAR + TM_YEAR_BASE a leap year? */
  135. static bool
  136. leapyear (long_int year)
  137. {
  138. /* Don't add YEAR to TM_YEAR_BASE, as that might overflow.
  139. Also, work even if YEAR is negative. */
  140. return
  141. ((year & 3) == 0
  142. && (year % 100 != 0
  143. || ((year / 100) & 3) == (- (TM_YEAR_BASE / 100) & 3)));
  144. }
  145. /* How many days come before each month (0-12). */
  146. #ifndef _LIBC
  147. static
  148. #endif
  149. const unsigned short int __mon_yday[2][13] =
  150. {
  151. /* Normal years. */
  152. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  153. /* Leap years. */
  154. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  155. };
  156. /* Do the values A and B differ according to the rules for tm_isdst?
  157. A and B differ if one is zero and the other positive. */
  158. static bool
  159. isdst_differ (int a, int b)
  160. {
  161. return (!a != !b) && (0 <= a) && (0 <= b);
  162. }
  163. /* Return an integer value measuring (YEAR1-YDAY1 HOUR1:MIN1:SEC1) -
  164. (YEAR0-YDAY0 HOUR0:MIN0:SEC0) in seconds, assuming that the clocks
  165. were not adjusted between the timestamps.
  166. The YEAR values uses the same numbering as TP->tm_year. Values
  167. need not be in the usual range. However, YEAR1 - YEAR0 must not
  168. overflow even when multiplied by three times the number of seconds
  169. in a year, and likewise for YDAY1 - YDAY0 and three times the
  170. number of seconds in a day. */
  171. static long_int
  172. ydhms_diff (long_int year1, long_int yday1, int hour1, int min1, int sec1,
  173. int year0, int yday0, int hour0, int min0, int sec0)
  174. {
  175. verify (-1 / 2 == 0);
  176. /* Compute intervening leap days correctly even if year is negative.
  177. Take care to avoid integer overflow here. */
  178. int a4 = shr (year1, 2) + shr (TM_YEAR_BASE, 2) - ! (year1 & 3);
  179. int b4 = shr (year0, 2) + shr (TM_YEAR_BASE, 2) - ! (year0 & 3);
  180. int a100 = (a4 + (a4 < 0)) / 25 - (a4 < 0);
  181. int b100 = (b4 + (b4 < 0)) / 25 - (b4 < 0);
  182. int a400 = shr (a100, 2);
  183. int b400 = shr (b100, 2);
  184. int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
  185. /* Compute the desired time without overflowing. */
  186. long_int years = year1 - year0;
  187. long_int days = 365 * years + yday1 - yday0 + intervening_leap_days;
  188. long_int hours = 24 * days + hour1 - hour0;
  189. long_int minutes = 60 * hours + min1 - min0;
  190. long_int seconds = 60 * minutes + sec1 - sec0;
  191. return seconds;
  192. }
  193. /* Return the average of A and B, even if A + B would overflow.
  194. Round toward positive infinity. */
  195. static long_int
  196. long_int_avg (long_int a, long_int b)
  197. {
  198. return shr (a, 1) + shr (b, 1) + ((a | b) & 1);
  199. }
  200. /* Return a long_int value corresponding to (YEAR-YDAY HOUR:MIN:SEC)
  201. minus *TP seconds, assuming no clock adjustments occurred between
  202. the two timestamps.
  203. YEAR and YDAY must not be so large that multiplying them by three times the
  204. number of seconds in a year (or day, respectively) would overflow long_int.
  205. *TP should be in the usual range. */
  206. static long_int
  207. tm_diff (long_int year, long_int yday, int hour, int min, int sec,
  208. struct tm const *tp)
  209. {
  210. return ydhms_diff (year, yday, hour, min, sec,
  211. tp->tm_year, tp->tm_yday,
  212. tp->tm_hour, tp->tm_min, tp->tm_sec);
  213. }
  214. /* Use CONVERT to convert T to a struct tm value in *TM. T must be in
  215. range for __time64_t. Return TM if successful, NULL (setting errno) on
  216. failure. */
  217. static struct tm *
  218. convert_time (struct tm *(*convert) (const __time64_t *, struct tm *),
  219. long_int t, struct tm *tm)
  220. {
  221. __time64_t x = t;
  222. return convert (&x, tm);
  223. }
  224. /* Use CONVERT to convert *T to a broken down time in *TP.
  225. If *T is out of range for conversion, adjust it so that
  226. it is the nearest in-range value and then convert that.
  227. A value is in range if it fits in both __time64_t and long_int.
  228. Return TP on success, NULL (setting errno) on failure. */
  229. static struct tm *
  230. ranged_convert (struct tm *(*convert) (const __time64_t *, struct tm *),
  231. long_int *t, struct tm *tp)
  232. {
  233. long_int t1 = (*t < mktime_min ? mktime_min
  234. : *t <= mktime_max ? *t : mktime_max);
  235. struct tm *r = convert_time (convert, t1, tp);
  236. if (r)
  237. {
  238. *t = t1;
  239. return r;
  240. }
  241. if (errno != EOVERFLOW)
  242. return NULL;
  243. long_int bad = t1;
  244. long_int ok = 0;
  245. struct tm oktm; oktm.tm_sec = -1;
  246. /* BAD is a known out-of-range value, and OK is a known in-range one.
  247. Use binary search to narrow the range between BAD and OK until
  248. they differ by 1. */
  249. while (true)
  250. {
  251. long_int mid = long_int_avg (ok, bad);
  252. if (mid == ok || mid == bad)
  253. break;
  254. if (convert_time (convert, mid, tp))
  255. ok = mid, oktm = *tp;
  256. else if (errno != EOVERFLOW)
  257. return NULL;
  258. else
  259. bad = mid;
  260. }
  261. if (oktm.tm_sec < 0)
  262. return NULL;
  263. *t = ok;
  264. *tp = oktm;
  265. return tp;
  266. }
  267. /* Convert *TP to a __time64_t value, inverting
  268. the monotonic and mostly-unit-linear conversion function CONVERT.
  269. Use *OFFSET to keep track of a guess at the offset of the result,
  270. compared to what the result would be for UTC without leap seconds.
  271. If *OFFSET's guess is correct, only one CONVERT call is needed.
  272. If successful, set *TP to the canonicalized struct tm;
  273. otherwise leave *TP alone, return ((time_t) -1) and set errno.
  274. This function is external because it is used also by timegm.c. */
  275. __time64_t
  276. __mktime_internal (struct tm *tp,
  277. struct tm *(*convert) (const __time64_t *, struct tm *),
  278. mktime_offset_t *offset)
  279. {
  280. struct tm tm;
  281. /* The maximum number of probes (calls to CONVERT) should be enough
  282. to handle any combinations of time zone rule changes, solar time,
  283. leap seconds, and oscillations around a spring-forward gap.
  284. POSIX.1 prohibits leap seconds, but some hosts have them anyway. */
  285. int remaining_probes = 6;
  286. /* Time requested. Copy it in case CONVERT modifies *TP; this can
  287. occur if TP is localtime's returned value and CONVERT is localtime. */
  288. int sec = tp->tm_sec;
  289. int min = tp->tm_min;
  290. int hour = tp->tm_hour;
  291. int mday = tp->tm_mday;
  292. int mon = tp->tm_mon;
  293. int year_requested = tp->tm_year;
  294. int isdst = tp->tm_isdst;
  295. /* 1 if the previous probe was DST. */
  296. int dst2 = 0;
  297. /* Ensure that mon is in range, and set year accordingly. */
  298. int mon_remainder = mon % 12;
  299. int negative_mon_remainder = mon_remainder < 0;
  300. int mon_years = mon / 12 - negative_mon_remainder;
  301. long_int lyear_requested = year_requested;
  302. long_int year = lyear_requested + mon_years;
  303. /* The other values need not be in range:
  304. the remaining code handles overflows correctly. */
  305. /* Calculate day of year from year, month, and day of month.
  306. The result need not be in range. */
  307. int mon_yday = ((__mon_yday[leapyear (year)]
  308. [mon_remainder + 12 * negative_mon_remainder])
  309. - 1);
  310. long_int lmday = mday;
  311. long_int yday = mon_yday + lmday;
  312. mktime_offset_t off = *offset;
  313. int negative_offset_guess;
  314. int sec_requested = sec;
  315. if (LEAP_SECONDS_POSSIBLE)
  316. {
  317. /* Handle out-of-range seconds specially,
  318. since ydhms_diff assumes every minute has 60 seconds. */
  319. if (sec < 0)
  320. sec = 0;
  321. if (59 < sec)
  322. sec = 59;
  323. }
  324. /* Invert CONVERT by probing. First assume the same offset as last
  325. time. */
  326. INT_SUBTRACT_WRAPV (0, off, &negative_offset_guess);
  327. long_int t0 = ydhms_diff (year, yday, hour, min, sec,
  328. EPOCH_YEAR - TM_YEAR_BASE, 0, 0, 0,
  329. negative_offset_guess);
  330. long_int t = t0, t1 = t0, t2 = t0;
  331. /* Repeatedly use the error to improve the guess. */
  332. while (true)
  333. {
  334. if (! ranged_convert (convert, &t, &tm))
  335. return -1;
  336. long_int dt = tm_diff (year, yday, hour, min, sec, &tm);
  337. if (dt == 0)
  338. break;
  339. if (t == t1 && t != t2
  340. && (tm.tm_isdst < 0
  341. || (isdst < 0
  342. ? dst2 <= (tm.tm_isdst != 0)
  343. : (isdst != 0) != (tm.tm_isdst != 0))))
  344. /* We can't possibly find a match, as we are oscillating
  345. between two values. The requested time probably falls
  346. within a spring-forward gap of size DT. Follow the common
  347. practice in this case, which is to return a time that is DT
  348. away from the requested time, preferring a time whose
  349. tm_isdst differs from the requested value. (If no tm_isdst
  350. was requested and only one of the two values has a nonzero
  351. tm_isdst, prefer that value.) In practice, this is more
  352. useful than returning -1. */
  353. goto offset_found;
  354. remaining_probes--;
  355. if (remaining_probes == 0)
  356. {
  357. __set_errno (EOVERFLOW);
  358. return -1;
  359. }
  360. t1 = t2, t2 = t, t += dt, dst2 = tm.tm_isdst != 0;
  361. }
  362. /* We have a match. Check whether tm.tm_isdst has the requested
  363. value, if any. */
  364. if (isdst_differ (isdst, tm.tm_isdst))
  365. {
  366. /* tm.tm_isdst has the wrong value. Look for a neighboring
  367. time with the right value, and use its UTC offset.
  368. Heuristic: probe the adjacent timestamps in both directions,
  369. looking for the desired isdst. If none is found within a
  370. reasonable duration bound, assume a one-hour DST difference.
  371. This should work for all real time zone histories in the tz
  372. database. */
  373. /* +1 if we wanted standard time but got DST, -1 if the reverse. */
  374. int dst_difference = (isdst == 0) - (tm.tm_isdst == 0);
  375. /* Distance between probes when looking for a DST boundary. In
  376. tzdata2003a, the shortest period of DST is 601200 seconds
  377. (e.g., America/Recife starting 2000-10-08 01:00), and the
  378. shortest period of non-DST surrounded by DST is 694800
  379. seconds (Africa/Tunis starting 1943-04-17 01:00). Use the
  380. minimum of these two values, so we don't miss these short
  381. periods when probing. */
  382. int stride = 601200;
  383. /* In TZDB 2021e, the longest period of DST (or of non-DST), in
  384. which the DST (or adjacent DST) difference is not one hour,
  385. is 457243209 seconds: e.g., America/Cambridge_Bay with leap
  386. seconds, starting 1965-10-31 00:00 in a switch from
  387. double-daylight time (-05) to standard time (-07), and
  388. continuing to 1980-04-27 02:00 in a switch from standard time
  389. (-07) to daylight time (-06). */
  390. int duration_max = 457243209;
  391. /* Search in both directions, so the maximum distance is half
  392. the duration; add the stride to avoid off-by-1 problems. */
  393. int delta_bound = duration_max / 2 + stride;
  394. int delta, direction;
  395. for (delta = stride; delta < delta_bound; delta += stride)
  396. for (direction = -1; direction <= 1; direction += 2)
  397. {
  398. long_int ot;
  399. if (! INT_ADD_WRAPV (t, delta * direction, &ot))
  400. {
  401. struct tm otm;
  402. if (! ranged_convert (convert, &ot, &otm))
  403. return -1;
  404. if (! isdst_differ (isdst, otm.tm_isdst))
  405. {
  406. /* We found the desired tm_isdst.
  407. Extrapolate back to the desired time. */
  408. long_int gt = ot + tm_diff (year, yday, hour, min, sec,
  409. &otm);
  410. if (mktime_min <= gt && gt <= mktime_max)
  411. {
  412. if (convert_time (convert, gt, &tm))
  413. {
  414. t = gt;
  415. goto offset_found;
  416. }
  417. if (errno != EOVERFLOW)
  418. return -1;
  419. }
  420. }
  421. }
  422. }
  423. /* No unusual DST offset was found nearby. Assume one-hour DST. */
  424. t += 60 * 60 * dst_difference;
  425. if (mktime_min <= t && t <= mktime_max && convert_time (convert, t, &tm))
  426. goto offset_found;
  427. __set_errno (EOVERFLOW);
  428. return -1;
  429. }
  430. offset_found:
  431. /* Set *OFFSET to the low-order bits of T - T0 - NEGATIVE_OFFSET_GUESS.
  432. This is just a heuristic to speed up the next mktime call, and
  433. correctness is unaffected if integer overflow occurs here. */
  434. INT_SUBTRACT_WRAPV (t, t0, offset);
  435. INT_SUBTRACT_WRAPV (*offset, negative_offset_guess, offset);
  436. if (LEAP_SECONDS_POSSIBLE && sec_requested != tm.tm_sec)
  437. {
  438. /* Adjust time to reflect the tm_sec requested, not the normalized value.
  439. Also, repair any damage from a false match due to a leap second. */
  440. long_int sec_adjustment = sec == 0 && tm.tm_sec == 60;
  441. sec_adjustment -= sec;
  442. sec_adjustment += sec_requested;
  443. if (INT_ADD_WRAPV (t, sec_adjustment, &t)
  444. || ! (mktime_min <= t && t <= mktime_max))
  445. {
  446. __set_errno (EOVERFLOW);
  447. return -1;
  448. }
  449. if (! convert_time (convert, t, &tm))
  450. return -1;
  451. }
  452. *tp = tm;
  453. return t;
  454. }
  455. #endif /* _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_INTERNAL */
  456. #if defined _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS
  457. /* Convert *TP to a __time64_t value. */
  458. __time64_t
  459. __mktime64 (struct tm *tp)
  460. {
  461. /* POSIX.1 8.1.1 requires that whenever mktime() is called, the
  462. time zone names contained in the external variable 'tzname' shall
  463. be set as if the tzset() function had been called. */
  464. __tzset ();
  465. # if defined _LIBC || NEED_MKTIME_WORKING
  466. static mktime_offset_t localtime_offset;
  467. return __mktime_internal (tp, __localtime64_r, &localtime_offset);
  468. # else
  469. # undef mktime
  470. return mktime (tp);
  471. # endif
  472. }
  473. #endif /* _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS */
  474. #if defined _LIBC && __TIMESIZE != 64
  475. libc_hidden_def (__mktime64)
  476. time_t
  477. mktime (struct tm *tp)
  478. {
  479. struct tm tm = *tp;
  480. __time64_t t = __mktime64 (&tm);
  481. if (in_time_t_range (t))
  482. {
  483. *tp = tm;
  484. return t;
  485. }
  486. else
  487. {
  488. __set_errno (EOVERFLOW);
  489. return -1;
  490. }
  491. }
  492. #endif
  493. weak_alias (mktime, timelocal)
  494. libc_hidden_def (mktime)
  495. libc_hidden_weak (timelocal)