Home | History | Annotate | Line # | Download | only in time
private.h revision 1.58
      1  1.53  christos /* Private header for tzdb code.  */
      2  1.53  christos 
      3  1.58  christos /*	$NetBSD: private.h,v 1.58 2022/03/22 17:48:39 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.52  christos /*
     33  1.52  christos ** zdump has been made independent of the rest of the time
     34  1.52  christos ** conversion package to increase confidence in the verification it provides.
     35  1.52  christos ** You can use zdump to help in verifying other implementations.
     36  1.52  christos ** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
     37  1.52  christos */
     38  1.52  christos #ifndef USE_LTZ
     39  1.52  christos # define USE_LTZ 1
     40  1.52  christos #endif
     41  1.52  christos 
     42  1.46  christos /* This string was in the Factory zone through version 2016f.  */
     43  1.25   mlelstv #define GRANDPARENTED	"Local time zone must be set--see zic manual page"
     44  1.25   mlelstv 
     45   1.1       jtc /*
     46   1.1       jtc ** Defaults for preprocessor symbols.
     47  1.43  christos ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
     48   1.1       jtc */
     49   1.1       jtc 
     50  1.45  christos #ifndef HAVE_DECL_ASCTIME_R
     51  1.45  christos #define HAVE_DECL_ASCTIME_R 1
     52  1.45  christos #endif
     53  1.45  christos 
     54  1.51  christos #if !defined HAVE_GENERIC && defined __has_extension
     55  1.51  christos # if __has_extension(c_generic_selections)
     56  1.51  christos #  define HAVE_GENERIC 1
     57  1.51  christos # else
     58  1.51  christos #  define HAVE_GENERIC 0
     59  1.51  christos # endif
     60  1.51  christos #endif
     61  1.51  christos /* _Generic is buggy in pre-4.9 GCC.  */
     62  1.51  christos #if !defined HAVE_GENERIC && defined __GNUC__
     63  1.51  christos # define HAVE_GENERIC (4 < __GNUC__ + (9 <= __GNUC_MINOR__))
     64  1.51  christos #endif
     65  1.51  christos #ifndef HAVE_GENERIC
     66  1.51  christos # define HAVE_GENERIC (201112 <= __STDC_VERSION__)
     67  1.51  christos #endif
     68  1.51  christos 
     69   1.6       jtc #ifndef HAVE_GETTEXT
     70   1.6       jtc #define HAVE_GETTEXT		0
     71   1.6       jtc #endif /* !defined HAVE_GETTEXT */
     72   1.6       jtc 
     73  1.17    kleink #ifndef HAVE_INCOMPATIBLE_CTIME_R
     74  1.17    kleink #define HAVE_INCOMPATIBLE_CTIME_R	0
     75  1.51  christos #endif
     76  1.17    kleink 
     77  1.32  christos #ifndef HAVE_LINK
     78  1.32  christos #define HAVE_LINK		1
     79  1.32  christos #endif /* !defined HAVE_LINK */
     80  1.32  christos 
     81  1.57  christos #ifndef HAVE_MALLOC_ERRNO
     82  1.57  christos #define HAVE_MALLOC_ERRNO 1
     83  1.57  christos #endif
     84  1.57  christos 
     85  1.45  christos #ifndef HAVE_POSIX_DECLS
     86  1.45  christos #define HAVE_POSIX_DECLS 1
     87  1.45  christos #endif
     88  1.45  christos 
     89  1.51  christos #ifndef HAVE_STDBOOL_H
     90  1.51  christos #define HAVE_STDBOOL_H (199901 <= __STDC_VERSION__)
     91  1.51  christos #endif
     92  1.51  christos 
     93  1.40  christos #ifndef HAVE_STRDUP
     94  1.40  christos #define HAVE_STRDUP 1
     95  1.40  christos #endif
     96  1.40  christos 
     97  1.52  christos #ifndef HAVE_STRTOLL
     98  1.52  christos #define HAVE_STRTOLL 1
     99  1.52  christos #endif
    100  1.52  christos 
    101  1.12    kleink #ifndef HAVE_SYMLINK
    102  1.12    kleink #define HAVE_SYMLINK		1
    103  1.12    kleink #endif /* !defined HAVE_SYMLINK */
    104   1.6       jtc 
    105  1.20    kleink #ifndef HAVE_SYS_STAT_H
    106  1.20    kleink #define HAVE_SYS_STAT_H		1
    107  1.20    kleink #endif /* !defined HAVE_SYS_STAT_H */
    108  1.20    kleink 
    109   1.1       jtc #ifndef HAVE_UNISTD_H
    110   1.1       jtc #define HAVE_UNISTD_H		1
    111   1.1       jtc #endif /* !defined HAVE_UNISTD_H */
    112   1.1       jtc 
    113   1.5       jtc #ifndef HAVE_UTMPX_H
    114  1.43  christos #define HAVE_UTMPX_H		1
    115   1.5       jtc #endif /* !defined HAVE_UTMPX_H */
    116   1.5       jtc 
    117  1.36  christos #ifndef NETBSD_INSPIRED
    118  1.36  christos # define NETBSD_INSPIRED 1
    119  1.36  christos #endif
    120   1.1       jtc 
    121  1.17    kleink #if HAVE_INCOMPATIBLE_CTIME_R
    122  1.17    kleink #define asctime_r _incompatible_asctime_r
    123  1.17    kleink #define ctime_r _incompatible_ctime_r
    124  1.17    kleink #endif /* HAVE_INCOMPATIBLE_CTIME_R */
    125  1.17    kleink 
    126  1.51  christos /* Enable tm_gmtoff, tm_zone, and environ on GNUish systems.  */
    127  1.36  christos #define _GNU_SOURCE 1
    128  1.47  christos /* Fix asctime_r on Solaris 11.  */
    129  1.36  christos #define _POSIX_PTHREAD_SEMANTICS 1
    130  1.47  christos /* Enable strtoimax on pre-C99 Solaris 11.  */
    131  1.36  christos #define __EXTENSIONS__ 1
    132  1.36  christos 
    133  1.51  christos /* To avoid having 'stat' fail unnecessarily with errno == EOVERFLOW,
    134  1.51  christos    enable large files on GNUish systems ...  */
    135  1.51  christos #ifndef _FILE_OFFSET_BITS
    136  1.51  christos # define _FILE_OFFSET_BITS 64
    137  1.51  christos #endif
    138  1.51  christos /* ... and on AIX ...  */
    139  1.51  christos #define _LARGE_FILES 1
    140  1.51  christos /* ... and enable large inode numbers on Mac OS X 10.5 and later.  */
    141  1.51  christos #define _DARWIN_USE_64_BIT_INODE 1
    142  1.51  christos 
    143   1.1       jtc /*
    144   1.1       jtc ** Nested includes
    145   1.1       jtc */
    146   1.1       jtc 
    147  1.36  christos #ifndef __NetBSD__
    148  1.56  christos /* Avoid clashes with NetBSD by renaming NetBSD's declarations.
    149  1.56  christos    If defining the 'timezone' variable, avoid a clash with FreeBSD's
    150  1.56  christos    'timezone' function by renaming its declaration.  */
    151  1.36  christos #define localtime_rz sys_localtime_rz
    152  1.36  christos #define mktime_z sys_mktime_z
    153  1.36  christos #define posix2time_z sys_posix2time_z
    154  1.36  christos #define time2posix_z sys_time2posix_z
    155  1.56  christos #if defined USG_COMPAT && USG_COMPAT == 2
    156  1.56  christos # define timezone sys_timezone
    157  1.56  christos #endif
    158  1.36  christos #define timezone_t sys_timezone_t
    159  1.36  christos #define tzalloc sys_tzalloc
    160  1.36  christos #define tzfree sys_tzfree
    161  1.36  christos #include <time.h>
    162  1.36  christos #undef localtime_rz
    163  1.36  christos #undef mktime_z
    164  1.36  christos #undef posix2time_z
    165  1.36  christos #undef time2posix_z
    166  1.56  christos #if defined USG_COMPAT && USG_COMPAT == 2
    167  1.56  christos # undef timezone
    168  1.56  christos #endif
    169  1.36  christos #undef timezone_t
    170  1.36  christos #undef tzalloc
    171  1.36  christos #undef tzfree
    172  1.36  christos #else
    173  1.36  christos #include "time.h"
    174  1.36  christos #endif
    175  1.36  christos 
    176  1.50  christos #include <sys/types.h>	/* for time_t */
    177  1.50  christos #include <string.h>
    178  1.50  christos #include <limits.h>	/* for CHAR_BIT et al. */
    179  1.50  christos #include <stdlib.h>
    180   1.1       jtc 
    181  1.50  christos #include <errno.h>
    182  1.38  christos 
    183  1.57  christos #ifndef EINVAL
    184  1.57  christos # define EINVAL ERANGE
    185  1.57  christos #endif
    186  1.57  christos 
    187  1.38  christos #ifndef ENAMETOOLONG
    188  1.38  christos # define ENAMETOOLONG EINVAL
    189  1.38  christos #endif
    190  1.57  christos #ifndef ENOMEM
    191  1.57  christos # define ENOMEM EINVAL
    192  1.57  christos #endif
    193  1.45  christos #ifndef ENOTSUP
    194  1.45  christos # define ENOTSUP EINVAL
    195  1.45  christos #endif
    196  1.38  christos #ifndef EOVERFLOW
    197  1.38  christos # define EOVERFLOW EINVAL
    198  1.38  christos #endif
    199  1.38  christos 
    200  1.25   mlelstv #if HAVE_GETTEXT
    201  1.50  christos #include <libintl.h>
    202  1.25   mlelstv #endif /* HAVE_GETTEXT */
    203  1.14    kleink 
    204  1.25   mlelstv #if HAVE_UNISTD_H
    205  1.51  christos #include <unistd.h>	/* for R_OK, and other POSIX goodness */
    206  1.25   mlelstv #endif /* HAVE_UNISTD_H */
    207   1.1       jtc 
    208  1.36  christos #ifndef HAVE_STRFTIME_L
    209  1.36  christos # if _POSIX_VERSION < 200809
    210  1.36  christos #  define HAVE_STRFTIME_L 0
    211  1.36  christos # else
    212  1.36  christos #  define HAVE_STRFTIME_L 1
    213  1.36  christos # endif
    214  1.36  christos #endif
    215  1.36  christos 
    216  1.51  christos #ifndef USG_COMPAT
    217  1.51  christos # ifndef _XOPEN_VERSION
    218  1.51  christos #  define USG_COMPAT 0
    219  1.51  christos # else
    220  1.51  christos #  define USG_COMPAT 1
    221  1.51  christos # endif
    222  1.51  christos #endif
    223  1.51  christos 
    224  1.51  christos #ifndef HAVE_TZNAME
    225  1.51  christos # if _POSIX_VERSION < 198808 && !USG_COMPAT
    226  1.51  christos #  define HAVE_TZNAME 0
    227  1.51  christos # else
    228  1.51  christos #  define HAVE_TZNAME 1
    229  1.51  christos # endif
    230  1.51  christos #endif
    231  1.51  christos 
    232  1.56  christos #ifndef ALTZONE
    233  1.56  christos # if defined __sun || defined _M_XENIX
    234  1.56  christos #  define ALTZONE 1
    235  1.56  christos # else
    236  1.56  christos #  define ALTZONE 0
    237  1.56  christos # endif
    238  1.56  christos #endif
    239  1.56  christos 
    240   1.1       jtc #ifndef R_OK
    241   1.1       jtc #define R_OK	4
    242   1.1       jtc #endif /* !defined R_OK */
    243   1.1       jtc 
    244   1.1       jtc /*
    245  1.25   mlelstv ** Define HAVE_STDINT_H's default value here, rather than at the
    246  1.47  christos ** start, since __GLIBC__ and INTMAX_MAX's values depend on
    247  1.47  christos ** previously-included files.  glibc 2.1 and Solaris 10 and later have
    248  1.47  christos ** stdint.h, even with pre-C99 compilers.
    249  1.25   mlelstv */
    250  1.25   mlelstv #ifndef HAVE_STDINT_H
    251  1.25   mlelstv #define HAVE_STDINT_H \
    252  1.33  christos    (199901 <= __STDC_VERSION__ \
    253  1.33  christos     || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__)	\
    254  1.47  christos     || __CYGWIN__ || INTMAX_MAX)
    255  1.25   mlelstv #endif /* !defined HAVE_STDINT_H */
    256  1.25   mlelstv 
    257  1.25   mlelstv #if HAVE_STDINT_H
    258  1.50  christos #include <stdint.h>
    259  1.25   mlelstv #endif /* !HAVE_STDINT_H */
    260  1.25   mlelstv 
    261  1.29  christos #ifndef HAVE_INTTYPES_H
    262  1.29  christos # define HAVE_INTTYPES_H HAVE_STDINT_H
    263  1.29  christos #endif
    264  1.29  christos #if HAVE_INTTYPES_H
    265  1.29  christos # include <inttypes.h>
    266  1.29  christos #endif
    267  1.29  christos 
    268  1.36  christos /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX.  */
    269  1.36  christos #ifdef __LONG_LONG_MAX__
    270  1.36  christos # ifndef LLONG_MAX
    271  1.36  christos #  define LLONG_MAX __LONG_LONG_MAX__
    272  1.36  christos # endif
    273  1.36  christos # ifndef LLONG_MIN
    274  1.36  christos #  define LLONG_MIN (-1 - LLONG_MAX)
    275  1.36  christos # endif
    276  1.36  christos #endif
    277  1.36  christos 
    278  1.25   mlelstv #ifndef INT_FAST64_MAX
    279  1.36  christos # ifdef LLONG_MAX
    280  1.25   mlelstv typedef long long	int_fast64_t;
    281  1.29  christos #  define INT_FAST64_MIN LLONG_MIN
    282  1.29  christos #  define INT_FAST64_MAX LLONG_MAX
    283  1.29  christos # else
    284  1.43  christos #  if LONG_MAX >> 31 < 0xffffffff
    285  1.25   mlelstv Please use a compiler that supports a 64-bit integer type (or wider);
    286  1.25   mlelstv you may need to compile with "-DHAVE_STDINT_H".
    287  1.36  christos #  endif
    288  1.25   mlelstv typedef long		int_fast64_t;
    289  1.36  christos #  define INT_FAST64_MIN LONG_MIN
    290  1.36  christos #  define INT_FAST64_MAX LONG_MAX
    291  1.36  christos # endif
    292  1.36  christos #endif
    293  1.36  christos 
    294  1.48  christos #ifndef PRIdFAST64
    295  1.36  christos # if INT_FAST64_MAX == LLONG_MAX
    296  1.48  christos #  define PRIdFAST64 "lld"
    297  1.36  christos # else
    298  1.48  christos #  define PRIdFAST64 "ld"
    299  1.36  christos # endif
    300  1.36  christos #endif
    301  1.25   mlelstv 
    302  1.48  christos #ifndef SCNdFAST64
    303  1.48  christos # define SCNdFAST64 PRIdFAST64
    304  1.48  christos #endif
    305  1.48  christos 
    306  1.29  christos #ifndef INT_FAST32_MAX
    307  1.29  christos # if INT_MAX >> 31 == 0
    308  1.29  christos typedef long int_fast32_t;
    309  1.36  christos #  define INT_FAST32_MAX LONG_MAX
    310  1.36  christos #  define INT_FAST32_MIN LONG_MIN
    311  1.29  christos # else
    312  1.29  christos typedef int int_fast32_t;
    313  1.36  christos #  define INT_FAST32_MAX INT_MAX
    314  1.36  christos #  define INT_FAST32_MIN INT_MIN
    315  1.29  christos # endif
    316  1.29  christos #endif
    317  1.29  christos 
    318  1.29  christos #ifndef INTMAX_MAX
    319  1.36  christos # ifdef LLONG_MAX
    320  1.29  christos typedef long long intmax_t;
    321  1.52  christos #  if HAVE_STRTOLL
    322  1.52  christos #   define strtoimax strtoll
    323  1.52  christos #  endif
    324  1.36  christos #  define INTMAX_MAX LLONG_MAX
    325  1.36  christos #  define INTMAX_MIN LLONG_MIN
    326  1.29  christos # else
    327  1.29  christos typedef long intmax_t;
    328  1.30  christos #  define INTMAX_MAX LONG_MAX
    329  1.30  christos #  define INTMAX_MIN LONG_MIN
    330  1.29  christos # endif
    331  1.52  christos # ifndef strtoimax
    332  1.52  christos #  define strtoimax strtol
    333  1.52  christos # endif
    334  1.29  christos #endif
    335  1.29  christos 
    336  1.36  christos #ifndef PRIdMAX
    337  1.36  christos # if INTMAX_MAX == LLONG_MAX
    338  1.36  christos #  define PRIdMAX "lld"
    339  1.36  christos # else
    340  1.36  christos #  define PRIdMAX "ld"
    341  1.36  christos # endif
    342  1.36  christos #endif
    343  1.36  christos 
    344  1.57  christos #ifndef UINT_FAST32_MAX
    345  1.57  christos typedef unsigned long uint_fast32_t;
    346  1.57  christos #endif
    347  1.57  christos 
    348  1.43  christos #ifndef UINT_FAST64_MAX
    349  1.43  christos # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
    350  1.43  christos typedef unsigned long long uint_fast64_t;
    351  1.43  christos # else
    352  1.43  christos #  if ULONG_MAX >> 31 >> 1 < 0xffffffff
    353  1.43  christos Please use a compiler that supports a 64-bit integer type (or wider);
    354  1.43  christos you may need to compile with "-DHAVE_STDINT_H".
    355  1.43  christos #  endif
    356  1.43  christos typedef unsigned long	uint_fast64_t;
    357  1.43  christos # endif
    358  1.43  christos #endif
    359  1.43  christos 
    360  1.29  christos #ifndef UINTMAX_MAX
    361  1.29  christos # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
    362  1.29  christos typedef unsigned long long uintmax_t;
    363  1.36  christos # else
    364  1.36  christos typedef unsigned long uintmax_t;
    365  1.36  christos # endif
    366  1.36  christos #endif
    367  1.36  christos 
    368  1.36  christos #ifndef PRIuMAX
    369  1.36  christos # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
    370  1.29  christos #  define PRIuMAX "llu"
    371  1.29  christos # else
    372  1.29  christos #  define PRIuMAX "lu"
    373  1.29  christos # endif
    374  1.29  christos #endif
    375  1.29  christos 
    376  1.25   mlelstv #ifndef INT32_MAX
    377  1.25   mlelstv #define INT32_MAX 0x7fffffff
    378  1.25   mlelstv #endif /* !defined INT32_MAX */
    379  1.25   mlelstv #ifndef INT32_MIN
    380  1.25   mlelstv #define INT32_MIN (-1 - INT32_MAX)
    381  1.25   mlelstv #endif /* !defined INT32_MIN */
    382   1.1       jtc 
    383  1.33  christos #ifndef SIZE_MAX
    384  1.33  christos #define SIZE_MAX ((size_t) -1)
    385  1.33  christos #endif
    386  1.33  christos 
    387  1.52  christos #if 3 <= __GNUC__
    388  1.57  christos # define ATTRIBUTE_CONST __attribute__((__const__))
    389  1.57  christos # define ATTRIBUTE_MALLOC __attribute__((__malloc__))
    390  1.57  christos # define ATTRIBUTE_PURE __attribute__((__pure__))
    391  1.57  christos # define ATTRIBUTE_FORMAT(spec) __attribute__((__format__ spec))
    392  1.27  christos #else
    393  1.29  christos # define ATTRIBUTE_CONST /* empty */
    394  1.52  christos # define ATTRIBUTE_MALLOC /* empty */
    395  1.29  christos # define ATTRIBUTE_PURE /* empty */
    396  1.30  christos # define ATTRIBUTE_FORMAT(spec) /* empty */
    397  1.27  christos #endif
    398  1.29  christos 
    399  1.29  christos #if !defined _Noreturn && __STDC_VERSION__ < 201112
    400  1.29  christos # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
    401  1.57  christos #  define _Noreturn __attribute__((__noreturn__))
    402  1.29  christos # else
    403  1.29  christos #  define _Noreturn
    404  1.29  christos # endif
    405  1.29  christos #endif
    406  1.29  christos 
    407  1.29  christos #if __STDC_VERSION__ < 199901 && !defined restrict
    408  1.29  christos # define restrict /* empty */
    409  1.27  christos #endif
    410  1.27  christos 
    411   1.1       jtc /*
    412  1.25   mlelstv ** Workarounds for compilers/systems.
    413   1.1       jtc */
    414   1.1       jtc 
    415  1.48  christos #ifndef EPOCH_LOCAL
    416  1.48  christos # define EPOCH_LOCAL 0
    417  1.48  christos #endif
    418  1.48  christos #ifndef EPOCH_OFFSET
    419  1.48  christos # define EPOCH_OFFSET 0
    420  1.48  christos #endif
    421  1.52  christos #ifndef RESERVE_STD_EXT_IDS
    422  1.52  christos # define RESERVE_STD_EXT_IDS 0
    423  1.52  christos #endif
    424  1.52  christos 
    425  1.52  christos /* If standard C identifiers with external linkage (e.g., localtime)
    426  1.52  christos    are reserved and are not already being renamed anyway, rename them
    427  1.52  christos    as if compiling with '-Dtime_tz=time_t'.  */
    428  1.52  christos #if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
    429  1.52  christos # define time_tz time_t
    430  1.52  christos #endif
    431  1.48  christos 
    432   1.1       jtc /*
    433  1.29  christos ** Compile with -Dtime_tz=T to build the tz package with a private
    434  1.29  christos ** time_t type equivalent to T rather than the system-supplied time_t.
    435  1.29  christos ** This debugging feature can test unusual design decisions
    436  1.29  christos ** (e.g., time_t wider than 'long', or unsigned time_t) even on
    437  1.29  christos ** typical platforms.
    438  1.29  christos */
    439  1.48  christos #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
    440  1.51  christos # define TZ_TIME_T 1
    441  1.51  christos #else
    442  1.51  christos # define TZ_TIME_T 0
    443  1.51  christos #endif
    444  1.51  christos 
    445  1.57  christos #if defined LOCALTIME_IMPLEMENTATION && TZ_TIME_T
    446  1.57  christos static time_t sys_time(time_t *x) { return time(x); }
    447  1.57  christos #endif
    448  1.57  christos 
    449  1.51  christos #if TZ_TIME_T
    450  1.29  christos 
    451  1.39  christos typedef time_tz tz_time_t;
    452  1.39  christos 
    453  1.56  christos # undef  asctime
    454  1.56  christos # define asctime tz_asctime
    455  1.56  christos # undef  asctime_r
    456  1.56  christos # define asctime_r tz_asctime_r
    457  1.29  christos # undef  ctime
    458  1.29  christos # define ctime tz_ctime
    459  1.29  christos # undef  ctime_r
    460  1.29  christos # define ctime_r tz_ctime_r
    461  1.29  christos # undef  difftime
    462  1.29  christos # define difftime tz_difftime
    463  1.29  christos # undef  gmtime
    464  1.29  christos # define gmtime tz_gmtime
    465  1.29  christos # undef  gmtime_r
    466  1.29  christos # define gmtime_r tz_gmtime_r
    467  1.29  christos # undef  localtime
    468  1.29  christos # define localtime tz_localtime
    469  1.29  christos # undef  localtime_r
    470  1.29  christos # define localtime_r tz_localtime_r
    471  1.39  christos # undef  localtime_rz
    472  1.39  christos # define localtime_rz tz_localtime_rz
    473  1.29  christos # undef  mktime
    474  1.29  christos # define mktime tz_mktime
    475  1.39  christos # undef  mktime_z
    476  1.39  christos # define mktime_z tz_mktime_z
    477  1.36  christos # undef  offtime
    478  1.36  christos # define offtime tz_offtime
    479  1.36  christos # undef  posix2time
    480  1.36  christos # define posix2time tz_posix2time
    481  1.39  christos # undef  posix2time_z
    482  1.39  christos # define posix2time_z tz_posix2time_z
    483  1.51  christos # undef  strftime
    484  1.51  christos # define strftime tz_strftime
    485  1.29  christos # undef  time
    486  1.29  christos # define time tz_time
    487  1.36  christos # undef  time2posix
    488  1.36  christos # define time2posix tz_time2posix
    489  1.39  christos # undef  time2posix_z
    490  1.39  christos # define time2posix_z tz_time2posix_z
    491  1.29  christos # undef  time_t
    492  1.29  christos # define time_t tz_time_t
    493  1.36  christos # undef  timegm
    494  1.36  christos # define timegm tz_timegm
    495  1.36  christos # undef  timelocal
    496  1.36  christos # define timelocal tz_timelocal
    497  1.36  christos # undef  timeoff
    498  1.36  christos # define timeoff tz_timeoff
    499  1.39  christos # undef  tzalloc
    500  1.39  christos # define tzalloc tz_tzalloc
    501  1.39  christos # undef  tzfree
    502  1.39  christos # define tzfree tz_tzfree
    503  1.39  christos # undef  tzset
    504  1.39  christos # define tzset tz_tzset
    505  1.39  christos # undef  tzsetwall
    506  1.39  christos # define tzsetwall tz_tzsetwall
    507  1.51  christos # if HAVE_STRFTIME_L
    508  1.51  christos #  undef  strftime_l
    509  1.51  christos #  define strftime_l tz_strftime_l
    510  1.51  christos # endif
    511  1.51  christos # if HAVE_TZNAME
    512  1.51  christos #  undef  tzname
    513  1.51  christos #  define tzname tz_tzname
    514  1.51  christos # endif
    515  1.51  christos # if USG_COMPAT
    516  1.51  christos #  undef  daylight
    517  1.51  christos #  define daylight tz_daylight
    518  1.51  christos #  undef  timezone
    519  1.51  christos #  define timezone tz_timezone
    520  1.51  christos # endif
    521  1.56  christos # if ALTZONE
    522  1.51  christos #  undef  altzone
    523  1.51  christos #  define altzone tz_altzone
    524  1.51  christos # endif
    525  1.29  christos 
    526  1.56  christos char *asctime(struct tm const *);
    527  1.56  christos char *asctime_r(struct tm const *restrict, char *restrict);
    528  1.29  christos char *ctime(time_t const *);
    529  1.29  christos char *ctime_r(time_t const *, char *);
    530  1.51  christos double difftime(time_t, time_t) ATTRIBUTE_CONST;
    531  1.51  christos size_t strftime(char *restrict, size_t, char const *restrict,
    532  1.51  christos 		struct tm const *restrict);
    533  1.51  christos # if HAVE_STRFTIME_L
    534  1.51  christos size_t strftime_l(char *restrict, size_t, char const *restrict,
    535  1.51  christos 		  struct tm const *restrict, locale_t);
    536  1.51  christos # endif
    537  1.29  christos struct tm *gmtime(time_t const *);
    538  1.29  christos struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
    539  1.29  christos struct tm *localtime(time_t const *);
    540  1.29  christos struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
    541  1.29  christos time_t mktime(struct tm *);
    542  1.36  christos time_t time(time_t *);
    543  1.39  christos void tzset(void);
    544  1.36  christos #endif
    545  1.29  christos 
    546  1.45  christos #if !HAVE_DECL_ASCTIME_R && !defined asctime_r
    547  1.45  christos extern char *asctime_r(struct tm const *restrict, char *restrict);
    548  1.36  christos #endif
    549  1.36  christos 
    550  1.51  christos #ifndef HAVE_DECL_ENVIRON
    551  1.51  christos # if defined environ || defined __USE_GNU
    552  1.51  christos #  define HAVE_DECL_ENVIRON 1
    553  1.51  christos # else
    554  1.51  christos #  define HAVE_DECL_ENVIRON 0
    555  1.51  christos # endif
    556  1.51  christos #endif
    557  1.51  christos 
    558  1.51  christos #if !HAVE_DECL_ENVIRON
    559  1.51  christos extern char **environ;
    560  1.51  christos #endif
    561  1.51  christos 
    562  1.56  christos #if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
    563  1.51  christos extern char *tzname[];
    564  1.56  christos #endif
    565  1.56  christos #if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
    566  1.44  christos extern long timezone;
    567  1.44  christos extern int daylight;
    568  1.44  christos #endif
    569  1.56  christos #if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
    570  1.44  christos extern long altzone;
    571  1.44  christos #endif
    572  1.44  christos 
    573  1.36  christos /*
    574  1.36  christos ** The STD_INSPIRED functions are similar, but most also need
    575  1.36  christos ** declarations if time_tz is defined.
    576  1.36  christos */
    577  1.36  christos 
    578  1.36  christos #ifdef STD_INSPIRED
    579  1.51  christos # if TZ_TIME_T || !defined tzsetwall
    580  1.36  christos void tzsetwall(void);
    581  1.36  christos # endif
    582  1.51  christos # if TZ_TIME_T || !defined offtime
    583  1.36  christos struct tm *offtime(time_t const *, long);
    584  1.36  christos # endif
    585  1.51  christos # if TZ_TIME_T || !defined timegm
    586  1.36  christos time_t timegm(struct tm *);
    587  1.36  christos # endif
    588  1.51  christos # if TZ_TIME_T || !defined timelocal
    589  1.36  christos time_t timelocal(struct tm *);
    590  1.36  christos # endif
    591  1.51  christos # if TZ_TIME_T || !defined timeoff
    592  1.36  christos time_t timeoff(struct tm *, long);
    593  1.36  christos # endif
    594  1.51  christos # if TZ_TIME_T || !defined time2posix
    595  1.36  christos time_t time2posix(time_t);
    596  1.36  christos # endif
    597  1.51  christos # if TZ_TIME_T || !defined posix2time
    598  1.36  christos time_t posix2time(time_t);
    599  1.36  christos # endif
    600  1.36  christos #endif
    601  1.36  christos 
    602  1.36  christos /* Infer TM_ZONE on systems where this information is known, but suppress
    603  1.36  christos    guessing if NO_TM_ZONE is defined.  Similarly for TM_GMTOFF.  */
    604  1.36  christos #if (defined __GLIBC__ \
    605  1.36  christos      || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
    606  1.36  christos      || (defined __APPLE__ && defined __MACH__))
    607  1.36  christos # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
    608  1.36  christos #  define TM_GMTOFF tm_gmtoff
    609  1.36  christos # endif
    610  1.36  christos # if !defined TM_ZONE && !defined NO_TM_ZONE
    611  1.36  christos #  define TM_ZONE tm_zone
    612  1.36  christos # endif
    613  1.36  christos #endif
    614  1.36  christos 
    615  1.36  christos /*
    616  1.36  christos ** Define functions that are ABI compatible with NetBSD but have
    617  1.36  christos ** better prototypes.  NetBSD 6.1.4 defines a pointer type timezone_t
    618  1.36  christos ** and labors under the misconception that 'const timezone_t' is a
    619  1.36  christos ** pointer to a constant.  This use of 'const' is ineffective, so it
    620  1.36  christos ** is not done here.  What we call 'struct state' NetBSD calls
    621  1.36  christos ** 'struct __state', but this is a private name so it doesn't matter.
    622  1.36  christos */
    623  1.37  christos #ifndef __NetBSD__
    624  1.36  christos #if NETBSD_INSPIRED
    625  1.36  christos typedef struct state *timezone_t;
    626  1.36  christos struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
    627  1.36  christos 			struct tm *restrict);
    628  1.36  christos time_t mktime_z(timezone_t restrict, struct tm *restrict);
    629  1.36  christos timezone_t tzalloc(char const *);
    630  1.36  christos void tzfree(timezone_t);
    631  1.36  christos # ifdef STD_INSPIRED
    632  1.51  christos #  if TZ_TIME_T || !defined posix2time_z
    633  1.36  christos time_t posix2time_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
    634  1.36  christos #  endif
    635  1.51  christos #  if TZ_TIME_T || !defined time2posix_z
    636  1.36  christos time_t time2posix_z(timezone_t __restrict, time_t) ATTRIBUTE_PURE;
    637  1.36  christos #  endif
    638  1.36  christos # endif
    639  1.29  christos #endif
    640  1.37  christos #endif
    641  1.29  christos 
    642  1.29  christos /*
    643   1.1       jtc ** Finally, some convenience items.
    644   1.1       jtc */
    645   1.1       jtc 
    646  1.51  christos #if HAVE_STDBOOL_H
    647  1.51  christos # include <stdbool.h>
    648  1.51  christos #else
    649  1.36  christos # define true 1
    650  1.36  christos # define false 0
    651  1.36  christos # define bool int
    652  1.36  christos #endif
    653   1.1       jtc 
    654  1.57  christos #define TYPE_BIT(type)	(sizeof(type) * CHAR_BIT)
    655  1.28  christos #define TYPE_SIGNED(type) (/*CONSTCOND*/((type) -1) < 0)
    656  1.41  christos #define TWOS_COMPLEMENT(t) (/*CONSTCOND*/(t) ~ (t) 0 < 0)
    657  1.41  christos 
    658  1.41  christos /* Max and min values of the integer type T, of which only the bottom
    659  1.41  christos    B bits are used, and where the highest-order used bit is considered
    660  1.41  christos    to be a sign bit if T is signed.  */
    661  1.41  christos #define MAXVAL(t, b) /*LINTED*/					\
    662  1.41  christos   ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))			\
    663  1.41  christos 	- 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
    664  1.41  christos #define MINVAL(t, b)						\
    665  1.41  christos   ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
    666  1.41  christos 
    667  1.51  christos /* The extreme time values, assuming no padding.  */
    668  1.51  christos #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
    669  1.51  christos #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
    670  1.51  christos 
    671  1.51  christos /* The extreme time values.  These are macros, not constants, so that
    672  1.57  christos    any portability problems occur only when compiling .c files that use
    673  1.51  christos    the macros, which is safer for applications that need only zdump and zic.
    674  1.51  christos    This implementation assumes no padding if time_t is signed and
    675  1.51  christos    either the compiler lacks support for _Generic or time_t is not one
    676  1.51  christos    of the standard signed integer types.  */
    677  1.51  christos #if HAVE_GENERIC
    678  1.51  christos # define TIME_T_MIN \
    679  1.51  christos     _Generic((time_t) 0, \
    680  1.51  christos 	     signed char: SCHAR_MIN, short: SHRT_MIN, \
    681  1.51  christos 	     int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
    682  1.51  christos 	     default: TIME_T_MIN_NO_PADDING)
    683  1.51  christos # define TIME_T_MAX \
    684  1.51  christos     (TYPE_SIGNED(time_t) \
    685  1.51  christos      ? _Generic((time_t) 0, \
    686  1.51  christos 		signed char: SCHAR_MAX, short: SHRT_MAX, \
    687  1.51  christos 		int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
    688  1.51  christos 		default: TIME_T_MAX_NO_PADDING)			    \
    689  1.51  christos      : (time_t) -1)
    690  1.48  christos #else
    691  1.51  christos # define TIME_T_MIN TIME_T_MIN_NO_PADDING
    692  1.51  christos # define TIME_T_MAX TIME_T_MAX_NO_PADDING
    693  1.48  christos #endif
    694  1.36  christos 
    695   1.1       jtc /*
    696   1.1       jtc ** 302 / 1000 is log10(2.0) rounded up.
    697   1.5       jtc ** Subtract one for the sign bit if the type is signed;
    698   1.5       jtc ** add one for integer division truncation;
    699   1.5       jtc ** add one more for a minus sign if the type is signed.
    700   1.1       jtc */
    701   1.1       jtc #define INT_STRLEN_MAXIMUM(type) \
    702  1.25   mlelstv 	((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
    703  1.25   mlelstv 	1 + TYPE_SIGNED(type))
    704   1.1       jtc 
    705   1.1       jtc /*
    706   1.1       jtc ** INITIALIZE(x)
    707   1.1       jtc */
    708   1.1       jtc 
    709  1.35  christos #if defined(__GNUC__) || defined(__lint__)
    710  1.34  christos # define INITIALIZE(x)	((x) = 0)
    711  1.34  christos #else
    712  1.34  christos # define INITIALIZE(x)
    713  1.34  christos #endif
    714   1.6       jtc 
    715  1.58  christos /* Whether memory access must strictly follow the C standard.
    716  1.58  christos    If 0, it's OK to read uninitialized storage so long as the value is
    717  1.58  christos    not relied upon.  Defining it to 0 lets mktime access parts of
    718  1.58  christos    struct tm that might be uninitialized, as a heuristic when the
    719  1.58  christos    standard doesn't say what to return and when tm_gmtoff can help
    720  1.58  christos    mktime likely infer a better value.  */
    721  1.36  christos #ifndef UNINIT_TRAP
    722  1.36  christos # define UNINIT_TRAP 0
    723  1.36  christos #endif
    724  1.36  christos 
    725  1.57  christos #ifdef DEBUG
    726  1.57  christos # define UNREACHABLE() abort()
    727  1.57  christos #elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
    728  1.57  christos # define UNREACHABLE() __builtin_unreachable()
    729  1.57  christos #elif defined __has_builtin
    730  1.57  christos # if __has_builtin(__builtin_unreachable)
    731  1.57  christos #  define UNREACHABLE() __builtin_unreachable()
    732  1.57  christos # endif
    733  1.57  christos #endif
    734  1.57  christos #ifndef UNREACHABLE
    735  1.57  christos # define UNREACHABLE() ((void) 0)
    736  1.57  christos #endif
    737  1.57  christos 
    738   1.6       jtc /*
    739   1.6       jtc ** For the benefit of GNU folk...
    740  1.34  christos ** '_(MSGID)' uses the current locale's message library string for MSGID.
    741   1.6       jtc ** The default is to use gettext if available, and use MSGID otherwise.
    742   1.6       jtc */
    743   1.6       jtc 
    744  1.25   mlelstv #if HAVE_GETTEXT
    745   1.6       jtc #define _(msgid) gettext(msgid)
    746  1.25   mlelstv #else /* !HAVE_GETTEXT */
    747   1.6       jtc #define _(msgid) msgid
    748  1.25   mlelstv #endif /* !HAVE_GETTEXT */
    749   1.6       jtc 
    750  1.34  christos #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
    751  1.34  christos # define TZ_DOMAIN "tz"
    752  1.34  christos #endif
    753  1.17    kleink 
    754  1.17    kleink #if HAVE_INCOMPATIBLE_CTIME_R
    755  1.17    kleink #undef asctime_r
    756  1.17    kleink #undef ctime_r
    757  1.25   mlelstv char *asctime_r(struct tm const *, char *);
    758  1.25   mlelstv char *ctime_r(time_t const *, char *);
    759  1.17    kleink #endif /* HAVE_INCOMPATIBLE_CTIME_R */
    760   1.1       jtc 
    761  1.50  christos /* Handy macros that are independent of tzfile implementation.  */
    762  1.50  christos 
    763  1.50  christos #define SECSPERMIN	60
    764  1.50  christos #define MINSPERHOUR	60
    765  1.50  christos #define HOURSPERDAY	24
    766  1.50  christos #define DAYSPERWEEK	7
    767  1.50  christos #define DAYSPERNYEAR	365
    768  1.50  christos #define DAYSPERLYEAR	366
    769  1.50  christos #define SECSPERHOUR	(SECSPERMIN * MINSPERHOUR)
    770  1.50  christos #define SECSPERDAY	((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
    771  1.50  christos #define MONSPERYEAR	12
    772  1.50  christos 
    773  1.57  christos #define YEARSPERREPEAT		400	/* years before a Gregorian repeat */
    774  1.57  christos #define DAYSPERREPEAT		((int_fast32_t) 400 * 365 + 100 - 4 + 1)
    775  1.57  christos #define SECSPERREPEAT		((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
    776  1.57  christos #define AVGSECSPERYEAR		(SECSPERREPEAT / YEARSPERREPEAT)
    777  1.57  christos 
    778  1.50  christos #define TM_SUNDAY	0
    779  1.50  christos #define TM_MONDAY	1
    780  1.50  christos #define TM_TUESDAY	2
    781  1.50  christos #define TM_WEDNESDAY	3
    782  1.50  christos #define TM_THURSDAY	4
    783  1.50  christos #define TM_FRIDAY	5
    784  1.50  christos #define TM_SATURDAY	6
    785  1.50  christos 
    786  1.50  christos #define TM_JANUARY	0
    787  1.50  christos #define TM_FEBRUARY	1
    788  1.50  christos #define TM_MARCH	2
    789  1.50  christos #define TM_APRIL	3
    790  1.50  christos #define TM_MAY		4
    791  1.50  christos #define TM_JUNE		5
    792  1.50  christos #define TM_JULY		6
    793  1.50  christos #define TM_AUGUST	7
    794  1.50  christos #define TM_SEPTEMBER	8
    795  1.50  christos #define TM_OCTOBER	9
    796  1.50  christos #define TM_NOVEMBER	10
    797  1.50  christos #define TM_DECEMBER	11
    798  1.50  christos 
    799  1.50  christos #define TM_YEAR_BASE	1900
    800  1.57  christos #define TM_WDAY_BASE	TM_MONDAY
    801  1.50  christos 
    802  1.50  christos #define EPOCH_YEAR	1970
    803  1.50  christos #define EPOCH_WDAY	TM_THURSDAY
    804  1.50  christos 
    805  1.50  christos #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
    806  1.50  christos 
    807  1.50  christos /*
    808  1.50  christos ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
    809  1.50  christos **	isleap(y) == isleap(y % 400)
    810  1.50  christos ** and so
    811  1.50  christos **	isleap(a + b) == isleap((a + b) % 400)
    812  1.50  christos ** or
    813  1.50  christos **	isleap(a + b) == isleap(a % 400 + b % 400)
    814  1.50  christos ** This is true even if % means modulo rather than Fortran remainder
    815  1.51  christos ** (which is allowed by C89 but not by C99 or later).
    816  1.50  christos ** We use this to avoid addition overflow problems.
    817  1.50  christos */
    818  1.50  christos 
    819  1.50  christos #define isleap_sum(a, b)	isleap((a) % 400 + (b) % 400)
    820  1.50  christos 
    821  1.55  christos #ifdef _LIBC
    822  1.55  christos #include "reentrant.h"
    823  1.54  christos extern struct __state *__lclptr;
    824  1.54  christos #if defined(__LIBC12_SOURCE__)
    825  1.54  christos #define tzset_unlocked __tzset_unlocked
    826  1.54  christos #else
    827  1.54  christos #define tzset_unlocked __tzset_unlocked50
    828  1.54  christos #endif
    829  1.54  christos 
    830  1.54  christos void tzset_unlocked(void);
    831  1.54  christos #ifdef _REENTRANT
    832  1.54  christos extern rwlock_t __lcl_lock;
    833  1.54  christos #endif
    834  1.55  christos #endif
    835  1.54  christos 
    836   1.1       jtc #endif /* !defined PRIVATE_H */
    837