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