private.h 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. /* Private header for tzdb code. */
  2. #ifndef PRIVATE_H
  3. #define PRIVATE_H
  4. /*
  5. ** This file is in the public domain, so clarified as of
  6. ** 1996-06-05 by Arthur David Olson.
  7. */
  8. /*
  9. ** This header is for use ONLY with the time conversion code.
  10. ** There is no guarantee that it will remain unchanged,
  11. ** or that it will remain at all.
  12. ** Do NOT copy it to any system include directory.
  13. ** Thank you!
  14. */
  15. /* PORT_TO_C89 means the code should work even if the underlying
  16. compiler and library support only C89 plus C99's 'long long'
  17. and perhaps a few other extensions to C89.
  18. This macro is obsolescent, and the plan is to remove it along with
  19. associated code. A good time to do that might be in the year 2029
  20. because RHEL 7 (whose GCC defaults to C89) extended life cycle
  21. support (ELS) is scheduled to end on 2028-06-30. */
  22. #ifndef PORT_TO_C89
  23. # define PORT_TO_C89 0
  24. #endif
  25. /* SUPPORT_C89 means the tzcode library should support C89 callers
  26. in addition to the usual support for C99-and-later callers.
  27. This defaults to 1 as POSIX requires, even though that can trigger
  28. latent bugs in callers. */
  29. #ifndef SUPPORT_C89
  30. # define SUPPORT_C89 1
  31. #endif
  32. #ifndef __STDC_VERSION__
  33. # define __STDC_VERSION__ 0
  34. #endif
  35. /* Define true, false and bool if they don't work out of the box. */
  36. #if PORT_TO_C89 && __STDC_VERSION__ < 199901
  37. # define true 1
  38. # define false 0
  39. # define bool int
  40. #elif __STDC_VERSION__ < 202311
  41. # include <stdbool.h>
  42. #endif
  43. #if __STDC_VERSION__ < 202311
  44. # define static_assert(cond) extern int static_assert_check[(cond) ? 1 : -1]
  45. #endif
  46. /*
  47. ** zdump has been made independent of the rest of the time
  48. ** conversion package to increase confidence in the verification it provides.
  49. ** You can use zdump to help in verifying other implementations.
  50. ** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
  51. */
  52. #ifndef USE_LTZ
  53. # define USE_LTZ 1
  54. #endif
  55. /* This string was in the Factory zone through version 2016f. */
  56. #define GRANDPARENTED "Local time zone must be set--see zic manual page"
  57. /*
  58. ** Defaults for preprocessor symbols.
  59. ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
  60. */
  61. #if !defined HAVE__GENERIC && defined __has_extension
  62. # if !__has_extension(c_generic_selections)
  63. # define HAVE__GENERIC 0
  64. # endif
  65. #endif
  66. /* _Generic is buggy in pre-4.9 GCC. */
  67. #if !defined HAVE__GENERIC && defined __GNUC__ && !defined __STRICT_ANSI__
  68. # define HAVE__GENERIC (4 < __GNUC__ + (9 <= __GNUC_MINOR__))
  69. #endif
  70. #ifndef HAVE__GENERIC
  71. # define HAVE__GENERIC (201112 <= __STDC_VERSION__)
  72. #endif
  73. #if !defined HAVE_GETTEXT && defined __has_include
  74. # if __has_include(<libintl.h>)
  75. # define HAVE_GETTEXT true
  76. # endif
  77. #endif
  78. #ifndef HAVE_GETTEXT
  79. # define HAVE_GETTEXT false
  80. #endif
  81. #ifndef HAVE_INCOMPATIBLE_CTIME_R
  82. # define HAVE_INCOMPATIBLE_CTIME_R 0
  83. #endif
  84. #ifndef HAVE_LINK
  85. # define HAVE_LINK 1
  86. #endif /* !defined HAVE_LINK */
  87. #ifndef HAVE_MALLOC_ERRNO
  88. # define HAVE_MALLOC_ERRNO 1
  89. #endif
  90. #ifndef HAVE_POSIX_DECLS
  91. # define HAVE_POSIX_DECLS 1
  92. #endif
  93. #ifndef HAVE_SETENV
  94. # define HAVE_SETENV 1
  95. #endif
  96. #ifndef HAVE_STRDUP
  97. # define HAVE_STRDUP 1
  98. #endif
  99. #ifndef HAVE_SYMLINK
  100. # define HAVE_SYMLINK 1
  101. #endif /* !defined HAVE_SYMLINK */
  102. #if !defined HAVE_SYS_STAT_H && defined __has_include
  103. # if !__has_include(<sys/stat.h>)
  104. # define HAVE_SYS_STAT_H false
  105. # endif
  106. #endif
  107. #ifndef HAVE_SYS_STAT_H
  108. # define HAVE_SYS_STAT_H true
  109. #endif
  110. #if !defined HAVE_UNISTD_H && defined __has_include
  111. # if !__has_include(<unistd.h>)
  112. # define HAVE_UNISTD_H false
  113. # endif
  114. #endif
  115. #ifndef HAVE_UNISTD_H
  116. # define HAVE_UNISTD_H true
  117. #endif
  118. #ifndef NETBSD_INSPIRED
  119. # define NETBSD_INSPIRED 1
  120. #endif
  121. #if HAVE_INCOMPATIBLE_CTIME_R
  122. # define asctime_r _incompatible_asctime_r
  123. # define ctime_r _incompatible_ctime_r
  124. #endif /* HAVE_INCOMPATIBLE_CTIME_R */
  125. /* Enable tm_gmtoff, tm_zone, and environ on GNUish systems. */
  126. #define _GNU_SOURCE 1
  127. /* Fix asctime_r on Solaris 11. */
  128. #define _POSIX_PTHREAD_SEMANTICS 1
  129. /* Enable strtoimax on pre-C99 Solaris 11. */
  130. #define __EXTENSIONS__ 1
  131. /* On GNUish systems where time_t might be 32 or 64 bits, use 64.
  132. On these platforms _FILE_OFFSET_BITS must also be 64; otherwise
  133. setting _TIME_BITS to 64 does not work. The code does not
  134. otherwise rely on _FILE_OFFSET_BITS being 64, since it does not
  135. use off_t or functions like 'stat' that depend on off_t. */
  136. #ifndef _FILE_OFFSET_BITS
  137. # define _FILE_OFFSET_BITS 64
  138. #endif
  139. #if !defined _TIME_BITS && _FILE_OFFSET_BITS == 64
  140. # define _TIME_BITS 64
  141. #endif
  142. /*
  143. ** Nested includes
  144. */
  145. /* Avoid clashes with NetBSD by renaming NetBSD's declarations.
  146. If defining the 'timezone' variable, avoid a clash with FreeBSD's
  147. 'timezone' function by renaming its declaration. */
  148. #define localtime_rz sys_localtime_rz
  149. #define mktime_z sys_mktime_z
  150. #define posix2time_z sys_posix2time_z
  151. #define time2posix_z sys_time2posix_z
  152. #if defined USG_COMPAT && USG_COMPAT == 2
  153. # define timezone sys_timezone
  154. #endif
  155. #define timezone_t sys_timezone_t
  156. #define tzalloc sys_tzalloc
  157. #define tzfree sys_tzfree
  158. #include <time.h>
  159. #undef localtime_rz
  160. #undef mktime_z
  161. #undef posix2time_z
  162. #undef time2posix_z
  163. #if defined USG_COMPAT && USG_COMPAT == 2
  164. # undef timezone
  165. #endif
  166. #undef timezone_t
  167. #undef tzalloc
  168. #undef tzfree
  169. #include <stddef.h>
  170. #include <string.h>
  171. #if !PORT_TO_C89
  172. # include <inttypes.h>
  173. #endif
  174. #include <limits.h> /* for CHAR_BIT et al. */
  175. #include <stdlib.h>
  176. #include <errno.h>
  177. #ifndef EINVAL
  178. # define EINVAL ERANGE
  179. #endif
  180. #ifndef ELOOP
  181. # define ELOOP EINVAL
  182. #endif
  183. #ifndef ENAMETOOLONG
  184. # define ENAMETOOLONG EINVAL
  185. #endif
  186. #ifndef ENOMEM
  187. # define ENOMEM EINVAL
  188. #endif
  189. #ifndef ENOTSUP
  190. # define ENOTSUP EINVAL
  191. #endif
  192. #ifndef EOVERFLOW
  193. # define EOVERFLOW EINVAL
  194. #endif
  195. #if HAVE_GETTEXT
  196. # include <libintl.h>
  197. #endif /* HAVE_GETTEXT */
  198. #if HAVE_UNISTD_H
  199. # include <unistd.h> /* for R_OK, and other POSIX goodness */
  200. #endif /* HAVE_UNISTD_H */
  201. /* SUPPORT_POSIX2008 means the tzcode library should support
  202. POSIX.1-2017-and-earlier callers in addition to the usual support for
  203. POSIX.1-2024-and-later callers; however, this can be
  204. incompatible with POSIX.1-2024-and-later callers.
  205. This macro is obsolescent, and the plan is to remove it
  206. along with any code needed only when it is nonzero.
  207. A good time to do that might be in the year 2034.
  208. This macro's name is SUPPORT_POSIX2008 because _POSIX_VERSION == 200809
  209. in POSIX.1-2017, a minor revision of POSIX.1-2008. */
  210. #ifndef SUPPORT_POSIX2008
  211. # if defined _POSIX_VERSION && _POSIX_VERSION <= 200809
  212. # define SUPPORT_POSIX2008 1
  213. # else
  214. # define SUPPORT_POSIX2008 0
  215. # endif
  216. #endif
  217. #ifndef HAVE_DECL_ASCTIME_R
  218. # if SUPPORT_POSIX2008
  219. # define HAVE_DECL_ASCTIME_R 1
  220. # else
  221. # define HAVE_DECL_ASCTIME_R 0
  222. # endif
  223. #endif
  224. #ifndef HAVE_STRFTIME_L
  225. # if _POSIX_VERSION < 200809
  226. # define HAVE_STRFTIME_L 0
  227. # else
  228. # define HAVE_STRFTIME_L 1
  229. # endif
  230. #endif
  231. #ifndef USG_COMPAT
  232. # ifndef _XOPEN_VERSION
  233. # define USG_COMPAT 0
  234. # else
  235. # define USG_COMPAT 1
  236. # endif
  237. #endif
  238. #ifndef HAVE_TZNAME
  239. # if _POSIX_VERSION < 198808 && !USG_COMPAT
  240. # define HAVE_TZNAME 0
  241. # else
  242. # define HAVE_TZNAME 1
  243. # endif
  244. #endif
  245. #ifndef ALTZONE
  246. # if defined __sun || defined _M_XENIX
  247. # define ALTZONE 1
  248. # else
  249. # define ALTZONE 0
  250. # endif
  251. #endif
  252. #ifndef R_OK
  253. # define R_OK 4
  254. #endif /* !defined R_OK */
  255. #if PORT_TO_C89
  256. /*
  257. ** Define HAVE_STDINT_H's default value here, rather than at the
  258. ** start, since __GLIBC__ and INTMAX_MAX's values depend on
  259. ** previously included files. glibc 2.1 and Solaris 10 and later have
  260. ** stdint.h, even with pre-C99 compilers.
  261. */
  262. #if !defined HAVE_STDINT_H && defined __has_include
  263. # define HAVE_STDINT_H true /* C23 __has_include implies C99 stdint.h. */
  264. #endif
  265. #ifndef HAVE_STDINT_H
  266. # define HAVE_STDINT_H \
  267. (199901 <= __STDC_VERSION__ \
  268. || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
  269. || __CYGWIN__ || INTMAX_MAX)
  270. #endif /* !defined HAVE_STDINT_H */
  271. #if HAVE_STDINT_H
  272. # include <stdint.h>
  273. #endif /* !HAVE_STDINT_H */
  274. #ifndef HAVE_INTTYPES_H
  275. # define HAVE_INTTYPES_H HAVE_STDINT_H
  276. #endif
  277. #if HAVE_INTTYPES_H
  278. # include <inttypes.h>
  279. #endif
  280. /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
  281. #if defined __LONG_LONG_MAX__ && !defined __STRICT_ANSI__
  282. # ifndef LLONG_MAX
  283. # define LLONG_MAX __LONG_LONG_MAX__
  284. # endif
  285. # ifndef LLONG_MIN
  286. # define LLONG_MIN (-1 - LLONG_MAX)
  287. # endif
  288. # ifndef ULLONG_MAX
  289. # define ULLONG_MAX (LLONG_MAX * 2ull + 1)
  290. # endif
  291. #endif
  292. #ifndef INT_FAST64_MAX
  293. # if 1 <= LONG_MAX >> 31 >> 31
  294. typedef long int_fast64_t;
  295. # define INT_FAST64_MIN LONG_MIN
  296. # define INT_FAST64_MAX LONG_MAX
  297. # else
  298. /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
  299. typedef long long int_fast64_t;
  300. # define INT_FAST64_MIN LLONG_MIN
  301. # define INT_FAST64_MAX LLONG_MAX
  302. # endif
  303. #endif
  304. #ifndef PRIdFAST64
  305. # if INT_FAST64_MAX == LONG_MAX
  306. # define PRIdFAST64 "ld"
  307. # else
  308. # define PRIdFAST64 "lld"
  309. # endif
  310. #endif
  311. #ifndef SCNdFAST64
  312. # define SCNdFAST64 PRIdFAST64
  313. #endif
  314. #ifndef INT_FAST32_MAX
  315. # if INT_MAX >> 31 == 0
  316. typedef long int_fast32_t;
  317. # define INT_FAST32_MAX LONG_MAX
  318. # define INT_FAST32_MIN LONG_MIN
  319. # else
  320. typedef int int_fast32_t;
  321. # define INT_FAST32_MAX INT_MAX
  322. # define INT_FAST32_MIN INT_MIN
  323. # endif
  324. #endif
  325. #ifndef INTMAX_MAX
  326. # ifdef LLONG_MAX
  327. typedef long long intmax_t;
  328. # ifndef HAVE_STRTOLL
  329. # define HAVE_STRTOLL true
  330. # endif
  331. # if HAVE_STRTOLL
  332. # define strtoimax strtoll
  333. # endif
  334. # define INTMAX_MAX LLONG_MAX
  335. # define INTMAX_MIN LLONG_MIN
  336. # else
  337. typedef long intmax_t;
  338. # define INTMAX_MAX LONG_MAX
  339. # define INTMAX_MIN LONG_MIN
  340. # endif
  341. # ifndef strtoimax
  342. # define strtoimax strtol
  343. # endif
  344. #endif
  345. #ifndef PRIdMAX
  346. # if INTMAX_MAX == LLONG_MAX
  347. # define PRIdMAX "lld"
  348. # else
  349. # define PRIdMAX "ld"
  350. # endif
  351. #endif
  352. #ifndef PTRDIFF_MAX
  353. # define PTRDIFF_MAX MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t))
  354. #endif
  355. #ifndef UINT_FAST32_MAX
  356. typedef unsigned long uint_fast32_t;
  357. #endif
  358. #ifndef UINT_FAST64_MAX
  359. # if 3 <= ULONG_MAX >> 31 >> 31
  360. typedef unsigned long uint_fast64_t;
  361. # define UINT_FAST64_MAX ULONG_MAX
  362. # else
  363. /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
  364. typedef unsigned long long uint_fast64_t;
  365. # define UINT_FAST64_MAX ULLONG_MAX
  366. # endif
  367. #endif
  368. #ifndef UINTMAX_MAX
  369. # ifdef ULLONG_MAX
  370. typedef unsigned long long uintmax_t;
  371. # define UINTMAX_MAX ULLONG_MAX
  372. # else
  373. typedef unsigned long uintmax_t;
  374. # define UINTMAX_MAX ULONG_MAX
  375. # endif
  376. #endif
  377. #ifndef PRIuMAX
  378. # ifdef ULLONG_MAX
  379. # define PRIuMAX "llu"
  380. # else
  381. # define PRIuMAX "lu"
  382. # endif
  383. #endif
  384. #ifndef SIZE_MAX
  385. # define SIZE_MAX ((size_t) -1)
  386. #endif
  387. #endif /* PORT_TO_C89 */
  388. /* The maximum size of any created object, as a signed integer.
  389. Although the C standard does not outright prohibit larger objects,
  390. behavior is undefined if the result of pointer subtraction does not
  391. fit into ptrdiff_t, and the code assumes in several places that
  392. pointer subtraction works. As a practical matter it's OK to not
  393. support objects larger than this. */
  394. #define INDEX_MAX ((ptrdiff_t) min(PTRDIFF_MAX, SIZE_MAX))
  395. /* Support ckd_add, ckd_sub, ckd_mul on C23 or recent-enough GCC-like
  396. hosts, unless compiled with -DHAVE_STDCKDINT_H=0 or with pre-C23 EDG. */
  397. #if !defined HAVE_STDCKDINT_H && defined __has_include
  398. # if __has_include(<stdckdint.h>)
  399. # define HAVE_STDCKDINT_H true
  400. # endif
  401. #endif
  402. #ifdef HAVE_STDCKDINT_H
  403. # if HAVE_STDCKDINT_H
  404. # include <stdckdint.h>
  405. # endif
  406. #elif defined __EDG__
  407. /* Do nothing, to work around EDG bug <https://bugs.gnu.org/53256>. */
  408. #elif defined __has_builtin
  409. # if __has_builtin(__builtin_add_overflow)
  410. # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
  411. # endif
  412. # if __has_builtin(__builtin_sub_overflow)
  413. # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
  414. # endif
  415. # if __has_builtin(__builtin_mul_overflow)
  416. # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
  417. # endif
  418. #elif 7 <= __GNUC__
  419. # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
  420. # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
  421. # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
  422. #endif
  423. #if (defined __has_c_attribute \
  424. && (202311 <= __STDC_VERSION__ || !defined __STRICT_ANSI__))
  425. # define HAVE___HAS_C_ATTRIBUTE true
  426. #else
  427. # define HAVE___HAS_C_ATTRIBUTE false
  428. #endif
  429. #if HAVE___HAS_C_ATTRIBUTE
  430. # if __has_c_attribute(deprecated)
  431. # define ATTRIBUTE_DEPRECATED [[deprecated]]
  432. # endif
  433. #endif
  434. #ifndef ATTRIBUTE_DEPRECATED
  435. # if 3 < __GNUC__ + (2 <= __GNUC_MINOR__)
  436. # define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
  437. # else
  438. # define ATTRIBUTE_DEPRECATED /* empty */
  439. # endif
  440. #endif
  441. #if HAVE___HAS_C_ATTRIBUTE
  442. # if __has_c_attribute(fallthrough)
  443. # define ATTRIBUTE_FALLTHROUGH [[fallthrough]]
  444. # endif
  445. #endif
  446. #ifndef ATTRIBUTE_FALLTHROUGH
  447. # if 7 <= __GNUC__
  448. # define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
  449. # else
  450. # define ATTRIBUTE_FALLTHROUGH ((void) 0)
  451. # endif
  452. #endif
  453. #if HAVE___HAS_C_ATTRIBUTE
  454. # if __has_c_attribute(maybe_unused)
  455. # define ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]]
  456. # endif
  457. #endif
  458. #ifndef ATTRIBUTE_MAYBE_UNUSED
  459. # if 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
  460. # define ATTRIBUTE_MAYBE_UNUSED __attribute__((unused))
  461. # else
  462. # define ATTRIBUTE_MAYBE_UNUSED /* empty */
  463. # endif
  464. #endif
  465. #if HAVE___HAS_C_ATTRIBUTE
  466. # if __has_c_attribute(noreturn)
  467. # define ATTRIBUTE_NORETURN [[noreturn]]
  468. # endif
  469. #endif
  470. #ifndef ATTRIBUTE_NORETURN
  471. # if 201112 <= __STDC_VERSION__
  472. # define ATTRIBUTE_NORETURN _Noreturn
  473. # elif 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
  474. # define ATTRIBUTE_NORETURN __attribute__((noreturn))
  475. # else
  476. # define ATTRIBUTE_NORETURN /* empty */
  477. # endif
  478. #endif
  479. #if HAVE___HAS_C_ATTRIBUTE
  480. # if __has_c_attribute(reproducible)
  481. # define ATTRIBUTE_REPRODUCIBLE [[reproducible]]
  482. # endif
  483. #endif
  484. #ifndef ATTRIBUTE_REPRODUCIBLE
  485. # define ATTRIBUTE_REPRODUCIBLE /* empty */
  486. #endif
  487. /* GCC attributes that are useful in tzcode.
  488. __attribute__((pure)) is stricter than [[reproducible]],
  489. so the latter is an adequate substitute in non-GCC C23 platforms. */
  490. #if __GNUC__ < 3
  491. # define ATTRIBUTE_FORMAT(spec) /* empty */
  492. # define ATTRIBUTE_PURE ATTRIBUTE_REPRODUCIBLE
  493. #else
  494. # define ATTRIBUTE_FORMAT(spec) __attribute__((format spec))
  495. # define ATTRIBUTE_PURE __attribute__((pure))
  496. #endif
  497. /* Avoid GCC bug 114833 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114833>.
  498. Remove this macro and its uses when the bug is fixed in a GCC release,
  499. because only the latest GCC matters for $(GCC_DEBUG_FLAGS). */
  500. #ifdef GCC_LINT
  501. # define ATTRIBUTE_PURE_114833 ATTRIBUTE_PURE
  502. #else
  503. # define ATTRIBUTE_PURE_114833 /* empty */
  504. #endif
  505. #if (__STDC_VERSION__ < 199901 && !defined restrict \
  506. && (PORT_TO_C89 || defined _MSC_VER))
  507. # define restrict /* empty */
  508. #endif
  509. /*
  510. ** Workarounds for compilers/systems.
  511. */
  512. #ifndef EPOCH_LOCAL
  513. # define EPOCH_LOCAL 0
  514. #endif
  515. #ifndef EPOCH_OFFSET
  516. # define EPOCH_OFFSET 0
  517. #endif
  518. #ifndef RESERVE_STD_EXT_IDS
  519. # define RESERVE_STD_EXT_IDS 0
  520. #endif
  521. /* If standard C identifiers with external linkage (e.g., localtime)
  522. are reserved and are not already being renamed anyway, rename them
  523. as if compiling with '-Dtime_tz=time_t'. */
  524. #if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
  525. # define time_tz time_t
  526. #endif
  527. /*
  528. ** Compile with -Dtime_tz=T to build the tz package with a private
  529. ** time_t type equivalent to T rather than the system-supplied time_t.
  530. ** This debugging feature can test unusual design decisions
  531. ** (e.g., time_t wider than 'long', or unsigned time_t) even on
  532. ** typical platforms.
  533. */
  534. #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
  535. # define TZ_TIME_T 1
  536. #else
  537. # define TZ_TIME_T 0
  538. #endif
  539. #if defined LOCALTIME_IMPLEMENTATION && TZ_TIME_T
  540. static time_t sys_time(time_t *x) { return time(x); }
  541. #endif
  542. #if TZ_TIME_T
  543. typedef time_tz tz_time_t;
  544. # undef asctime
  545. # define asctime tz_asctime
  546. # undef ctime
  547. # define ctime tz_ctime
  548. # undef difftime
  549. # define difftime tz_difftime
  550. # undef gmtime
  551. # define gmtime tz_gmtime
  552. # undef gmtime_r
  553. # define gmtime_r tz_gmtime_r
  554. # undef localtime
  555. # define localtime tz_localtime
  556. # undef localtime_r
  557. # define localtime_r tz_localtime_r
  558. # undef localtime_rz
  559. # define localtime_rz tz_localtime_rz
  560. # undef mktime
  561. # define mktime tz_mktime
  562. # undef mktime_z
  563. # define mktime_z tz_mktime_z
  564. # undef offtime
  565. # define offtime tz_offtime
  566. # undef posix2time
  567. # define posix2time tz_posix2time
  568. # undef posix2time_z
  569. # define posix2time_z tz_posix2time_z
  570. # undef strftime
  571. # define strftime tz_strftime
  572. # undef time
  573. # define time tz_time
  574. # undef time2posix
  575. # define time2posix tz_time2posix
  576. # undef time2posix_z
  577. # define time2posix_z tz_time2posix_z
  578. # undef time_t
  579. # define time_t tz_time_t
  580. # undef timegm
  581. # define timegm tz_timegm
  582. # undef timelocal
  583. # define timelocal tz_timelocal
  584. # undef timeoff
  585. # define timeoff tz_timeoff
  586. # undef tzalloc
  587. # define tzalloc tz_tzalloc
  588. # undef tzfree
  589. # define tzfree tz_tzfree
  590. # undef tzset
  591. # define tzset tz_tzset
  592. # if SUPPORT_POSIX2008
  593. # undef asctime_r
  594. # define asctime_r tz_asctime_r
  595. # undef ctime_r
  596. # define ctime_r tz_ctime_r
  597. # endif
  598. # if HAVE_STRFTIME_L
  599. # undef strftime_l
  600. # define strftime_l tz_strftime_l
  601. # endif
  602. # if HAVE_TZNAME
  603. # undef tzname
  604. # define tzname tz_tzname
  605. # endif
  606. # if USG_COMPAT
  607. # undef daylight
  608. # define daylight tz_daylight
  609. # undef timezone
  610. # define timezone tz_timezone
  611. # endif
  612. # if ALTZONE
  613. # undef altzone
  614. # define altzone tz_altzone
  615. # endif
  616. # if __STDC_VERSION__ < 202311
  617. # define DEPRECATED_IN_C23 /* empty */
  618. # else
  619. # define DEPRECATED_IN_C23 ATTRIBUTE_DEPRECATED
  620. # endif
  621. DEPRECATED_IN_C23 char *asctime(struct tm const *);
  622. DEPRECATED_IN_C23 char *ctime(time_t const *);
  623. #if SUPPORT_POSIX2008
  624. char *asctime_r(struct tm const *restrict, char *restrict);
  625. char *ctime_r(time_t const *, char *);
  626. #endif
  627. double difftime(time_t, time_t);
  628. size_t strftime(char *restrict, size_t, char const *restrict,
  629. struct tm const *restrict);
  630. # if HAVE_STRFTIME_L
  631. size_t strftime_l(char *restrict, size_t, char const *restrict,
  632. struct tm const *restrict, locale_t);
  633. # endif
  634. struct tm *gmtime(time_t const *);
  635. struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
  636. struct tm *localtime(time_t const *);
  637. struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
  638. time_t mktime(struct tm *);
  639. time_t time(time_t *);
  640. time_t timegm(struct tm *);
  641. void tzset(void);
  642. #endif
  643. #ifndef HAVE_DECL_TIMEGM
  644. # if (202311 <= __STDC_VERSION__ \
  645. || defined __GLIBC__ || defined __tm_zone /* musl */ \
  646. || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
  647. || (defined __APPLE__ && defined __MACH__))
  648. # define HAVE_DECL_TIMEGM true
  649. # else
  650. # define HAVE_DECL_TIMEGM false
  651. # endif
  652. #endif
  653. #if !HAVE_DECL_TIMEGM && !defined timegm
  654. time_t timegm(struct tm *);
  655. #endif
  656. #if !HAVE_DECL_ASCTIME_R && !defined asctime_r && SUPPORT_POSIX2008
  657. extern char *asctime_r(struct tm const *restrict, char *restrict);
  658. #endif
  659. #ifndef HAVE_DECL_ENVIRON
  660. # if defined environ || defined __USE_GNU
  661. # define HAVE_DECL_ENVIRON 1
  662. # else
  663. # define HAVE_DECL_ENVIRON 0
  664. # endif
  665. #endif
  666. #if !HAVE_DECL_ENVIRON
  667. extern char **environ;
  668. #endif
  669. #if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
  670. extern char *tzname[];
  671. #endif
  672. #if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
  673. extern long timezone;
  674. extern int daylight;
  675. #endif
  676. #if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
  677. extern long altzone;
  678. #endif
  679. /*
  680. ** The STD_INSPIRED functions are similar, but most also need
  681. ** declarations if time_tz is defined.
  682. */
  683. #ifndef STD_INSPIRED
  684. # define STD_INSPIRED 0
  685. #endif
  686. #if STD_INSPIRED
  687. # if TZ_TIME_T || !defined offtime
  688. struct tm *offtime(time_t const *, long);
  689. # endif
  690. # if TZ_TIME_T || !defined timelocal
  691. time_t timelocal(struct tm *);
  692. # endif
  693. # if TZ_TIME_T || !defined timeoff
  694. # define EXTERN_TIMEOFF
  695. # endif
  696. # if TZ_TIME_T || !defined time2posix
  697. time_t time2posix(time_t);
  698. # endif
  699. # if TZ_TIME_T || !defined posix2time
  700. time_t posix2time(time_t);
  701. # endif
  702. #endif
  703. /* Infer TM_ZONE on systems where this information is known, but suppress
  704. guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
  705. #if (200809 < _POSIX_VERSION \
  706. || defined __GLIBC__ \
  707. || defined __tm_zone /* musl */ \
  708. || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
  709. || (defined __APPLE__ && defined __MACH__))
  710. # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
  711. # define TM_GMTOFF tm_gmtoff
  712. # endif
  713. # if !defined TM_ZONE && !defined NO_TM_ZONE
  714. # define TM_ZONE tm_zone
  715. # endif
  716. #endif
  717. /*
  718. ** Define functions that are ABI compatible with NetBSD but have
  719. ** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
  720. ** and labors under the misconception that 'const timezone_t' is a
  721. ** pointer to a constant. This use of 'const' is ineffective, so it
  722. ** is not done here. What we call 'struct state' NetBSD calls
  723. ** 'struct __state', but this is a private name so it doesn't matter.
  724. */
  725. #if NETBSD_INSPIRED
  726. typedef struct state *timezone_t;
  727. struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
  728. struct tm *restrict);
  729. time_t mktime_z(timezone_t restrict, struct tm *restrict);
  730. timezone_t tzalloc(char const *);
  731. void tzfree(timezone_t);
  732. # if STD_INSPIRED
  733. # if TZ_TIME_T || !defined posix2time_z
  734. ATTRIBUTE_PURE time_t posix2time_z(timezone_t, time_t);
  735. # endif
  736. # if TZ_TIME_T || !defined time2posix_z
  737. ATTRIBUTE_PURE time_t time2posix_z(timezone_t, time_t);
  738. # endif
  739. # endif
  740. #endif
  741. /*
  742. ** Finally, some convenience items.
  743. */
  744. #define TYPE_BIT(type) (CHAR_BIT * (ptrdiff_t) sizeof(type))
  745. #define TYPE_SIGNED(type) (((type) -1) < 0)
  746. #define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
  747. /* Minimum and maximum of two values. Use lower case to avoid
  748. naming clashes with standard include files. */
  749. #define max(a, b) ((a) > (b) ? (a) : (b))
  750. #define min(a, b) ((a) < (b) ? (a) : (b))
  751. /* Max and min values of the integer type T, of which only the bottom
  752. B bits are used, and where the highest-order used bit is considered
  753. to be a sign bit if T is signed. */
  754. #define MAXVAL(t, b) \
  755. ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
  756. - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
  757. #define MINVAL(t, b) \
  758. ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
  759. /* The extreme time values, assuming no padding. */
  760. #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
  761. #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
  762. /* The extreme time values. These are macros, not constants, so that
  763. any portability problems occur only when compiling .c files that use
  764. the macros, which is safer for applications that need only zdump and zic.
  765. This implementation assumes no padding if time_t is signed and
  766. either the compiler lacks support for _Generic or time_t is not one
  767. of the standard signed integer types. */
  768. #if HAVE__GENERIC
  769. # define TIME_T_MIN \
  770. _Generic((time_t) 0, \
  771. signed char: SCHAR_MIN, short: SHRT_MIN, \
  772. int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
  773. default: TIME_T_MIN_NO_PADDING)
  774. # define TIME_T_MAX \
  775. (TYPE_SIGNED(time_t) \
  776. ? _Generic((time_t) 0, \
  777. signed char: SCHAR_MAX, short: SHRT_MAX, \
  778. int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
  779. default: TIME_T_MAX_NO_PADDING) \
  780. : (time_t) -1)
  781. enum { SIGNED_PADDING_CHECK_NEEDED
  782. = _Generic((time_t) 0,
  783. signed char: false, short: false,
  784. int: false, long: false, long long: false,
  785. default: true) };
  786. #else
  787. # define TIME_T_MIN TIME_T_MIN_NO_PADDING
  788. # define TIME_T_MAX TIME_T_MAX_NO_PADDING
  789. enum { SIGNED_PADDING_CHECK_NEEDED = true };
  790. #endif
  791. /* Try to check the padding assumptions. Although TIME_T_MAX and the
  792. following check can both have undefined behavior on oddball
  793. platforms due to shifts exceeding widths of signed integers, these
  794. platforms' compilers are likely to diagnose these issues in integer
  795. constant expressions, so it shouldn't hurt to check statically. */
  796. static_assert(! TYPE_SIGNED(time_t) || ! SIGNED_PADDING_CHECK_NEEDED
  797. || TIME_T_MAX >> (TYPE_BIT(time_t) - 2) == 1);
  798. /*
  799. ** 302 / 1000 is log10(2.0) rounded up.
  800. ** Subtract one for the sign bit if the type is signed;
  801. ** add one for integer division truncation;
  802. ** add one more for a minus sign if the type is signed.
  803. */
  804. #define INT_STRLEN_MAXIMUM(type) \
  805. ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
  806. 1 + TYPE_SIGNED(type))
  807. /*
  808. ** INITIALIZE(x)
  809. */
  810. #ifdef GCC_LINT
  811. # define INITIALIZE(x) ((x) = 0)
  812. #else
  813. # define INITIALIZE(x)
  814. #endif
  815. /* Whether memory access must strictly follow the C standard.
  816. If 0, it's OK to read uninitialized storage so long as the value is
  817. not relied upon. Defining it to 0 lets mktime access parts of
  818. struct tm that might be uninitialized, as a heuristic when the
  819. standard doesn't say what to return and when tm_gmtoff can help
  820. mktime likely infer a better value. */
  821. #ifndef UNINIT_TRAP
  822. # define UNINIT_TRAP 0
  823. #endif
  824. /* localtime.c sometimes needs access to timeoff if it is not already public.
  825. tz_private_timeoff should be used only by localtime.c. */
  826. #if (!defined EXTERN_TIMEOFF \
  827. && defined TM_GMTOFF && (200809 < _POSIX_VERSION || ! UNINIT_TRAP))
  828. # ifndef timeoff
  829. # define timeoff tz_private_timeoff
  830. # endif
  831. # define EXTERN_TIMEOFF
  832. #endif
  833. #ifdef EXTERN_TIMEOFF
  834. time_t timeoff(struct tm *, long);
  835. #endif
  836. #ifdef DEBUG
  837. # undef unreachable
  838. # define unreachable() abort()
  839. #elif !defined unreachable
  840. # ifdef __has_builtin
  841. # if __has_builtin(__builtin_unreachable)
  842. # define unreachable() __builtin_unreachable()
  843. # endif
  844. # elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
  845. # define unreachable() __builtin_unreachable()
  846. # endif
  847. # ifndef unreachable
  848. # define unreachable() ((void) 0)
  849. # endif
  850. #endif
  851. /*
  852. ** For the benefit of GNU folk...
  853. ** '_(MSGID)' uses the current locale's message library string for MSGID.
  854. ** The default is to use gettext if available, and use MSGID otherwise.
  855. */
  856. #if HAVE_GETTEXT
  857. #define _(msgid) gettext(msgid)
  858. #else /* !HAVE_GETTEXT */
  859. #define _(msgid) msgid
  860. #endif /* !HAVE_GETTEXT */
  861. #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
  862. # define TZ_DOMAIN "tz"
  863. #endif
  864. #if HAVE_INCOMPATIBLE_CTIME_R
  865. #undef asctime_r
  866. #undef ctime_r
  867. char *asctime_r(struct tm const *restrict, char *restrict);
  868. char *ctime_r(time_t const *, char *);
  869. #endif /* HAVE_INCOMPATIBLE_CTIME_R */
  870. /* Handy macros that are independent of tzfile implementation. */
  871. enum {
  872. SECSPERMIN = 60,
  873. MINSPERHOUR = 60,
  874. SECSPERHOUR = SECSPERMIN * MINSPERHOUR,
  875. HOURSPERDAY = 24,
  876. DAYSPERWEEK = 7,
  877. DAYSPERNYEAR = 365,
  878. DAYSPERLYEAR = DAYSPERNYEAR + 1,
  879. MONSPERYEAR = 12,
  880. YEARSPERREPEAT = 400 /* years before a Gregorian repeat */
  881. };
  882. #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
  883. #define DAYSPERREPEAT ((int_fast32_t) 400 * 365 + 100 - 4 + 1)
  884. #define SECSPERREPEAT ((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
  885. #define AVGSECSPERYEAR (SECSPERREPEAT / YEARSPERREPEAT)
  886. /* How many years to generate (in zic.c) or search through (in localtime.c).
  887. This is two years larger than the obvious 400, to avoid edge cases.
  888. E.g., suppose a rule applies from 2012 on with transitions
  889. in March and September, plus one-off transitions in November 2013,
  890. and suppose the rule cannot be expressed as a proleptic TZ string.
  891. If zic looked only at the last 400 years, it would set max_year=2413,
  892. with the intent that the 400 years 2014 through 2413 will be repeated.
  893. The last transition listed in the tzfile would be in 2413-09,
  894. less than 400 years after the last one-off transition in 2013-11.
  895. Two years is not overkill for localtime.c, as a one-year bump
  896. would mishandle 2023d's America/Ciudad_Juarez for November 2422. */
  897. enum { years_of_observations = YEARSPERREPEAT + 2 };
  898. enum {
  899. TM_SUNDAY,
  900. TM_MONDAY,
  901. TM_TUESDAY,
  902. TM_WEDNESDAY,
  903. TM_THURSDAY,
  904. TM_FRIDAY,
  905. TM_SATURDAY
  906. };
  907. enum {
  908. TM_JANUARY,
  909. TM_FEBRUARY,
  910. TM_MARCH,
  911. TM_APRIL,
  912. TM_MAY,
  913. TM_JUNE,
  914. TM_JULY,
  915. TM_AUGUST,
  916. TM_SEPTEMBER,
  917. TM_OCTOBER,
  918. TM_NOVEMBER,
  919. TM_DECEMBER
  920. };
  921. enum {
  922. TM_YEAR_BASE = 1900,
  923. TM_WDAY_BASE = TM_MONDAY,
  924. EPOCH_YEAR = 1970,
  925. EPOCH_WDAY = TM_THURSDAY
  926. };
  927. #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
  928. /*
  929. ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
  930. ** isleap(y) == isleap(y % 400)
  931. ** and so
  932. ** isleap(a + b) == isleap((a + b) % 400)
  933. ** or
  934. ** isleap(a + b) == isleap(a % 400 + b % 400)
  935. ** This is true even if % means modulo rather than Fortran remainder
  936. ** (which is allowed by C89 but not by C99 or later).
  937. ** We use this to avoid addition overflow problems.
  938. */
  939. #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
  940. #endif /* !defined PRIVATE_H */