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