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