tzfile.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <limits.h>
  16. #include <stdio.h>
  17. #include <stdio_ext.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <sys/stat.h>
  23. #include <stdint.h>
  24. #include <alloc_buffer.h>
  25. #include <set-freeres.h>
  26. #include <timezone/tzfile.h>
  27. int __use_tzfile;
  28. static dev_t tzfile_dev;
  29. static ino64_t tzfile_ino;
  30. static __time64_t tzfile_mtime;
  31. struct ttinfo
  32. {
  33. int offset; /* Seconds east of GMT. */
  34. unsigned char isdst; /* Used to set tm_isdst. */
  35. unsigned char idx; /* Index into `zone_names'. */
  36. unsigned char isstd; /* Transition times are in standard time. */
  37. unsigned char isgmt; /* Transition times are in GMT. */
  38. };
  39. struct leap
  40. {
  41. __time64_t transition; /* Time the transition takes effect. */
  42. long int change; /* Seconds of correction to apply. */
  43. };
  44. static size_t num_transitions;
  45. static __time64_t *transitions;
  46. static unsigned char *type_idxs;
  47. static size_t num_types;
  48. static struct ttinfo *types;
  49. static char *zone_names;
  50. static long int rule_stdoff;
  51. static long int rule_dstoff;
  52. static size_t num_leaps;
  53. static struct leap *leaps;
  54. static char *tzspec;
  55. /* Used to restore the daylight variable during time conversion, as if
  56. tzset had been called. */
  57. static int daylight_saved;
  58. #include <endian.h>
  59. #include <byteswap.h>
  60. /* Decode the four bytes at PTR as a signed integer in network byte order. */
  61. static inline int
  62. __attribute ((always_inline))
  63. decode (const void *ptr)
  64. {
  65. if (BYTE_ORDER == BIG_ENDIAN && sizeof (int) == 4)
  66. return *(const int *) ptr;
  67. if (sizeof (int) == 4)
  68. return bswap_32 (*(const int *) ptr);
  69. const unsigned char *p = ptr;
  70. int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
  71. result = (result << 8) | *p++;
  72. result = (result << 8) | *p++;
  73. result = (result << 8) | *p++;
  74. result = (result << 8) | *p++;
  75. return result;
  76. }
  77. static inline int64_t
  78. __attribute ((always_inline))
  79. decode64 (const void *ptr)
  80. {
  81. if ((BYTE_ORDER == BIG_ENDIAN))
  82. return *(const int64_t *) ptr;
  83. return bswap_64 (*(const int64_t *) ptr);
  84. }
  85. void
  86. __tzfile_read (const char *file, size_t extra, char **extrap)
  87. {
  88. static const char default_tzdir[] = TZDIR;
  89. size_t num_isstd, num_isgmt;
  90. FILE *f;
  91. struct tzhead tzhead;
  92. size_t chars;
  93. size_t i;
  94. int was_using_tzfile = __use_tzfile;
  95. int trans_width = 4;
  96. char *new = NULL;
  97. _Static_assert (sizeof (__time64_t) == 8,
  98. "__time64_t must be eight bytes");
  99. __use_tzfile = 0;
  100. if (file == NULL)
  101. /* No user specification; use the site-wide default. */
  102. file = TZDEFAULT;
  103. else if (*file == '\0')
  104. /* User specified the empty string; use UTC with no leap seconds. */
  105. goto ret_free_transitions;
  106. else
  107. {
  108. /* We must not allow to read an arbitrary file in a setuid
  109. program. So we fail for any file which is not in the
  110. directory hierarchy starting at TZDIR
  111. and which is not the system wide default TZDEFAULT. */
  112. if (__libc_enable_secure
  113. && ((*file == '/'
  114. && strcmp (file, TZDEFAULT) != 0
  115. && (strncmp (file, default_tzdir, sizeof (default_tzdir) - 1)
  116. != 0))
  117. || strstr (file, "../") != NULL))
  118. /* This test is certainly a bit too restrictive but it should
  119. catch all critical cases. */
  120. goto ret_free_transitions;
  121. }
  122. if (*file != '/')
  123. {
  124. const char *tzdir;
  125. tzdir = getenv ("TZDIR");
  126. if (tzdir == NULL || *tzdir == '\0')
  127. tzdir = default_tzdir;
  128. if (__asprintf (&new, "%s/%s", tzdir, file) == -1)
  129. goto ret_free_transitions;
  130. file = new;
  131. }
  132. /* If we were already using tzfile, check whether the file changed. */
  133. struct __stat64_t64 st;
  134. if (was_using_tzfile
  135. && __stat64_time64 (file, &st) == 0
  136. && tzfile_ino == st.st_ino && tzfile_dev == st.st_dev
  137. && tzfile_mtime == st.st_mtime)
  138. goto done; /* Nothing to do. */
  139. /* Note the file is opened with cancellation in the I/O functions
  140. disabled and if available FD_CLOEXEC set. */
  141. f = fopen (file, "rce");
  142. if (f == NULL)
  143. goto ret_free_transitions;
  144. /* Get information about the file we are actually using. */
  145. if (__fstat64_time64 (__fileno (f), &st) != 0)
  146. goto lose;
  147. free ((void *) transitions);
  148. transitions = NULL;
  149. /* Remember the inode and device number and modification time. */
  150. tzfile_dev = st.st_dev;
  151. tzfile_ino = st.st_ino;
  152. tzfile_mtime = st.st_mtime;
  153. /* No threads reading this stream. */
  154. __fsetlocking (f, FSETLOCKING_BYCALLER);
  155. read_again:
  156. if (__builtin_expect (__fread_unlocked ((void *) &tzhead, sizeof (tzhead),
  157. 1, f) != 1, 0)
  158. || memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic)) != 0)
  159. goto lose;
  160. num_transitions = (size_t) decode (tzhead.tzh_timecnt);
  161. num_types = (size_t) decode (tzhead.tzh_typecnt);
  162. chars = (size_t) decode (tzhead.tzh_charcnt);
  163. num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
  164. num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
  165. num_isgmt = (size_t) decode (tzhead.tzh_ttisutcnt);
  166. if (__glibc_unlikely (num_isstd > num_types || num_isgmt > num_types))
  167. goto lose;
  168. if (trans_width == 4 && tzhead.tzh_version[0] != '\0')
  169. {
  170. /* We use the 8-byte format. */
  171. trans_width = 8;
  172. /* Position the stream before the second header. */
  173. size_t to_skip = (num_transitions * (4 + 1)
  174. + num_types * 6
  175. + chars
  176. + num_leaps * 8
  177. + num_isstd
  178. + num_isgmt);
  179. if (fseek (f, to_skip, SEEK_CUR) != 0)
  180. goto lose;
  181. goto read_again;
  182. }
  183. /* Compute the size of the POSIX time zone specification in the
  184. file. */
  185. size_t tzspec_len;
  186. if (trans_width == 8)
  187. {
  188. off_t rem = st.st_size - __ftello (f);
  189. if (__builtin_expect (rem < 0
  190. || (size_t) rem < (num_transitions * (8 + 1)
  191. + num_types * 6
  192. + chars), 0))
  193. goto lose;
  194. tzspec_len = (size_t) rem - (num_transitions * (8 + 1)
  195. + num_types * 6
  196. + chars);
  197. if (__builtin_expect (num_leaps > SIZE_MAX / 12
  198. || tzspec_len < num_leaps * 12, 0))
  199. goto lose;
  200. tzspec_len -= num_leaps * 12;
  201. if (__glibc_unlikely (tzspec_len < num_isstd))
  202. goto lose;
  203. tzspec_len -= num_isstd;
  204. if (__glibc_unlikely (tzspec_len == 0 || tzspec_len - 1 < num_isgmt))
  205. goto lose;
  206. tzspec_len -= num_isgmt + 1;
  207. if (tzspec_len == 0)
  208. goto lose;
  209. }
  210. else
  211. tzspec_len = 0;
  212. /* The file is parsed into a single heap allocation, comprising of
  213. the following arrays:
  214. __time64_t transitions[num_transitions];
  215. struct leap leaps[num_leaps];
  216. struct ttinfo types[num_types];
  217. unsigned char type_idxs[num_types];
  218. char zone_names[chars];
  219. char tzspec[tzspec_len];
  220. char extra_array[extra]; // Stored into *pextras if requested.
  221. The piece-wise allocations from buf below verify that no
  222. overflow/wraparound occurred in these computations.
  223. The order of the suballocations is important for alignment
  224. purposes. __time64_t outside a struct may require more alignment
  225. then inside a struct on some architectures, so it must come
  226. first. */
  227. _Static_assert (__alignof (__time64_t) >= __alignof (struct leap),
  228. "alignment of __time64_t");
  229. _Static_assert (__alignof (struct leap) >= __alignof (struct ttinfo),
  230. "alignment of struct leap");
  231. struct alloc_buffer buf;
  232. {
  233. size_t total_size = (num_transitions * sizeof (__time64_t)
  234. + num_leaps * sizeof (struct leap)
  235. + num_types * sizeof (struct ttinfo)
  236. + num_transitions /* type_idxs */
  237. + chars /* zone_names */
  238. + tzspec_len + extra);
  239. transitions = malloc (total_size);
  240. if (transitions == NULL)
  241. goto lose;
  242. buf = alloc_buffer_create (transitions, total_size);
  243. }
  244. /* The address of the first allocation is already stored in the
  245. pointer transitions. */
  246. (void) alloc_buffer_alloc_array (&buf, __time64_t, num_transitions);
  247. leaps = alloc_buffer_alloc_array (&buf, struct leap, num_leaps);
  248. types = alloc_buffer_alloc_array (&buf, struct ttinfo, num_types);
  249. type_idxs = alloc_buffer_alloc_array (&buf, unsigned char, num_transitions);
  250. zone_names = alloc_buffer_alloc_array (&buf, char, chars);
  251. if (trans_width == 8)
  252. tzspec = alloc_buffer_alloc_array (&buf, char, tzspec_len);
  253. else
  254. tzspec = NULL;
  255. if (extra > 0)
  256. *extrap = alloc_buffer_alloc_array (&buf, char, extra);
  257. if (alloc_buffer_has_failed (&buf))
  258. goto lose;
  259. if (__glibc_unlikely (__fread_unlocked (transitions, trans_width,
  260. num_transitions, f)
  261. != num_transitions)
  262. || __glibc_unlikely (__fread_unlocked (type_idxs, 1, num_transitions, f)
  263. != num_transitions))
  264. goto lose;
  265. /* Check for bogus indices in the data file, so we can hereafter
  266. safely use type_idxs[T] as indices into `types' and never crash. */
  267. for (i = 0; i < num_transitions; ++i)
  268. if (__glibc_unlikely (type_idxs[i] >= num_types))
  269. goto lose;
  270. if (trans_width == 4)
  271. {
  272. /* Decode the transition times, stored as 4-byte integers in
  273. network (big-endian) byte order. We work from the end of the
  274. array so as not to clobber the next element to be
  275. processed. */
  276. i = num_transitions;
  277. while (i-- > 0)
  278. transitions[i] = decode ((char *) transitions + i * 4);
  279. }
  280. else if (BYTE_ORDER != BIG_ENDIAN)
  281. {
  282. /* Decode the transition times, stored as 8-byte integers in
  283. network (big-endian) byte order. */
  284. for (i = 0; i < num_transitions; ++i)
  285. transitions[i] = decode64 ((char *) transitions + i * 8);
  286. }
  287. for (i = 0; i < num_types; ++i)
  288. {
  289. unsigned char x[4];
  290. int c;
  291. if (__builtin_expect (__fread_unlocked (x, 1,
  292. sizeof (x), f) != sizeof (x),
  293. 0))
  294. goto lose;
  295. c = __getc_unlocked (f);
  296. if (__glibc_unlikely ((unsigned int) c > 1u))
  297. goto lose;
  298. types[i].isdst = c;
  299. c = __getc_unlocked (f);
  300. if (__glibc_unlikely ((size_t) c > chars))
  301. /* Bogus index in data file. */
  302. goto lose;
  303. types[i].idx = c;
  304. types[i].offset = decode (x);
  305. }
  306. if (__glibc_unlikely (__fread_unlocked (zone_names, 1, chars, f) != chars))
  307. goto lose;
  308. for (i = 0; i < num_leaps; ++i)
  309. {
  310. unsigned char x[8];
  311. if (__builtin_expect (__fread_unlocked (x, 1, trans_width, f)
  312. != trans_width, 0))
  313. goto lose;
  314. if (trans_width == 4)
  315. leaps[i].transition = decode (x);
  316. else
  317. leaps[i].transition = decode64 (x);
  318. if (__glibc_unlikely (__fread_unlocked (x, 1, 4, f) != 4))
  319. goto lose;
  320. leaps[i].change = (long int) decode (x);
  321. }
  322. for (i = 0; i < num_isstd; ++i)
  323. {
  324. int c = __getc_unlocked (f);
  325. if (__glibc_unlikely (c == EOF))
  326. goto lose;
  327. types[i].isstd = c != 0;
  328. }
  329. while (i < num_types)
  330. types[i++].isstd = 0;
  331. for (i = 0; i < num_isgmt; ++i)
  332. {
  333. int c = __getc_unlocked (f);
  334. if (__glibc_unlikely (c == EOF))
  335. goto lose;
  336. types[i].isgmt = c != 0;
  337. }
  338. while (i < num_types)
  339. types[i++].isgmt = 0;
  340. /* Read the POSIX TZ-style information if possible. */
  341. if (tzspec != NULL)
  342. {
  343. assert (tzspec_len > 0);
  344. /* Skip over the newline first. */
  345. if (__getc_unlocked (f) != '\n'
  346. || (__fread_unlocked (tzspec, 1, tzspec_len - 1, f)
  347. != tzspec_len - 1))
  348. tzspec = NULL;
  349. else
  350. tzspec[tzspec_len - 1] = '\0';
  351. }
  352. /* Don't use an empty TZ string. */
  353. if (tzspec != NULL && tzspec[0] == '\0')
  354. tzspec = NULL;
  355. fclose (f);
  356. /* First "register" all time zone abbreviations. */
  357. for (i = 0; i < num_types; ++i)
  358. if (__tzstring (&zone_names[types[i].idx]) == NULL)
  359. goto ret_free_transitions;
  360. /* Find the standard and daylight time offsets used by the rule file.
  361. We choose the offsets in the types of each flavor that are
  362. transitioned to earliest in time. */
  363. __tzname[0] = NULL;
  364. __tzname[1] = NULL;
  365. for (i = num_transitions; i > 0; )
  366. {
  367. int type = type_idxs[--i];
  368. int dst = types[type].isdst;
  369. if (__tzname[dst] == NULL)
  370. {
  371. int idx = types[type].idx;
  372. __tzname[dst] = __tzstring (&zone_names[idx]);
  373. if (__tzname[1 - dst] != NULL)
  374. break;
  375. }
  376. }
  377. if (__tzname[0] == NULL)
  378. {
  379. /* This should only happen if there are no transition rules.
  380. In this case there's usually only one single type, unless
  381. e.g. the data file has a truncated time-range. */
  382. __tzname[0] = __tzstring (zone_names);
  383. }
  384. if (__tzname[1] == NULL)
  385. __tzname[1] = __tzname[0];
  386. daylight_saved = 0;
  387. if (num_transitions == 0)
  388. /* Use the first rule (which should also be the only one). */
  389. rule_stdoff = rule_dstoff = types[0].offset;
  390. else
  391. {
  392. rule_stdoff = 0;
  393. /* Search for the last rule with a standard time offset. This
  394. will be used for the global timezone variable. */
  395. i = num_transitions - 1;
  396. do
  397. if (!types[type_idxs[i]].isdst)
  398. {
  399. rule_stdoff = types[type_idxs[i]].offset;
  400. break;
  401. }
  402. else
  403. daylight_saved = 1;
  404. while (i-- > 0);
  405. /* Keep searching to see if there is a DST rule. This
  406. information will be used to set the global daylight
  407. variable. */
  408. while (i-- > 0 && !daylight_saved)
  409. daylight_saved = types[type_idxs[i]].isdst;
  410. }
  411. __daylight = daylight_saved;
  412. __timezone = -rule_stdoff;
  413. done:
  414. __use_tzfile = 1;
  415. free (new);
  416. return;
  417. lose:
  418. fclose (f);
  419. ret_free_transitions:
  420. free (new);
  421. free ((void *) transitions);
  422. transitions = NULL;
  423. }
  424. /* The user specified a hand-made timezone, but not its DST rules.
  425. We will use the names and offsets from the user, and the rules
  426. from the TZDEFRULES file. */
  427. void
  428. __tzfile_default (const char *std, const char *dst,
  429. int stdoff, int dstoff)
  430. {
  431. size_t stdlen = strlen (std) + 1;
  432. size_t dstlen = strlen (dst) + 1;
  433. size_t i;
  434. int isdst;
  435. char *cp;
  436. __tzfile_read (TZDEFRULES, stdlen + dstlen, &cp);
  437. if (!__use_tzfile)
  438. return;
  439. if (num_types < 2)
  440. {
  441. __use_tzfile = 0;
  442. return;
  443. }
  444. /* Ignore the zone names read from the file and use the given ones
  445. instead. */
  446. __mempcpy (__mempcpy (cp, std, stdlen), dst, dstlen);
  447. zone_names = cp;
  448. /* Now there are only two zones, regardless of what the file contained. */
  449. num_types = 2;
  450. /* Now correct the transition times for the user-specified standard and
  451. daylight offsets from GMT. */
  452. isdst = 0;
  453. for (i = 0; i < num_transitions; ++i)
  454. {
  455. struct ttinfo *trans_type = &types[type_idxs[i]];
  456. /* We will use only types 0 (standard) and 1 (daylight).
  457. Fix up this transition to point to whichever matches
  458. the flavor of its original type. */
  459. type_idxs[i] = trans_type->isdst;
  460. if (trans_type->isgmt)
  461. /* The transition time is in GMT. No correction to apply. */ ;
  462. else if (isdst && !trans_type->isstd)
  463. /* The type says this transition is in "local wall clock time", and
  464. wall clock time as of the previous transition was DST. Correct
  465. for the difference between the rule's DST offset and the user's
  466. DST offset. */
  467. transitions[i] += dstoff - rule_dstoff;
  468. else
  469. /* This transition is in "local wall clock time", and wall clock
  470. time as of this iteration is non-DST. Correct for the
  471. difference between the rule's standard offset and the user's
  472. standard offset. */
  473. transitions[i] += stdoff - rule_stdoff;
  474. /* The DST state of "local wall clock time" for the next iteration is
  475. as specified by this transition. */
  476. isdst = trans_type->isdst;
  477. }
  478. /* Now that we adjusted the transitions to the requested offsets,
  479. reset the rule_stdoff and rule_dstoff values appropriately. They
  480. are used elsewhere. */
  481. rule_stdoff = stdoff;
  482. rule_dstoff = dstoff;
  483. /* Reset types 0 and 1 to describe the user's settings. */
  484. types[0].idx = 0;
  485. types[0].offset = stdoff;
  486. types[0].isdst = 0;
  487. types[1].idx = stdlen;
  488. types[1].offset = dstoff;
  489. types[1].isdst = 1;
  490. /* Reset time zone abbreviations to point to the user's abbreviations. */
  491. __tzname[0] = (char *) std;
  492. __tzname[1] = (char *) dst;
  493. /* Set the timezone. */
  494. __timezone = -types[0].offset;
  495. /* Invalidate the tzfile attribute cache to force rereading
  496. TZDEFRULES the next time it is used. */
  497. tzfile_dev = 0;
  498. tzfile_ino = 0;
  499. tzfile_mtime = 0;
  500. }
  501. void
  502. __tzfile_compute (__time64_t timer, int use_localtime,
  503. long int *leap_correct, int *leap_hit,
  504. struct tm *tp)
  505. {
  506. size_t i;
  507. if (use_localtime)
  508. {
  509. __tzname[0] = NULL;
  510. __tzname[1] = NULL;
  511. if (__glibc_unlikely (num_transitions == 0 || timer < transitions[0]))
  512. {
  513. /* TIMER is before any transition (or there are no transitions).
  514. Choose the first non-DST type
  515. (or the first if they're all DST types). */
  516. i = 0;
  517. while (i < num_types && types[i].isdst)
  518. {
  519. if (__tzname[1] == NULL)
  520. __tzname[1] = __tzstring (&zone_names[types[i].idx]);
  521. ++i;
  522. }
  523. if (i == num_types)
  524. i = 0;
  525. __tzname[0] = __tzstring (&zone_names[types[i].idx]);
  526. if (__tzname[1] == NULL)
  527. {
  528. size_t j = i;
  529. while (j < num_types)
  530. if (types[j].isdst)
  531. {
  532. __tzname[1] = __tzstring (&zone_names[types[j].idx]);
  533. break;
  534. }
  535. else
  536. ++j;
  537. }
  538. }
  539. else if (__glibc_unlikely (timer >= transitions[num_transitions - 1]))
  540. {
  541. if (__glibc_unlikely (tzspec == NULL))
  542. {
  543. use_last:
  544. i = num_transitions;
  545. goto found;
  546. }
  547. /* Parse the POSIX TZ-style string. */
  548. __tzset_parse_tz (tzspec);
  549. /* Convert to broken down structure. If this fails do not
  550. use the string. */
  551. if (__glibc_unlikely (! __offtime (timer, 0, tp)))
  552. goto use_last;
  553. /* Use the rules from the TZ string to compute the change. */
  554. __tz_compute (timer, tp, 1);
  555. /* If tzspec comes from posixrules loaded by __tzfile_default,
  556. override the STD and DST zone names with the ones user
  557. requested in TZ envvar. */
  558. if (__glibc_unlikely (zone_names == (char *) &leaps[num_leaps]))
  559. {
  560. assert (num_types == 2);
  561. __tzname[0] = __tzstring (zone_names);
  562. __tzname[1] = __tzstring (&zone_names[strlen (zone_names) + 1]);
  563. }
  564. goto leap;
  565. }
  566. else
  567. {
  568. /* Find the first transition after TIMER, and
  569. then pick the type of the transition before it. */
  570. size_t lo = 0;
  571. size_t hi = num_transitions - 1;
  572. /* Assume that DST is changing twice a year and guess
  573. initial search spot from it. Half of a gregorian year
  574. has on average 365.2425 * 86400 / 2 = 15778476 seconds.
  575. The value i can be truncated if size_t is smaller than
  576. __time64_t, but this is harmless because it is just
  577. a guess. */
  578. i = (transitions[num_transitions - 1] - timer) / 15778476;
  579. if (i < num_transitions)
  580. {
  581. i = num_transitions - 1 - i;
  582. if (timer < transitions[i])
  583. {
  584. if (i < 10 || timer >= transitions[i - 10])
  585. {
  586. /* Linear search. */
  587. while (timer < transitions[i - 1])
  588. --i;
  589. goto found;
  590. }
  591. hi = i - 10;
  592. }
  593. else
  594. {
  595. if (i + 10 >= num_transitions || timer < transitions[i + 10])
  596. {
  597. /* Linear search. */
  598. while (timer >= transitions[i])
  599. ++i;
  600. goto found;
  601. }
  602. lo = i + 10;
  603. }
  604. }
  605. /* Binary search. */
  606. /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
  607. while (lo + 1 < hi)
  608. {
  609. i = (lo + hi) / 2;
  610. if (timer < transitions[i])
  611. hi = i;
  612. else
  613. lo = i;
  614. }
  615. i = hi;
  616. found:
  617. /* assert (timer >= transitions[i - 1]
  618. && (i == num_transitions || timer < transitions[i])); */
  619. __tzname[types[type_idxs[i - 1]].isdst]
  620. = __tzstring (&zone_names[types[type_idxs[i - 1]].idx]);
  621. size_t j = i;
  622. while (j < num_transitions)
  623. {
  624. int type = type_idxs[j];
  625. int dst = types[type].isdst;
  626. int idx = types[type].idx;
  627. if (__tzname[dst] == NULL)
  628. {
  629. __tzname[dst] = __tzstring (&zone_names[idx]);
  630. if (__tzname[1 - dst] != NULL)
  631. break;
  632. }
  633. ++j;
  634. }
  635. if (__glibc_unlikely (__tzname[0] == NULL))
  636. __tzname[0] = __tzname[1];
  637. i = type_idxs[i - 1];
  638. }
  639. struct ttinfo *info = &types[i];
  640. __daylight = daylight_saved;
  641. __timezone = -rule_stdoff;
  642. if (__tzname[0] == NULL)
  643. {
  644. /* This should only happen if there are no transition rules.
  645. In this case there should be only one single type. */
  646. assert (num_types == 1);
  647. __tzname[0] = __tzstring (zone_names);
  648. }
  649. if (__tzname[1] == NULL)
  650. /* There is no daylight saving time. */
  651. __tzname[1] = __tzname[0];
  652. tp->tm_isdst = info->isdst;
  653. assert (strcmp (&zone_names[info->idx], __tzname[tp->tm_isdst]) == 0);
  654. tp->tm_zone = __tzname[tp->tm_isdst];
  655. tp->tm_gmtoff = info->offset;
  656. }
  657. leap:
  658. *leap_correct = 0L;
  659. *leap_hit = 0;
  660. /* Find the last leap second correction transition time before TIMER. */
  661. i = num_leaps;
  662. do
  663. if (i-- == 0)
  664. return;
  665. while (timer < leaps[i].transition);
  666. /* Apply its correction. */
  667. *leap_correct = leaps[i].change;
  668. if (timer == leaps[i].transition /* Exactly at the transition time. */
  669. && (leaps[i].change > (i == 0 ? 0 : leaps[i - 1].change)))
  670. {
  671. *leap_hit = 1;
  672. while (i > 0
  673. && leaps[i].transition == leaps[i - 1].transition + 1
  674. && leaps[i].change == leaps[i - 1].change + 1)
  675. {
  676. ++*leap_hit;
  677. --i;
  678. }
  679. }
  680. }
  681. weak_alias (transitions, __libc_tzfile_freemem_ptr)