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