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--see zic manual page"
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
sys_time(time_t *x)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  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 timelocal
756 time_t timelocal(struct tm *);
757 # endif
758 # if TZ_TIME_T || !defined timeoff
759 #  define EXTERN_TIMEOFF
760 # endif
761 # if TZ_TIME_T || !defined time2posix
762 time_t time2posix(time_t);
763 # endif
764 # if TZ_TIME_T || !defined posix2time
765 time_t posix2time(time_t);
766 # endif
767 #endif
768 
769 /* Infer TM_ZONE on systems where this information is known, but suppress
770    guessing if NO_TM_ZONE is defined.  Similarly for TM_GMTOFF.  */
771 #if (200809 < _POSIX_VERSION \
772      || defined __GLIBC__ \
773      || defined __tm_zone /* musl */ \
774      || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
775      || (defined __APPLE__ && defined __MACH__))
776 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
777 #  define TM_GMTOFF tm_gmtoff
778 # endif
779 # if !defined TM_ZONE && !defined NO_TM_ZONE
780 #  define TM_ZONE tm_zone
781 # endif
782 #endif
783 
784 /*
785 ** Define functions that are ABI compatible with NetBSD but have
786 ** better prototypes.  NetBSD 6.1.4 defines a pointer type timezone_t
787 ** and labors under the misconception that 'const timezone_t' is a
788 ** pointer to a constant.  This use of 'const' is ineffective, so it
789 ** is not done here.  What we call 'struct state' NetBSD calls
790 ** 'struct __state', but this is a private name so it doesn't matter.
791 */
792 #if NETBSD_INSPIRED
793 typedef struct state *timezone_t;
794 struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
795 			struct tm *restrict);
796 time_t mktime_z(timezone_t restrict, struct tm *restrict);
797 timezone_t tzalloc(char const *);
798 void tzfree(timezone_t);
799 # if STD_INSPIRED
800 #  if TZ_TIME_T || !defined posix2time_z
801 ATTRIBUTE_REPRODUCIBLE time_t posix2time_z(timezone_t, time_t);
802 #  endif
803 #  if TZ_TIME_T || !defined time2posix_z
804 ATTRIBUTE_REPRODUCIBLE time_t time2posix_z(timezone_t, time_t);
805 #  endif
806 # endif
807 #endif
808 
809 /*
810 ** Finally, some convenience items.
811 */
812 
813 #define TYPE_BIT(type) (CHAR_BIT * (ptrdiff_t) sizeof(type))
814 #define TYPE_SIGNED(type) (((type) -1) < 0)
815 #define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
816 
817 /* Minimum and maximum of two values.  Use lower case to avoid
818    naming clashes with standard include files.  */
819 #define max(a, b) ((a) > (b) ? (a) : (b))
820 #define min(a, b) ((a) < (b) ? (a) : (b))
821 
822 /* Max and min values of the integer type T, of which only the bottom
823    B bits are used, and where the highest-order used bit is considered
824    to be a sign bit if T is signed.  */
825 #define MAXVAL(t, b)						\
826   ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))			\
827 	- 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
828 #define MINVAL(t, b)						\
829   ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
830 
831 /* The extreme time values, assuming no padding.  */
832 #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
833 #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
834 
835 /* The extreme time values.  These are macros, not constants, so that
836    any portability problems occur only when compiling .c files that use
837    the macros, which is safer for applications that need only zdump and zic.
838    This implementation assumes no padding if time_t is signed and
839    either the compiler lacks support for _Generic or time_t is not one
840    of the standard signed integer types.  */
841 #if HAVE__GENERIC
842 # define TIME_T_MIN \
843     _Generic((time_t) 0, \
844 	     signed char: SCHAR_MIN, short: SHRT_MIN, \
845 	     int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
846 	     default: TIME_T_MIN_NO_PADDING)
847 # define TIME_T_MAX \
848     (TYPE_SIGNED(time_t) \
849      ? _Generic((time_t) 0, \
850 		signed char: SCHAR_MAX, short: SHRT_MAX, \
851 		int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
852 		default: TIME_T_MAX_NO_PADDING)			    \
853      : (time_t) -1)
854 enum { SIGNED_PADDING_CHECK_NEEDED
855          = _Generic((time_t) 0,
856 		    signed char: false, short: false,
857 		    int: false, long: false, long long: false,
858 		    default: true) };
859 #else
860 # define TIME_T_MIN TIME_T_MIN_NO_PADDING
861 # define TIME_T_MAX TIME_T_MAX_NO_PADDING
862 enum { SIGNED_PADDING_CHECK_NEEDED = true };
863 #endif
864 /* Try to check the padding assumptions.  Although TIME_T_MAX and the
865    following check can both have undefined behavior on oddball
866    platforms due to shifts exceeding widths of signed integers, these
867    platforms' compilers are likely to diagnose these issues in integer
868    constant expressions, so it shouldn't hurt to check statically.  */
869 static_assert(! TYPE_SIGNED(time_t) || ! SIGNED_PADDING_CHECK_NEEDED
870 	      || TIME_T_MAX >> (TYPE_BIT(time_t) - 2) == 1);
871 
872 /*
873 ** 302 / 1000 is log10(2.0) rounded up.
874 ** Subtract one for the sign bit if the type is signed;
875 ** add one for integer division truncation;
876 ** add one more for a minus sign if the type is signed.
877 */
878 #define INT_STRLEN_MAXIMUM(type) \
879 	((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
880 	1 + TYPE_SIGNED(type))
881 
882 /*
883 ** INITIALIZE(x)
884 */
885 
886 #ifdef GCC_LINT
887 # define INITIALIZE(x)	((x) = 0)
888 #else
889 # define INITIALIZE(x)
890 #endif
891 
892 /* Whether memory access must strictly follow the C standard.
893    If 0, it's OK to read uninitialized storage so long as the value is
894    not relied upon.  Defining it to 0 lets mktime access parts of
895    struct tm that might be uninitialized, as a heuristic when the
896    standard doesn't say what to return and when tm_gmtoff can help
897    mktime likely infer a better value.  */
898 #ifndef UNINIT_TRAP
899 # define UNINIT_TRAP 0
900 #endif
901 
902 /* localtime.c sometimes needs access to timeoff if it is not already public.
903    tz_private_timeoff should be used only by localtime.c.  */
904 #if (!defined EXTERN_TIMEOFF \
905      && defined TM_GMTOFF && (200809 < _POSIX_VERSION || ! UNINIT_TRAP))
906 # ifndef timeoff
907 #  define timeoff tz_private_timeoff
908 # endif
909 # define EXTERN_TIMEOFF
910 #endif
911 #ifdef EXTERN_TIMEOFF
912 time_t timeoff(struct tm *, long);
913 #endif
914 
915 #ifdef DEBUG
916 # undef unreachable
917 # define unreachable() abort()
918 #elif !defined unreachable
919 # ifdef __has_builtin
920 #  if __has_builtin(__builtin_unreachable)
921 #   define unreachable() __builtin_unreachable()
922 #  endif
923 # elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
924 #  define unreachable() __builtin_unreachable()
925 # endif
926 # ifndef unreachable
927 #  define unreachable() ((void) 0)
928 # endif
929 #endif
930 
931 /*
932 ** For the benefit of GNU folk...
933 ** '_(MSGID)' uses the current locale's message library string for MSGID.
934 ** The default is to use gettext if available, and use MSGID otherwise.
935 */
936 
937 #if HAVE_GETTEXT
938 #define _(msgid) gettext(msgid)
939 #else /* !HAVE_GETTEXT */
940 #define _(msgid) msgid
941 #endif /* !HAVE_GETTEXT */
942 
943 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
944 # define TZ_DOMAIN "tz"
945 #endif
946 
947 #if HAVE_INCOMPATIBLE_CTIME_R
948 #undef asctime_r
949 #undef ctime_r
950 char *asctime_r(struct tm const *restrict, char *restrict);
951 char *ctime_r(time_t const *, char *);
952 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
953 
954 /* Handy macros that are independent of tzfile implementation.  */
955 
956 enum {
957   SECSPERMIN = 60,
958   MINSPERHOUR = 60,
959   SECSPERHOUR = SECSPERMIN * MINSPERHOUR,
960   HOURSPERDAY = 24,
961   DAYSPERWEEK = 7,
962   DAYSPERNYEAR = 365,
963   DAYSPERLYEAR = DAYSPERNYEAR + 1,
964   MONSPERYEAR = 12,
965   YEARSPERREPEAT = 400	/* years before a Gregorian repeat */
966 };
967 
968 #define SECSPERDAY	((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
969 
970 #define DAYSPERREPEAT		((int_fast32_t) 400 * 365 + 100 - 4 + 1)
971 #define SECSPERREPEAT		((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
972 #define AVGSECSPERYEAR		(SECSPERREPEAT / YEARSPERREPEAT)
973 
974 /* How many years to generate (in zic.c) or search through (in localtime.c).
975    This is two years larger than the obvious 400, to avoid edge cases.
976    E.g., suppose a non-POSIX.1-2017 rule applies from 2012 on with transitions
977    in March and September, plus one-off transitions in November 2013.
978    If zic looked only at the last 400 years, it would set max_year=2413,
979    with the intent that the 400 years 2014 through 2413 will be repeated.
980    The last transition listed in the tzfile would be in 2413-09,
981    less than 400 years after the last one-off transition in 2013-11.
982    Two years is not overkill for localtime.c, as a one-year bump
983    would mishandle 2023d's America/Ciudad_Juarez for November 2422.  */
984 enum { years_of_observations = YEARSPERREPEAT + 2 };
985 
986 enum {
987   TM_SUNDAY,
988   TM_MONDAY,
989   TM_TUESDAY,
990   TM_WEDNESDAY,
991   TM_THURSDAY,
992   TM_FRIDAY,
993   TM_SATURDAY
994 };
995 
996 enum {
997   TM_JANUARY,
998   TM_FEBRUARY,
999   TM_MARCH,
1000   TM_APRIL,
1001   TM_MAY,
1002   TM_JUNE,
1003   TM_JULY,
1004   TM_AUGUST,
1005   TM_SEPTEMBER,
1006   TM_OCTOBER,
1007   TM_NOVEMBER,
1008   TM_DECEMBER
1009 };
1010 
1011 enum {
1012   TM_YEAR_BASE = 1900,
1013   TM_WDAY_BASE = TM_MONDAY,
1014   EPOCH_YEAR = 1970,
1015   EPOCH_WDAY = TM_THURSDAY
1016 };
1017 
1018 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
1019 
1020 /*
1021 ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
1022 **	isleap(y) == isleap(y % 400)
1023 ** and so
1024 **	isleap(a + b) == isleap((a + b) % 400)
1025 ** or
1026 **	isleap(a + b) == isleap(a % 400 + b % 400)
1027 ** This is true even if % means modulo rather than Fortran remainder
1028 ** (which is allowed by C89 but not by C99 or later).
1029 ** We use this to avoid addition overflow problems.
1030 */
1031 
1032 #define isleap_sum(a, b)	isleap((a) % 400 + (b) % 400)
1033 
1034 #endif /* !defined PRIVATE_H */
1035