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