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