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