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