Home | History | Annotate | Line # | Download | only in time
private.h revision 1.67
      1 /* Private header for tzdb code.  */
      2 
      3 /*	$NetBSD: private.h,v 1.67 2023/12/23 20:48:38 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 # define static_assert(cond) extern int static_assert_check[(cond) ? 1 : -1]
     64 #endif
     65 
     66 /*
     67 ** zdump has been made independent of the rest of the time
     68 ** conversion package to increase confidence in the verification it provides.
     69 ** You can use zdump to help in verifying other implementations.
     70 ** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
     71 */
     72 #ifndef USE_LTZ
     73 # define USE_LTZ 1
     74 #endif
     75 
     76 /* This string was in the Factory zone through version 2016f.  */
     77 #define GRANDPARENTED	"Local time zone must be set--see zic manual page"
     78 
     79 /*
     80 ** Defaults for preprocessor symbols.
     81 ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
     82 */
     83 
     84 #ifndef HAVE_DECL_ASCTIME_R
     85 # define HAVE_DECL_ASCTIME_R 1
     86 #endif
     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 #ifndef HAVE_STRFTIME_L
    256 # if _POSIX_VERSION < 200809
    257 #  define HAVE_STRFTIME_L 0
    258 # else
    259 #  define HAVE_STRFTIME_L 1
    260 # endif
    261 #endif
    262 
    263 #ifndef USG_COMPAT
    264 # ifndef _XOPEN_VERSION
    265 #  define USG_COMPAT 0
    266 # else
    267 #  define USG_COMPAT 1
    268 # endif
    269 #endif
    270 
    271 #ifndef HAVE_TZNAME
    272 # if _POSIX_VERSION < 198808 && !USG_COMPAT
    273 #  define HAVE_TZNAME 0
    274 # else
    275 #  define HAVE_TZNAME 1
    276 # endif
    277 #endif
    278 
    279 #ifndef ALTZONE
    280 # if defined __sun || defined _M_XENIX
    281 #  define ALTZONE 1
    282 # else
    283 #  define ALTZONE 0
    284 # endif
    285 #endif
    286 
    287 #ifndef R_OK
    288 # define R_OK 4
    289 #endif /* !defined R_OK */
    290 
    291 #if PORT_TO_C89
    292 
    293 /*
    294 ** Define HAVE_STDINT_H's default value here, rather than at the
    295 ** start, since __GLIBC__ and INTMAX_MAX's values depend on
    296 ** previously included files.  glibc 2.1 and Solaris 10 and later have
    297 ** stdint.h, even with pre-C99 compilers.
    298 */
    299 #if !defined HAVE_STDINT_H && defined __has_include
    300 # define HAVE_STDINT_H true /* C23 __has_include implies C99 stdint.h.  */
    301 #endif
    302 #ifndef HAVE_STDINT_H
    303 # define HAVE_STDINT_H \
    304    (199901 <= __STDC_VERSION__ \
    305     || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
    306     || __CYGWIN__ || INTMAX_MAX)
    307 #endif /* !defined HAVE_STDINT_H */
    308 
    309 #if HAVE_STDINT_H
    310 # include <stdint.h>
    311 #endif /* !HAVE_STDINT_H */
    312 
    313 #ifndef HAVE_INTTYPES_H
    314 # define HAVE_INTTYPES_H HAVE_STDINT_H
    315 #endif
    316 #if HAVE_INTTYPES_H
    317 # include <inttypes.h>
    318 #endif
    319 
    320 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX.  */
    321 #if defined __LONG_LONG_MAX__ && !defined __STRICT_ANSI__
    322 # ifndef LLONG_MAX
    323 #  define LLONG_MAX __LONG_LONG_MAX__
    324 # endif
    325 # ifndef LLONG_MIN
    326 #  define LLONG_MIN (-1 - LLONG_MAX)
    327 # endif
    328 # ifndef ULLONG_MAX
    329 #  define ULLONG_MAX (LLONG_MAX * 2ull + 1)
    330 # endif
    331 #endif
    332 
    333 #ifndef INT_FAST64_MAX
    334 # if 1 <= LONG_MAX >> 31 >> 31
    335 typedef long int_fast64_t;
    336 #  define INT_FAST64_MIN LONG_MIN
    337 #  define INT_FAST64_MAX LONG_MAX
    338 # else
    339 /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler.  */
    340 typedef long long int_fast64_t;
    341 #  define INT_FAST64_MIN LLONG_MIN
    342 #  define INT_FAST64_MAX LLONG_MAX
    343 # endif
    344 #endif
    345 
    346 #ifndef PRIdFAST64
    347 # if INT_FAST64_MAX == LONG_MAX
    348 #  define PRIdFAST64 "ld"
    349 # else
    350 #  define PRIdFAST64 "lld"
    351 # endif
    352 #endif
    353 
    354 #ifndef SCNdFAST64
    355 # define SCNdFAST64 PRIdFAST64
    356 #endif
    357 
    358 #ifndef INT_FAST32_MAX
    359 # if INT_MAX >> 31 == 0
    360 typedef long int_fast32_t;
    361 #  define INT_FAST32_MAX LONG_MAX
    362 #  define INT_FAST32_MIN LONG_MIN
    363 # else
    364 typedef int int_fast32_t;
    365 #  define INT_FAST32_MAX INT_MAX
    366 #  define INT_FAST32_MIN INT_MIN
    367 # endif
    368 #endif
    369 
    370 #ifndef INTMAX_MAX
    371 # ifdef LLONG_MAX
    372 typedef long long intmax_t;
    373 #  ifndef HAVE_STRTOLL
    374 #   define HAVE_STRTOLL true
    375 #  endif
    376 #  if HAVE_STRTOLL
    377 #   define strtoimax strtoll
    378 #  endif
    379 #  define INTMAX_MAX LLONG_MAX
    380 #  define INTMAX_MIN LLONG_MIN
    381 # else
    382 typedef long intmax_t;
    383 #  define INTMAX_MAX LONG_MAX
    384 #  define INTMAX_MIN LONG_MIN
    385 # endif
    386 # ifndef strtoimax
    387 #  define strtoimax strtol
    388 # endif
    389 #endif
    390 
    391 #ifndef PRIdMAX
    392 # if INTMAX_MAX == LLONG_MAX
    393 #  define PRIdMAX "lld"
    394 # else
    395 #  define PRIdMAX "ld"
    396 # endif
    397 #endif
    398 
    399 #ifndef PTRDIFF_MAX
    400 # define PTRDIFF_MAX MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t))
    401 #endif
    402 
    403 #ifndef UINT_FAST32_MAX
    404 typedef unsigned long uint_fast32_t;
    405 #endif
    406 
    407 #ifndef UINT_FAST64_MAX
    408 # if 3 <= ULONG_MAX >> 31 >> 31
    409 typedef unsigned long uint_fast64_t;
    410 #  define UINT_FAST64_MAX ULONG_MAX
    411 # else
    412 /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler.  */
    413 typedef unsigned long long uint_fast64_t;
    414 #  define UINT_FAST64_MAX ULLONG_MAX
    415 # endif
    416 #endif
    417 
    418 #ifndef UINTMAX_MAX
    419 # ifdef ULLONG_MAX
    420 typedef unsigned long long uintmax_t;
    421 #  define UINTMAX_MAX ULLONG_MAX
    422 # else
    423 typedef unsigned long uintmax_t;
    424 #  define UINTMAX_MAX ULONG_MAX
    425 # endif
    426 #endif
    427 
    428 #ifndef PRIuMAX
    429 # ifdef ULLONG_MAX
    430 #  define PRIuMAX "llu"
    431 # else
    432 #  define PRIuMAX "lu"
    433 # endif
    434 #endif
    435 
    436 #ifndef SIZE_MAX
    437 # define SIZE_MAX ((size_t) -1)
    438 #endif
    439 
    440 #endif /* PORT_TO_C89 */
    441 
    442 /* The maximum size of any created object, as a signed integer.
    443    Although the C standard does not outright prohibit larger objects,
    444    behavior is undefined if the result of pointer subtraction does not
    445    fit into ptrdiff_t, and the code assumes in several places that
    446    pointer subtraction works.  As a practical matter it's OK to not
    447    support objects larger than this.  */
    448 #define INDEX_MAX ((ptrdiff_t) min(PTRDIFF_MAX, SIZE_MAX))
    449 
    450 /* Support ckd_add, ckd_sub, ckd_mul on C23 or recent-enough GCC-like
    451    hosts, unless compiled with -DHAVE_STDCKDINT_H=0 or with pre-C23 EDG.  */
    452 #if !defined HAVE_STDCKDINT_H && defined __has_include
    453 # if __has_include(<stdckdint.h>)
    454 #  define HAVE_STDCKDINT_H true
    455 # endif
    456 #endif
    457 #ifdef HAVE_STDCKDINT_H
    458 # if HAVE_STDCKDINT_H
    459 #  include <stdckdint.h>
    460 # endif
    461 #elif defined __EDG__
    462 /* Do nothing, to work around EDG bug <https://bugs.gnu.org/53256>.  */
    463 #elif defined __has_builtin
    464 # if __has_builtin(__builtin_add_overflow)
    465 #  define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
    466 # endif
    467 # if __has_builtin(__builtin_sub_overflow)
    468 #  define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
    469 # endif
    470 # if __has_builtin(__builtin_mul_overflow)
    471 #  define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
    472 # endif
    473 #elif 7 <= __GNUC__
    474 # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
    475 # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
    476 # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
    477 #endif
    478 
    479 #if 3 <= __GNUC__
    480 # define ATTRIBUTE_MALLOC __attribute__((__malloc__))
    481 # define ATTRIBUTE_FORMAT(spec) __attribute__((__format__ spec))
    482 #else
    483 # define ATTRIBUTE_MALLOC /* empty */
    484 # define ATTRIBUTE_FORMAT(spec) /* empty */
    485 #endif
    486 
    487 #if (defined __has_c_attribute \
    488      && (202311 <= __STDC_VERSION__ || !defined __STRICT_ANSI__))
    489 # define HAVE___HAS_C_ATTRIBUTE true
    490 #else
    491 # define HAVE___HAS_C_ATTRIBUTE false
    492 #endif
    493 
    494 #if HAVE___HAS_C_ATTRIBUTE
    495 # if __has_c_attribute(deprecated)
    496 #  define ATTRIBUTE_DEPRECATED [[deprecated]]
    497 # endif
    498 #endif
    499 #ifndef ATTRIBUTE_DEPRECATED
    500 # if 3 < __GNUC__ + (2 <= __GNUC_MINOR__)
    501 #  define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
    502 # else
    503 #  define ATTRIBUTE_DEPRECATED /* empty */
    504 # endif
    505 #endif
    506 
    507 #if HAVE___HAS_C_ATTRIBUTE
    508 # if __has_c_attribute(fallthrough)
    509 #  define ATTRIBUTE_FALLTHROUGH [[fallthrough]]
    510 # endif
    511 #endif
    512 #ifndef ATTRIBUTE_FALLTHROUGH
    513 # if 7 <= __GNUC__
    514 #  define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
    515 # else
    516 #  define ATTRIBUTE_FALLTHROUGH ((void) 0)
    517 # endif
    518 #endif
    519 
    520 #if HAVE___HAS_C_ATTRIBUTE
    521 # if __has_c_attribute(maybe_unused)
    522 #  define ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]]
    523 # endif
    524 #endif
    525 #ifndef ATTRIBUTE_MAYBE_UNUSED
    526 # if 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
    527 #  define ATTRIBUTE_MAYBE_UNUSED __attribute__((unused))
    528 # else
    529 #  define ATTRIBUTE_MAYBE_UNUSED /* empty */
    530 # endif
    531 #endif
    532 
    533 #if HAVE___HAS_C_ATTRIBUTE
    534 # if __has_c_attribute(noreturn)
    535 #  define ATTRIBUTE_NORETURN [[noreturn]]
    536 # endif
    537 #endif
    538 #ifndef ATTRIBUTE_NORETURN
    539 # if 201112 <= __STDC_VERSION__
    540 #  define ATTRIBUTE_NORETURN _Noreturn
    541 # elif 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
    542 #  define ATTRIBUTE_NORETURN __attribute__((noreturn))
    543 # else
    544 #  define ATTRIBUTE_NORETURN /* empty */
    545 # endif
    546 #endif
    547 
    548 #if HAVE___HAS_C_ATTRIBUTE
    549 # if __has_c_attribute(reproducible)
    550 #  define ATTRIBUTE_REPRODUCIBLE [[reproducible]]
    551 # endif
    552 #endif
    553 #ifndef ATTRIBUTE_REPRODUCIBLE
    554 # if 3 <= __GNUC__
    555 #  define ATTRIBUTE_REPRODUCIBLE __attribute__((pure))
    556 # else
    557 #  define ATTRIBUTE_REPRODUCIBLE /* empty */
    558 # endif
    559 #endif
    560 
    561 #if HAVE___HAS_C_ATTRIBUTE
    562 # if __has_c_attribute(unsequenced)
    563 #  define ATTRIBUTE_UNSEQUENCED [[unsequenced]]
    564 # endif
    565 #endif
    566 #ifndef ATTRIBUTE_UNSEQUENCED
    567 # if 3 <= __GNUC__
    568 #  define ATTRIBUTE_UNSEQUENCED __attribute__((const))
    569 # else
    570 #  define ATTRIBUTE_UNSEQUENCED /* empty */
    571 # endif
    572 #endif
    573 
    574 #if (__STDC_VERSION__ < 199901 && !defined restrict \
    575      && (PORT_TO_C89 || defined _MSC_VER))
    576 # define restrict /* empty */
    577 #endif
    578 
    579 /*
    580 ** Workarounds for compilers/systems.
    581 */
    582 
    583 #ifndef EPOCH_LOCAL
    584 # define EPOCH_LOCAL 0
    585 #endif
    586 #ifndef EPOCH_OFFSET
    587 # define EPOCH_OFFSET 0
    588 #endif
    589 #ifndef RESERVE_STD_EXT_IDS
    590 # define RESERVE_STD_EXT_IDS 0
    591 #endif
    592 
    593 /* If standard C identifiers with external linkage (e.g., localtime)
    594    are reserved and are not already being renamed anyway, rename them
    595    as if compiling with '-Dtime_tz=time_t'.  */
    596 #if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
    597 # define time_tz time_t
    598 #endif
    599 
    600 /*
    601 ** Compile with -Dtime_tz=T to build the tz package with a private
    602 ** time_t type equivalent to T rather than the system-supplied time_t.
    603 ** This debugging feature can test unusual design decisions
    604 ** (e.g., time_t wider than 'long', or unsigned time_t) even on
    605 ** typical platforms.
    606 */
    607 #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
    608 # define TZ_TIME_T 1
    609 #else
    610 # define TZ_TIME_T 0
    611 #endif
    612 
    613 #if defined LOCALTIME_IMPLEMENTATION && TZ_TIME_T
    614 static time_t sys_time(time_t *x) { return time(x); }
    615 #endif
    616 
    617 #if TZ_TIME_T
    618 
    619 typedef time_tz tz_time_t;
    620 
    621 # undef  asctime
    622 # define asctime tz_asctime
    623 # undef  asctime_r
    624 # define asctime_r tz_asctime_r
    625 # undef  ctime
    626 # define ctime tz_ctime
    627 # undef  ctime_r
    628 # define ctime_r tz_ctime_r
    629 # undef  difftime
    630 # define difftime tz_difftime
    631 # undef  gmtime
    632 # define gmtime tz_gmtime
    633 # undef  gmtime_r
    634 # define gmtime_r tz_gmtime_r
    635 # undef  localtime
    636 # define localtime tz_localtime
    637 # undef  localtime_r
    638 # define localtime_r tz_localtime_r
    639 # undef  localtime_rz
    640 # define localtime_rz tz_localtime_rz
    641 # undef  mktime
    642 # define mktime tz_mktime
    643 # undef  mktime_z
    644 # define mktime_z tz_mktime_z
    645 # undef  offtime
    646 # define offtime tz_offtime
    647 # undef  posix2time
    648 # define posix2time tz_posix2time
    649 # undef  posix2time_z
    650 # define posix2time_z tz_posix2time_z
    651 # undef  strftime
    652 # define strftime tz_strftime
    653 # undef  time
    654 # define time tz_time
    655 # undef  time2posix
    656 # define time2posix tz_time2posix
    657 # undef  time2posix_z
    658 # define time2posix_z tz_time2posix_z
    659 # undef  time_t
    660 # define time_t tz_time_t
    661 # undef  timegm
    662 # define timegm tz_timegm
    663 # undef  timelocal
    664 # define timelocal tz_timelocal
    665 # undef  timeoff
    666 # define timeoff tz_timeoff
    667 # undef  tzalloc
    668 # define tzalloc tz_tzalloc
    669 # undef  tzfree
    670 # define tzfree tz_tzfree
    671 # undef  tzset
    672 # define tzset tz_tzset
    673 # undef  tzsetwall
    674 # define tzsetwall tz_tzsetwall
    675 # if HAVE_STRFTIME_L
    676 #  undef  strftime_l
    677 #  define strftime_l tz_strftime_l
    678 # endif
    679 # if HAVE_TZNAME
    680 #  undef  tzname
    681 #  define tzname tz_tzname
    682 # endif
    683 # if USG_COMPAT
    684 #  undef  daylight
    685 #  define daylight tz_daylight
    686 #  undef  timezone
    687 #  define timezone tz_timezone
    688 # endif
    689 # if ALTZONE
    690 #  undef  altzone
    691 #  define altzone tz_altzone
    692 # endif
    693 
    694 # if __STDC_VERSION__ < 202311
    695 #  define DEPRECATED_IN_C23 /* empty */
    696 # else
    697 #  define DEPRECATED_IN_C23 ATTRIBUTE_DEPRECATED
    698 # endif
    699 DEPRECATED_IN_C23 char *asctime(struct tm const *);
    700 char *asctime_r(struct tm const *restrict, char *restrict);
    701 DEPRECATED_IN_C23 char *ctime(time_t const *);
    702 char *ctime_r(time_t const *, char *);
    703 ATTRIBUTE_UNSEQUENCED double difftime(time_t, time_t);
    704 size_t strftime(char *restrict, size_t, char const *restrict,
    705 		struct tm const *restrict);
    706 # if HAVE_STRFTIME_L
    707 size_t strftime_l(char *restrict, size_t, char const *restrict,
    708 		  struct tm const *restrict, locale_t);
    709 # endif
    710 struct tm *gmtime(time_t const *);
    711 struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
    712 struct tm *localtime(time_t const *);
    713 struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
    714 time_t mktime(struct tm *);
    715 time_t time(time_t *);
    716 time_t timegm(struct tm *);
    717 void tzset(void);
    718 #endif
    719 
    720 #ifndef HAVE_DECL_TIMEGM
    721 # if (202311 <= __STDC_VERSION__ \
    722       || defined __GLIBC__ || defined __tm_zone /* musl */ \
    723       || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
    724       || (defined __APPLE__ && defined __MACH__))
    725 #  define HAVE_DECL_TIMEGM true
    726 # else
    727 #  define HAVE_DECL_TIMEGM false
    728 # endif
    729 #endif
    730 #if !HAVE_DECL_TIMEGM && !defined timegm
    731 time_t timegm(struct tm *);
    732 #endif
    733 
    734 #if !HAVE_DECL_ASCTIME_R && !defined asctime_r
    735 extern char *asctime_r(struct tm const *restrict, char *restrict);
    736 #endif
    737 
    738 #ifndef HAVE_DECL_ENVIRON
    739 # if defined environ || defined __USE_GNU
    740 #  define HAVE_DECL_ENVIRON 1
    741 # else
    742 #  define HAVE_DECL_ENVIRON 0
    743 # endif
    744 #endif
    745 
    746 #if !HAVE_DECL_ENVIRON
    747 extern char **environ;
    748 #endif
    749 
    750 #if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
    751 extern char *tzname[];
    752 #endif
    753 #if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
    754 extern long timezone;
    755 extern int daylight;
    756 #endif
    757 #if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
    758 extern long altzone;
    759 #endif
    760 
    761 /*
    762 ** The STD_INSPIRED functions are similar, but most also need
    763 ** declarations if time_tz is defined.
    764 */
    765 
    766 #ifndef STD_INSPIRED
    767 # define STD_INSPIRED 0
    768 #endif
    769 #if STD_INSPIRED
    770 # if TZ_TIME_T || !defined tzsetwall
    771 void tzsetwall(void);
    772 # endif
    773 # if TZ_TIME_T || !defined offtime
    774 struct tm *offtime(time_t const *, long);
    775 # endif
    776 # if TZ_TIME_T || !defined timelocal
    777 time_t timelocal(struct tm *);
    778 # endif
    779 # if TZ_TIME_T || !defined timeoff
    780 time_t timeoff(struct tm *, long);
    781 # endif
    782 # if TZ_TIME_T || !defined time2posix
    783 time_t time2posix(time_t);
    784 # endif
    785 # if TZ_TIME_T || !defined posix2time
    786 time_t posix2time(time_t);
    787 # endif
    788 #endif
    789 
    790 /* Infer TM_ZONE on systems where this information is known, but suppress
    791    guessing if NO_TM_ZONE is defined.  Similarly for TM_GMTOFF.  */
    792 #if (defined __GLIBC__ \
    793      || defined __tm_zone /* musl */ \
    794      || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
    795      || (defined __APPLE__ && defined __MACH__))
    796 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
    797 #  define TM_GMTOFF tm_gmtoff
    798 # endif
    799 # if !defined TM_ZONE && !defined NO_TM_ZONE
    800 #  define TM_ZONE tm_zone
    801 # endif
    802 #endif
    803 
    804 /*
    805 ** Define functions that are ABI compatible with NetBSD but have
    806 ** better prototypes.  NetBSD 6.1.4 defines a pointer type timezone_t
    807 ** and labors under the misconception that 'const timezone_t' is a
    808 ** pointer to a constant.  This use of 'const' is ineffective, so it
    809 ** is not done here.  What we call 'struct state' NetBSD calls
    810 ** 'struct __state', but this is a private name so it doesn't matter.
    811 */
    812 #ifndef __NetBSD__
    813 #if NETBSD_INSPIRED
    814 typedef struct state *timezone_t;
    815 struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
    816 			struct tm *restrict);
    817 time_t mktime_z(timezone_t restrict, struct tm *restrict);
    818 timezone_t tzalloc(char const *);
    819 void tzfree(timezone_t);
    820 # if STD_INSPIRED
    821 #  if TZ_TIME_T || !defined posix2time_z
    822 ATTRIBUTE_REPRODUCIBLE time_t posix2time_z(timezone_t __restrict, time_t);
    823 #  endif
    824 #  if TZ_TIME_T || !defined time2posix_z
    825 ATTRIBUTE_REPRODUCIBLE time_t time2posix_z(timezone_t __restrict, time_t);
    826 #  endif
    827 # endif
    828 #endif
    829 #endif
    830 
    831 /*
    832 ** Finally, some convenience items.
    833 */
    834 
    835 #define TYPE_BIT(type) (CHAR_BIT * (ptrdiff_t) sizeof(type))
    836 #define TYPE_SIGNED(type) (/*CONSTCOND*/((type) -1) < 0)
    837 #define TWOS_COMPLEMENT(t) (/*CONSTCOND*/(t) ~ (t) 0 < 0)
    838 
    839 /* Minimum and maximum of two values.  Use lower case to avoid
    840    naming clashes with standard include files.  */
    841 #define max(a, b) ((a) > (b) ? (a) : (b))
    842 #define min(a, b) ((a) < (b) ? (a) : (b))
    843 
    844 /* Max and min values of the integer type T, of which only the bottom
    845    B bits are used, and where the highest-order used bit is considered
    846    to be a sign bit if T is signed.  */
    847 #define MAXVAL(t, b) /*LINTED*/					\
    848   ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))			\
    849 	- 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
    850 #define MINVAL(t, b)						\
    851   ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
    852 
    853 /* The extreme time values, assuming no padding.  */
    854 #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
    855 #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
    856 
    857 /* The extreme time values.  These are macros, not constants, so that
    858    any portability problems occur only when compiling .c files that use
    859    the macros, which is safer for applications that need only zdump and zic.
    860    This implementation assumes no padding if time_t is signed and
    861    either the compiler lacks support for _Generic or time_t is not one
    862    of the standard signed integer types.  */
    863 #if HAVE__GENERIC
    864 # define TIME_T_MIN \
    865     _Generic((time_t) 0, \
    866 	     signed char: SCHAR_MIN, short: SHRT_MIN, \
    867 	     int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
    868 	     default: TIME_T_MIN_NO_PADDING)
    869 # define TIME_T_MAX \
    870     (TYPE_SIGNED(time_t) \
    871      ? _Generic((time_t) 0, \
    872 		signed char: SCHAR_MAX, short: SHRT_MAX, \
    873 		int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
    874 		default: TIME_T_MAX_NO_PADDING)			    \
    875      : (time_t) -1)
    876 enum { SIGNED_PADDING_CHECK_NEEDED
    877          = _Generic((time_t) 0,
    878 		    signed char: false, short: false,
    879 		    int: false, long: false, long long: false,
    880 		    default: true) };
    881 #else
    882 # define TIME_T_MIN TIME_T_MIN_NO_PADDING
    883 # define TIME_T_MAX TIME_T_MAX_NO_PADDING
    884 enum { SIGNED_PADDING_CHECK_NEEDED = true };
    885 #endif
    886 /* Try to check the padding assumptions.  Although TIME_T_MAX and the
    887    following check can both have undefined behavior on oddball
    888    platforms due to shifts exceeding widths of signed integers, these
    889    platforms' compilers are likely to diagnose these issues in integer
    890    constant expressions, so it shouldn't hurt to check statically.  */
    891 static_assert(! TYPE_SIGNED(time_t) || ! SIGNED_PADDING_CHECK_NEEDED
    892 	      || TIME_T_MAX >> (TYPE_BIT(time_t) - 2) == 1);
    893 
    894 /*
    895 ** 302 / 1000 is log10(2.0) rounded up.
    896 ** Subtract one for the sign bit if the type is signed;
    897 ** add one for integer division truncation;
    898 ** add one more for a minus sign if the type is signed.
    899 */
    900 #define INT_STRLEN_MAXIMUM(type) \
    901 	((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
    902 	1 + TYPE_SIGNED(type))
    903 
    904 /*
    905 ** INITIALIZE(x)
    906 */
    907 
    908 #if defined(__GNUC__) || defined(__lint__)
    909 # define INITIALIZE(x)	((x) = 0)
    910 #else
    911 # define INITIALIZE(x)
    912 #endif
    913 
    914 /* Whether memory access must strictly follow the C standard.
    915    If 0, it's OK to read uninitialized storage so long as the value is
    916    not relied upon.  Defining it to 0 lets mktime access parts of
    917    struct tm that might be uninitialized, as a heuristic when the
    918    standard doesn't say what to return and when tm_gmtoff can help
    919    mktime likely infer a better value.  */
    920 #ifndef UNINIT_TRAP
    921 # define UNINIT_TRAP 0
    922 #endif
    923 
    924 #ifdef DEBUG
    925 # undef unreachable
    926 # define unreachable() abort()
    927 #elif !defined unreachable
    928 # ifdef __has_builtin
    929 #  if __has_builtin(__builtin_unreachable)
    930 #   define unreachable() __builtin_unreachable()
    931 #  endif
    932 # elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
    933 #  define unreachable() __builtin_unreachable()
    934 # endif
    935 # ifndef unreachable
    936 #  define unreachable() ((void) 0)
    937 # endif
    938 #endif
    939 
    940 /*
    941 ** For the benefit of GNU folk...
    942 ** '_(MSGID)' uses the current locale's message library string for MSGID.
    943 ** The default is to use gettext if available, and use MSGID otherwise.
    944 */
    945 
    946 #if HAVE_GETTEXT
    947 #define _(msgid) gettext(msgid)
    948 #else /* !HAVE_GETTEXT */
    949 #define _(msgid) msgid
    950 #endif /* !HAVE_GETTEXT */
    951 
    952 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
    953 # define TZ_DOMAIN "tz"
    954 #endif
    955 
    956 #if HAVE_INCOMPATIBLE_CTIME_R
    957 #undef asctime_r
    958 #undef ctime_r
    959 char *asctime_r(struct tm const *restrict, char *restrict);
    960 char *ctime_r(time_t const *, char *);
    961 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
    962 
    963 /* Handy macros that are independent of tzfile implementation.  */
    964 
    965 #ifndef SECSPERMIN
    966 enum {
    967   SECSPERMIN = 60,
    968   MINSPERHOUR = 60,
    969   SECSPERHOUR = SECSPERMIN * MINSPERHOUR,
    970   HOURSPERDAY = 24,
    971   DAYSPERWEEK = 7,
    972   DAYSPERNYEAR = 365,
    973   DAYSPERLYEAR = DAYSPERNYEAR + 1,
    974   MONSPERYEAR = 12,
    975   YEARSPERREPEAT = 400	/* years before a Gregorian repeat */
    976 };
    977 #endif
    978 
    979 #define SECSPERDAY	((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
    980 
    981 #define DAYSPERREPEAT		((int_fast32_t) 400 * 365 + 100 - 4 + 1)
    982 #define SECSPERREPEAT		((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
    983 #define AVGSECSPERYEAR		(SECSPERREPEAT / YEARSPERREPEAT)
    984 
    985 #ifndef TM_SUNDAY
    986 enum {
    987   TM_SUNDAY,
    988   TM_MONDAY,
    989   TM_TUESDAY,
    990   TM_WEDNESDAY,
    991   TM_THURSDAY,
    992   TM_FRIDAY,
    993   TM_SATURDAY
    994 };
    995 #endif
    996 
    997 #ifndef TM_JANUARY
    998 enum {
    999   TM_JANUARY,
   1000   TM_FEBRUARY,
   1001   TM_MARCH,
   1002   TM_APRIL,
   1003   TM_MAY,
   1004   TM_JUNE,
   1005   TM_JULY,
   1006   TM_AUGUST,
   1007   TM_SEPTEMBER,
   1008   TM_OCTOBER,
   1009   TM_NOVEMBER,
   1010   TM_DECEMBER
   1011 };
   1012 #endif
   1013 
   1014 #ifndef TM_YEAR_BASE
   1015 enum {
   1016   TM_YEAR_BASE = 1900,
   1017   TM_WDAY_BASE = TM_MONDAY,
   1018   EPOCH_YEAR = 1970,
   1019   EPOCH_WDAY = TM_THURSDAY
   1020 };
   1021 #endif
   1022 
   1023 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
   1024 
   1025 /*
   1026 ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
   1027 **	isleap(y) == isleap(y % 400)
   1028 ** and so
   1029 **	isleap(a + b) == isleap((a + b) % 400)
   1030 ** or
   1031 **	isleap(a + b) == isleap(a % 400 + b % 400)
   1032 ** This is true even if % means modulo rather than Fortran remainder
   1033 ** (which is allowed by C89 but not by C99 or later).
   1034 ** We use this to avoid addition overflow problems.
   1035 */
   1036 
   1037 #define isleap_sum(a, b)	isleap((a) % 400 + (b) % 400)
   1038 
   1039 #ifdef _LIBC
   1040 #include "reentrant.h"
   1041 extern struct __state *__lclptr;
   1042 #if defined(__LIBC12_SOURCE__)
   1043 #define tzset_unlocked __tzset_unlocked
   1044 #else
   1045 #define tzset_unlocked __tzset_unlocked50
   1046 #endif
   1047 
   1048 void tzset_unlocked(void);
   1049 #ifdef _REENTRANT
   1050 extern rwlock_t __lcl_lock;
   1051 #endif
   1052 #endif
   1053 
   1054 #endif /* !defined PRIVATE_H */
   1055