private.h 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. SUPPORT_C89 means the
  17. tzcode library should support C89 callers in addition to the usual
  18. support for C99-and-later callers. These macros are obsolescent,
  19. and the plan is to remove them along with any code needed only when
  20. they are nonzero. */
  21. #ifndef PORT_TO_C89
  22. # define PORT_TO_C89 0
  23. #endif
  24. #ifndef SUPPORT_C89
  25. # define SUPPORT_C89 0
  26. #endif
  27. #ifndef __STDC_VERSION__
  28. # define __STDC_VERSION__ 0
  29. #endif
  30. /* Define true, false and bool if they don't work out of the box. */
  31. #if PORT_TO_C89 && __STDC_VERSION__ < 199901
  32. # define true 1
  33. # define false 0
  34. # define bool int
  35. #elif __STDC_VERSION__ < 202311
  36. # include <stdbool.h>
  37. #endif
  38. #if __STDC_VERSION__ < 202311
  39. # define static_assert(cond) extern int static_assert_check[(cond) ? 1 : -1]
  40. #endif
  41. /*
  42. ** zdump has been made independent of the rest of the time
  43. ** conversion package to increase confidence in the verification it provides.
  44. ** You can use zdump to help in verifying other implementations.
  45. ** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
  46. */
  47. #ifndef USE_LTZ
  48. # define USE_LTZ 1
  49. #endif
  50. /* This string was in the Factory zone through version 2016f. */
  51. #define GRANDPARENTED "Local time zone must be set--see zic manual page"
  52. /*
  53. ** Defaults for preprocessor symbols.
  54. ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
  55. */
  56. #ifndef HAVE_DECL_ASCTIME_R
  57. # define HAVE_DECL_ASCTIME_R 1
  58. #endif
  59. #if !defined HAVE__GENERIC && defined __has_extension
  60. # if __has_extension(c_generic_selections)
  61. # define HAVE__GENERIC 1
  62. # else
  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. #ifndef HAVE_STRFTIME_L
  202. # if _POSIX_VERSION < 200809
  203. # define HAVE_STRFTIME_L 0
  204. # else
  205. # define HAVE_STRFTIME_L 1
  206. # endif
  207. #endif
  208. #ifndef USG_COMPAT
  209. # ifndef _XOPEN_VERSION
  210. # define USG_COMPAT 0
  211. # else
  212. # define USG_COMPAT 1
  213. # endif
  214. #endif
  215. #ifndef HAVE_TZNAME
  216. # if _POSIX_VERSION < 198808 && !USG_COMPAT
  217. # define HAVE_TZNAME 0
  218. # else
  219. # define HAVE_TZNAME 1
  220. # endif
  221. #endif
  222. #ifndef ALTZONE
  223. # if defined __sun || defined _M_XENIX
  224. # define ALTZONE 1
  225. # else
  226. # define ALTZONE 0
  227. # endif
  228. #endif
  229. #ifndef R_OK
  230. # define R_OK 4
  231. #endif /* !defined R_OK */
  232. #if PORT_TO_C89
  233. /*
  234. ** Define HAVE_STDINT_H's default value here, rather than at the
  235. ** start, since __GLIBC__ and INTMAX_MAX's values depend on
  236. ** previously included files. glibc 2.1 and Solaris 10 and later have
  237. ** stdint.h, even with pre-C99 compilers.
  238. */
  239. #if !defined HAVE_STDINT_H && defined __has_include
  240. # define HAVE_STDINT_H true /* C23 __has_include implies C99 stdint.h. */
  241. #endif
  242. #ifndef HAVE_STDINT_H
  243. # define HAVE_STDINT_H \
  244. (199901 <= __STDC_VERSION__ \
  245. || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
  246. || __CYGWIN__ || INTMAX_MAX)
  247. #endif /* !defined HAVE_STDINT_H */
  248. #if HAVE_STDINT_H
  249. # include <stdint.h>
  250. #endif /* !HAVE_STDINT_H */
  251. #ifndef HAVE_INTTYPES_H
  252. # define HAVE_INTTYPES_H HAVE_STDINT_H
  253. #endif
  254. #if HAVE_INTTYPES_H
  255. # include <inttypes.h>
  256. #endif
  257. /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
  258. #if defined __LONG_LONG_MAX__ && !defined __STRICT_ANSI__
  259. # ifndef LLONG_MAX
  260. # define LLONG_MAX __LONG_LONG_MAX__
  261. # endif
  262. # ifndef LLONG_MIN
  263. # define LLONG_MIN (-1 - LLONG_MAX)
  264. # endif
  265. # ifndef ULLONG_MAX
  266. # define ULLONG_MAX (LLONG_MAX * 2ull + 1)
  267. # endif
  268. #endif
  269. #ifndef INT_FAST64_MAX
  270. # if 1 <= LONG_MAX >> 31 >> 31
  271. typedef long int_fast64_t;
  272. # define INT_FAST64_MIN LONG_MIN
  273. # define INT_FAST64_MAX LONG_MAX
  274. # else
  275. /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
  276. typedef long long int_fast64_t;
  277. # define INT_FAST64_MIN LLONG_MIN
  278. # define INT_FAST64_MAX LLONG_MAX
  279. # endif
  280. #endif
  281. #ifndef PRIdFAST64
  282. # if INT_FAST64_MAX == LONG_MAX
  283. # define PRIdFAST64 "ld"
  284. # else
  285. # define PRIdFAST64 "lld"
  286. # endif
  287. #endif
  288. #ifndef SCNdFAST64
  289. # define SCNdFAST64 PRIdFAST64
  290. #endif
  291. #ifndef INT_FAST32_MAX
  292. # if INT_MAX >> 31 == 0
  293. typedef long int_fast32_t;
  294. # define INT_FAST32_MAX LONG_MAX
  295. # define INT_FAST32_MIN LONG_MIN
  296. # else
  297. typedef int int_fast32_t;
  298. # define INT_FAST32_MAX INT_MAX
  299. # define INT_FAST32_MIN INT_MIN
  300. # endif
  301. #endif
  302. #ifndef INTMAX_MAX
  303. # ifdef LLONG_MAX
  304. typedef long long intmax_t;
  305. # ifndef HAVE_STRTOLL
  306. # define HAVE_STRTOLL true
  307. # endif
  308. # if HAVE_STRTOLL
  309. # define strtoimax strtoll
  310. # endif
  311. # define INTMAX_MAX LLONG_MAX
  312. # define INTMAX_MIN LLONG_MIN
  313. # else
  314. typedef long intmax_t;
  315. # define INTMAX_MAX LONG_MAX
  316. # define INTMAX_MIN LONG_MIN
  317. # endif
  318. # ifndef strtoimax
  319. # define strtoimax strtol
  320. # endif
  321. #endif
  322. #ifndef PRIdMAX
  323. # if INTMAX_MAX == LLONG_MAX
  324. # define PRIdMAX "lld"
  325. # else
  326. # define PRIdMAX "ld"
  327. # endif
  328. #endif
  329. #ifndef PTRDIFF_MAX
  330. # define PTRDIFF_MAX MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t))
  331. #endif
  332. #ifndef UINT_FAST32_MAX
  333. typedef unsigned long uint_fast32_t;
  334. #endif
  335. #ifndef UINT_FAST64_MAX
  336. # if 3 <= ULONG_MAX >> 31 >> 31
  337. typedef unsigned long uint_fast64_t;
  338. # define UINT_FAST64_MAX ULONG_MAX
  339. # else
  340. /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
  341. typedef unsigned long long uint_fast64_t;
  342. # define UINT_FAST64_MAX ULLONG_MAX
  343. # endif
  344. #endif
  345. #ifndef UINTMAX_MAX
  346. # ifdef ULLONG_MAX
  347. typedef unsigned long long uintmax_t;
  348. # define UINTMAX_MAX ULLONG_MAX
  349. # else
  350. typedef unsigned long uintmax_t;
  351. # define UINTMAX_MAX ULONG_MAX
  352. # endif
  353. #endif
  354. #ifndef PRIuMAX
  355. # ifdef ULLONG_MAX
  356. # define PRIuMAX "llu"
  357. # else
  358. # define PRIuMAX "lu"
  359. # endif
  360. #endif
  361. #ifndef SIZE_MAX
  362. # define SIZE_MAX ((size_t) -1)
  363. #endif
  364. #endif /* PORT_TO_C89 */
  365. /* The maximum size of any created object, as a signed integer.
  366. Although the C standard does not outright prohibit larger objects,
  367. behavior is undefined if the result of pointer subtraction does not
  368. fit into ptrdiff_t, and the code assumes in several places that
  369. pointer subtraction works. As a practical matter it's OK to not
  370. support objects larger than this. */
  371. #define INDEX_MAX ((ptrdiff_t) min(PTRDIFF_MAX, SIZE_MAX))
  372. /* Support ckd_add, ckd_sub, ckd_mul on C23 or recent-enough GCC-like
  373. hosts, unless compiled with -DHAVE_STDCKDINT_H=0 or with pre-C23 EDG. */
  374. #if !defined HAVE_STDCKDINT_H && defined __has_include
  375. # if __has_include(<stdckdint.h>)
  376. # define HAVE_STDCKDINT_H true
  377. # endif
  378. #endif
  379. #ifdef HAVE_STDCKDINT_H
  380. # if HAVE_STDCKDINT_H
  381. # include <stdckdint.h>
  382. # endif
  383. #elif defined __EDG__
  384. /* Do nothing, to work around EDG bug <https://bugs.gnu.org/53256>. */
  385. #elif defined __has_builtin
  386. # if __has_builtin(__builtin_add_overflow)
  387. # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
  388. # endif
  389. # if __has_builtin(__builtin_sub_overflow)
  390. # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
  391. # endif
  392. # if __has_builtin(__builtin_mul_overflow)
  393. # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
  394. # endif
  395. #elif 7 <= __GNUC__
  396. # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
  397. # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
  398. # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
  399. #endif
  400. #if 3 <= __GNUC__
  401. # define ATTRIBUTE_MALLOC __attribute__((malloc))
  402. # define ATTRIBUTE_FORMAT(spec) __attribute__((format spec))
  403. #else
  404. # define ATTRIBUTE_MALLOC /* empty */
  405. # define ATTRIBUTE_FORMAT(spec) /* empty */
  406. #endif
  407. #if (defined __has_c_attribute \
  408. && (202311 <= __STDC_VERSION__ || !defined __STRICT_ANSI__))
  409. # define HAVE___HAS_C_ATTRIBUTE true
  410. #else
  411. # define HAVE___HAS_C_ATTRIBUTE false
  412. #endif
  413. #if HAVE___HAS_C_ATTRIBUTE
  414. # if __has_c_attribute(deprecated)
  415. # define ATTRIBUTE_DEPRECATED [[deprecated]]
  416. # endif
  417. #endif
  418. #ifndef ATTRIBUTE_DEPRECATED
  419. # if 3 < __GNUC__ + (2 <= __GNUC_MINOR__)
  420. # define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
  421. # else
  422. # define ATTRIBUTE_DEPRECATED /* empty */
  423. # endif
  424. #endif
  425. #if HAVE___HAS_C_ATTRIBUTE
  426. # if __has_c_attribute(fallthrough)
  427. # define ATTRIBUTE_FALLTHROUGH [[fallthrough]]
  428. # endif
  429. #endif
  430. #ifndef ATTRIBUTE_FALLTHROUGH
  431. # if 7 <= __GNUC__
  432. # define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
  433. # else
  434. # define ATTRIBUTE_FALLTHROUGH ((void) 0)
  435. # endif
  436. #endif
  437. #if HAVE___HAS_C_ATTRIBUTE
  438. # if __has_c_attribute(maybe_unused)
  439. # define ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]]
  440. # endif
  441. #endif
  442. #ifndef ATTRIBUTE_MAYBE_UNUSED
  443. # if 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
  444. # define ATTRIBUTE_MAYBE_UNUSED __attribute__((unused))
  445. # else
  446. # define ATTRIBUTE_MAYBE_UNUSED /* empty */
  447. # endif
  448. #endif
  449. #if HAVE___HAS_C_ATTRIBUTE
  450. # if __has_c_attribute(noreturn)
  451. # define ATTRIBUTE_NORETURN [[noreturn]]
  452. # endif
  453. #endif
  454. #ifndef ATTRIBUTE_NORETURN
  455. # if 201112 <= __STDC_VERSION__
  456. # define ATTRIBUTE_NORETURN _Noreturn
  457. # elif 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
  458. # define ATTRIBUTE_NORETURN __attribute__((noreturn))
  459. # else
  460. # define ATTRIBUTE_NORETURN /* empty */
  461. # endif
  462. #endif
  463. #if HAVE___HAS_C_ATTRIBUTE
  464. # if __has_c_attribute(reproducible)
  465. # define ATTRIBUTE_REPRODUCIBLE [[reproducible]]
  466. # endif
  467. #endif
  468. #ifndef ATTRIBUTE_REPRODUCIBLE
  469. # if 3 <= __GNUC__
  470. # define ATTRIBUTE_REPRODUCIBLE __attribute__((pure))
  471. # else
  472. # define ATTRIBUTE_REPRODUCIBLE /* empty */
  473. # endif
  474. #endif
  475. #if HAVE___HAS_C_ATTRIBUTE
  476. # if __has_c_attribute(unsequenced)
  477. # define ATTRIBUTE_UNSEQUENCED [[unsequenced]]
  478. # endif
  479. #endif
  480. #ifndef ATTRIBUTE_UNSEQUENCED
  481. # if 3 <= __GNUC__
  482. # define ATTRIBUTE_UNSEQUENCED __attribute__((const))
  483. # else
  484. # define ATTRIBUTE_UNSEQUENCED /* empty */
  485. # endif
  486. #endif
  487. #if (__STDC_VERSION__ < 199901 && !defined restrict \
  488. && (PORT_TO_C89 || defined _MSC_VER))
  489. # define restrict /* empty */
  490. #endif
  491. /*
  492. ** Workarounds for compilers/systems.
  493. */
  494. #ifndef EPOCH_LOCAL
  495. # define EPOCH_LOCAL 0
  496. #endif
  497. #ifndef EPOCH_OFFSET
  498. # define EPOCH_OFFSET 0
  499. #endif
  500. #ifndef RESERVE_STD_EXT_IDS
  501. # define RESERVE_STD_EXT_IDS 0
  502. #endif
  503. /* If standard C identifiers with external linkage (e.g., localtime)
  504. are reserved and are not already being renamed anyway, rename them
  505. as if compiling with '-Dtime_tz=time_t'. */
  506. #if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
  507. # define time_tz time_t
  508. #endif
  509. /*
  510. ** Compile with -Dtime_tz=T to build the tz package with a private
  511. ** time_t type equivalent to T rather than the system-supplied time_t.
  512. ** This debugging feature can test unusual design decisions
  513. ** (e.g., time_t wider than 'long', or unsigned time_t) even on
  514. ** typical platforms.
  515. */
  516. #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
  517. # define TZ_TIME_T 1
  518. #else
  519. # define TZ_TIME_T 0
  520. #endif
  521. #if defined LOCALTIME_IMPLEMENTATION && TZ_TIME_T
  522. static time_t sys_time(time_t *x) { return time(x); }
  523. #endif
  524. #if TZ_TIME_T
  525. typedef time_tz tz_time_t;
  526. # undef asctime
  527. # define asctime tz_asctime
  528. # undef asctime_r
  529. # define asctime_r tz_asctime_r
  530. # undef ctime
  531. # define ctime tz_ctime
  532. # undef ctime_r
  533. # define ctime_r tz_ctime_r
  534. # undef difftime
  535. # define difftime tz_difftime
  536. # undef gmtime
  537. # define gmtime tz_gmtime
  538. # undef gmtime_r
  539. # define gmtime_r tz_gmtime_r
  540. # undef localtime
  541. # define localtime tz_localtime
  542. # undef localtime_r
  543. # define localtime_r tz_localtime_r
  544. # undef localtime_rz
  545. # define localtime_rz tz_localtime_rz
  546. # undef mktime
  547. # define mktime tz_mktime
  548. # undef mktime_z
  549. # define mktime_z tz_mktime_z
  550. # undef offtime
  551. # define offtime tz_offtime
  552. # undef posix2time
  553. # define posix2time tz_posix2time
  554. # undef posix2time_z
  555. # define posix2time_z tz_posix2time_z
  556. # undef strftime
  557. # define strftime tz_strftime
  558. # undef time
  559. # define time tz_time
  560. # undef time2posix
  561. # define time2posix tz_time2posix
  562. # undef time2posix_z
  563. # define time2posix_z tz_time2posix_z
  564. # undef time_t
  565. # define time_t tz_time_t
  566. # undef timegm
  567. # define timegm tz_timegm
  568. # undef timelocal
  569. # define timelocal tz_timelocal
  570. # undef timeoff
  571. # define timeoff tz_timeoff
  572. # undef tzalloc
  573. # define tzalloc tz_tzalloc
  574. # undef tzfree
  575. # define tzfree tz_tzfree
  576. # undef tzset
  577. # define tzset tz_tzset
  578. # if HAVE_STRFTIME_L
  579. # undef strftime_l
  580. # define strftime_l tz_strftime_l
  581. # endif
  582. # if HAVE_TZNAME
  583. # undef tzname
  584. # define tzname tz_tzname
  585. # endif
  586. # if USG_COMPAT
  587. # undef daylight
  588. # define daylight tz_daylight
  589. # undef timezone
  590. # define timezone tz_timezone
  591. # endif
  592. # if ALTZONE
  593. # undef altzone
  594. # define altzone tz_altzone
  595. # endif
  596. # if __STDC_VERSION__ < 202311
  597. # define DEPRECATED_IN_C23 /* empty */
  598. # else
  599. # define DEPRECATED_IN_C23 ATTRIBUTE_DEPRECATED
  600. # endif
  601. DEPRECATED_IN_C23 char *asctime(struct tm const *);
  602. char *asctime_r(struct tm const *restrict, char *restrict);
  603. DEPRECATED_IN_C23 char *ctime(time_t const *);
  604. char *ctime_r(time_t const *, char *);
  605. ATTRIBUTE_UNSEQUENCED double difftime(time_t, time_t);
  606. size_t strftime(char *restrict, size_t, char const *restrict,
  607. struct tm const *restrict);
  608. # if HAVE_STRFTIME_L
  609. size_t strftime_l(char *restrict, size_t, char const *restrict,
  610. struct tm const *restrict, locale_t);
  611. # endif
  612. struct tm *gmtime(time_t const *);
  613. struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
  614. struct tm *localtime(time_t const *);
  615. struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
  616. time_t mktime(struct tm *);
  617. time_t time(time_t *);
  618. time_t timegm(struct tm *);
  619. void tzset(void);
  620. #endif
  621. #ifndef HAVE_DECL_TIMEGM
  622. # if (202311 <= __STDC_VERSION__ \
  623. || defined __GLIBC__ || defined __tm_zone /* musl */ \
  624. || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
  625. || (defined __APPLE__ && defined __MACH__))
  626. # define HAVE_DECL_TIMEGM true
  627. # else
  628. # define HAVE_DECL_TIMEGM false
  629. # endif
  630. #endif
  631. #if !HAVE_DECL_TIMEGM && !defined timegm
  632. time_t timegm(struct tm *);
  633. #endif
  634. #if !HAVE_DECL_ASCTIME_R && !defined asctime_r
  635. extern char *asctime_r(struct tm const *restrict, char *restrict);
  636. #endif
  637. #ifndef HAVE_DECL_ENVIRON
  638. # if defined environ || defined __USE_GNU
  639. # define HAVE_DECL_ENVIRON 1
  640. # else
  641. # define HAVE_DECL_ENVIRON 0
  642. # endif
  643. #endif
  644. #if !HAVE_DECL_ENVIRON
  645. extern char **environ;
  646. #endif
  647. #if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
  648. extern char *tzname[];
  649. #endif
  650. #if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
  651. extern long timezone;
  652. extern int daylight;
  653. #endif
  654. #if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
  655. extern long altzone;
  656. #endif
  657. /*
  658. ** The STD_INSPIRED functions are similar, but most also need
  659. ** declarations if time_tz is defined.
  660. */
  661. #ifndef STD_INSPIRED
  662. # define STD_INSPIRED 0
  663. #endif
  664. #if STD_INSPIRED
  665. # if TZ_TIME_T || !defined offtime
  666. struct tm *offtime(time_t const *, long);
  667. # endif
  668. # if TZ_TIME_T || !defined timelocal
  669. time_t timelocal(struct tm *);
  670. # endif
  671. # if TZ_TIME_T || !defined timeoff
  672. time_t timeoff(struct tm *, long);
  673. # endif
  674. # if TZ_TIME_T || !defined time2posix
  675. time_t time2posix(time_t);
  676. # endif
  677. # if TZ_TIME_T || !defined posix2time
  678. time_t posix2time(time_t);
  679. # endif
  680. #endif
  681. /* Infer TM_ZONE on systems where this information is known, but suppress
  682. guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
  683. #if (defined __GLIBC__ \
  684. || defined __tm_zone /* musl */ \
  685. || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
  686. || (defined __APPLE__ && defined __MACH__))
  687. # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
  688. # define TM_GMTOFF tm_gmtoff
  689. # endif
  690. # if !defined TM_ZONE && !defined NO_TM_ZONE
  691. # define TM_ZONE tm_zone
  692. # endif
  693. #endif
  694. /*
  695. ** Define functions that are ABI compatible with NetBSD but have
  696. ** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
  697. ** and labors under the misconception that 'const timezone_t' is a
  698. ** pointer to a constant. This use of 'const' is ineffective, so it
  699. ** is not done here. What we call 'struct state' NetBSD calls
  700. ** 'struct __state', but this is a private name so it doesn't matter.
  701. */
  702. #if NETBSD_INSPIRED
  703. typedef struct state *timezone_t;
  704. struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
  705. struct tm *restrict);
  706. time_t mktime_z(timezone_t restrict, struct tm *restrict);
  707. timezone_t tzalloc(char const *);
  708. void tzfree(timezone_t);
  709. # if STD_INSPIRED
  710. # if TZ_TIME_T || !defined posix2time_z
  711. ATTRIBUTE_REPRODUCIBLE time_t posix2time_z(timezone_t, time_t);
  712. # endif
  713. # if TZ_TIME_T || !defined time2posix_z
  714. ATTRIBUTE_REPRODUCIBLE time_t time2posix_z(timezone_t, time_t);
  715. # endif
  716. # endif
  717. #endif
  718. /*
  719. ** Finally, some convenience items.
  720. */
  721. #define TYPE_BIT(type) (CHAR_BIT * (ptrdiff_t) sizeof(type))
  722. #define TYPE_SIGNED(type) (((type) -1) < 0)
  723. #define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
  724. /* Minimum and maximum of two values. Use lower case to avoid
  725. naming clashes with standard include files. */
  726. #define max(a, b) ((a) > (b) ? (a) : (b))
  727. #define min(a, b) ((a) < (b) ? (a) : (b))
  728. /* Max and min values of the integer type T, of which only the bottom
  729. B bits are used, and where the highest-order used bit is considered
  730. to be a sign bit if T is signed. */
  731. #define MAXVAL(t, b) \
  732. ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
  733. - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
  734. #define MINVAL(t, b) \
  735. ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
  736. /* The extreme time values, assuming no padding. */
  737. #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
  738. #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
  739. /* The extreme time values. These are macros, not constants, so that
  740. any portability problems occur only when compiling .c files that use
  741. the macros, which is safer for applications that need only zdump and zic.
  742. This implementation assumes no padding if time_t is signed and
  743. either the compiler lacks support for _Generic or time_t is not one
  744. of the standard signed integer types. */
  745. #if HAVE__GENERIC
  746. # define TIME_T_MIN \
  747. _Generic((time_t) 0, \
  748. signed char: SCHAR_MIN, short: SHRT_MIN, \
  749. int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
  750. default: TIME_T_MIN_NO_PADDING)
  751. # define TIME_T_MAX \
  752. (TYPE_SIGNED(time_t) \
  753. ? _Generic((time_t) 0, \
  754. signed char: SCHAR_MAX, short: SHRT_MAX, \
  755. int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
  756. default: TIME_T_MAX_NO_PADDING) \
  757. : (time_t) -1)
  758. enum { SIGNED_PADDING_CHECK_NEEDED
  759. = _Generic((time_t) 0,
  760. signed char: false, short: false,
  761. int: false, long: false, long long: false,
  762. default: true) };
  763. #else
  764. # define TIME_T_MIN TIME_T_MIN_NO_PADDING
  765. # define TIME_T_MAX TIME_T_MAX_NO_PADDING
  766. enum { SIGNED_PADDING_CHECK_NEEDED = true };
  767. #endif
  768. /* Try to check the padding assumptions. Although TIME_T_MAX and the
  769. following check can both have undefined behavior on oddball
  770. platforms due to shifts exceeding widths of signed integers, these
  771. platforms' compilers are likely to diagnose these issues in integer
  772. constant expressions, so it shouldn't hurt to check statically. */
  773. static_assert(! TYPE_SIGNED(time_t) || ! SIGNED_PADDING_CHECK_NEEDED
  774. || TIME_T_MAX >> (TYPE_BIT(time_t) - 2) == 1);
  775. /*
  776. ** 302 / 1000 is log10(2.0) rounded up.
  777. ** Subtract one for the sign bit if the type is signed;
  778. ** add one for integer division truncation;
  779. ** add one more for a minus sign if the type is signed.
  780. */
  781. #define INT_STRLEN_MAXIMUM(type) \
  782. ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
  783. 1 + TYPE_SIGNED(type))
  784. /*
  785. ** INITIALIZE(x)
  786. */
  787. #ifdef GCC_LINT
  788. # define INITIALIZE(x) ((x) = 0)
  789. #else
  790. # define INITIALIZE(x)
  791. #endif
  792. /* Whether memory access must strictly follow the C standard.
  793. If 0, it's OK to read uninitialized storage so long as the value is
  794. not relied upon. Defining it to 0 lets mktime access parts of
  795. struct tm that might be uninitialized, as a heuristic when the
  796. standard doesn't say what to return and when tm_gmtoff can help
  797. mktime likely infer a better value. */
  798. #ifndef UNINIT_TRAP
  799. # define UNINIT_TRAP 0
  800. #endif
  801. #ifdef DEBUG
  802. # undef unreachable
  803. # define unreachable() abort()
  804. #elif !defined unreachable
  805. # ifdef __has_builtin
  806. # if __has_builtin(__builtin_unreachable)
  807. # define unreachable() __builtin_unreachable()
  808. # endif
  809. # elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
  810. # define unreachable() __builtin_unreachable()
  811. # endif
  812. # ifndef unreachable
  813. # define unreachable() ((void) 0)
  814. # endif
  815. #endif
  816. /*
  817. ** For the benefit of GNU folk...
  818. ** '_(MSGID)' uses the current locale's message library string for MSGID.
  819. ** The default is to use gettext if available, and use MSGID otherwise.
  820. */
  821. #if HAVE_GETTEXT
  822. #define _(msgid) gettext(msgid)
  823. #else /* !HAVE_GETTEXT */
  824. #define _(msgid) msgid
  825. #endif /* !HAVE_GETTEXT */
  826. #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
  827. # define TZ_DOMAIN "tz"
  828. #endif
  829. #if HAVE_INCOMPATIBLE_CTIME_R
  830. #undef asctime_r
  831. #undef ctime_r
  832. char *asctime_r(struct tm const *restrict, char *restrict);
  833. char *ctime_r(time_t const *, char *);
  834. #endif /* HAVE_INCOMPATIBLE_CTIME_R */
  835. /* Handy macros that are independent of tzfile implementation. */
  836. enum {
  837. SECSPERMIN = 60,
  838. MINSPERHOUR = 60,
  839. SECSPERHOUR = SECSPERMIN * MINSPERHOUR,
  840. HOURSPERDAY = 24,
  841. DAYSPERWEEK = 7,
  842. DAYSPERNYEAR = 365,
  843. DAYSPERLYEAR = DAYSPERNYEAR + 1,
  844. MONSPERYEAR = 12,
  845. YEARSPERREPEAT = 400 /* years before a Gregorian repeat */
  846. };
  847. #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
  848. #define DAYSPERREPEAT ((int_fast32_t) 400 * 365 + 100 - 4 + 1)
  849. #define SECSPERREPEAT ((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
  850. #define AVGSECSPERYEAR (SECSPERREPEAT / YEARSPERREPEAT)
  851. enum {
  852. TM_SUNDAY,
  853. TM_MONDAY,
  854. TM_TUESDAY,
  855. TM_WEDNESDAY,
  856. TM_THURSDAY,
  857. TM_FRIDAY,
  858. TM_SATURDAY
  859. };
  860. enum {
  861. TM_JANUARY,
  862. TM_FEBRUARY,
  863. TM_MARCH,
  864. TM_APRIL,
  865. TM_MAY,
  866. TM_JUNE,
  867. TM_JULY,
  868. TM_AUGUST,
  869. TM_SEPTEMBER,
  870. TM_OCTOBER,
  871. TM_NOVEMBER,
  872. TM_DECEMBER
  873. };
  874. enum {
  875. TM_YEAR_BASE = 1900,
  876. TM_WDAY_BASE = TM_MONDAY,
  877. EPOCH_YEAR = 1970,
  878. EPOCH_WDAY = TM_THURSDAY
  879. };
  880. #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
  881. /*
  882. ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
  883. ** isleap(y) == isleap(y % 400)
  884. ** and so
  885. ** isleap(a + b) == isleap((a + b) % 400)
  886. ** or
  887. ** isleap(a + b) == isleap(a % 400 + b % 400)
  888. ** This is true even if % means modulo rather than Fortran remainder
  889. ** (which is allowed by C89 but not by C99 or later).
  890. ** We use this to avoid addition overflow problems.
  891. */
  892. #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
  893. #endif /* !defined PRIVATE_H */