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