blob: 838ab2b2df8f927a36d2a35d15dc88e6c4600fbd [file] [log] [blame]
Almaz Mingaleev5411aff2022-02-11 12:27:12 +00001/* Private header for tzdb code. */
2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003#ifndef PRIVATE_H
4
5#define PRIVATE_H
6
7/*
8** This file is in the public domain, so clarified as of
9** 1996-06-05 by Arthur David Olson.
10*/
11
12/*
13** This header is for use ONLY with the time conversion code.
14** There is no guarantee that it will remain unchanged,
15** or that it will remain at all.
16** Do NOT copy it to any system include directory.
17** Thank you!
18*/
19
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +010020/* PORT_TO_C89 means the code should work even if the underlying
21 compiler and library support only C89. SUPPORT_C89 means the
22 tzcode library should support C89 callers in addition to the usual
23 support for C99-and-later callers. These macros are obsolescent,
24 and the plan is to remove them along with any code needed only when
25 they are nonzero. */
26#ifndef PORT_TO_C89
27# define PORT_TO_C89 0
28#endif
29#ifndef SUPPORT_C89
30# define SUPPORT_C89 0
31#endif
32
33#ifndef __STDC_VERSION__
34# define __STDC_VERSION__ 0
35#endif
36
37/* Define true, false and bool if they don't work out of the box. */
38#if PORT_TO_C89 && __STDC_VERSION__ < 199901
39# define true 1
40# define false 0
41# define bool int
42#elif __STDC_VERSION__ < 202311
43# include <stdbool.h>
44#endif
45
46#if __STDC_VERSION__ < 202311
47# define static_assert(cond) extern int static_assert_check[(cond) ? 1 : -1]
48#endif
49
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000050/*
51** zdump has been made independent of the rest of the time
52** conversion package to increase confidence in the verification it provides.
53** You can use zdump to help in verifying other implementations.
54** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
55*/
56#ifndef USE_LTZ
57# define USE_LTZ 1
58#endif
59
60/* This string was in the Factory zone through version 2016f. */
Elliott Hughesce4783c2013-07-12 17:31:11 -070061#define GRANDPARENTED "Local time zone must be set--see zic manual page"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062
63/*
64** Defaults for preprocessor symbols.
Elliott Hughes9fb22a32015-10-07 17:13:40 -070065** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066*/
67
Elliott Hughes0a610d02016-07-29 14:04:17 -070068#ifndef HAVE_DECL_ASCTIME_R
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +010069# define HAVE_DECL_ASCTIME_R 1
Elliott Hughes0a610d02016-07-29 14:04:17 -070070#endif
71
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +010072#if !defined HAVE__GENERIC && defined __has_extension
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000073# if __has_extension(c_generic_selections)
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +010074# define HAVE__GENERIC 1
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000075# else
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +010076# define HAVE__GENERIC 0
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000077# endif
78#endif
79/* _Generic is buggy in pre-4.9 GCC. */
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +010080#if !defined HAVE__GENERIC && defined __GNUC__ && !defined __STRICT_ANSI__
81# define HAVE__GENERIC (4 < __GNUC__ + (9 <= __GNUC_MINOR__))
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000082#endif
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +010083#ifndef HAVE__GENERIC
84# define HAVE__GENERIC (201112 <= __STDC_VERSION__)
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000085#endif
86
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +010087#if !defined HAVE_GETTEXT && defined __has_include
88# if __has_include(<libintl.h>)
89# define HAVE_GETTEXT true
90# endif
91#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092#ifndef HAVE_GETTEXT
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +010093# define HAVE_GETTEXT false
94#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
96#ifndef HAVE_INCOMPATIBLE_CTIME_R
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +010097# define HAVE_INCOMPATIBLE_CTIME_R 0
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000098#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099
Calin Juravle627d37c2014-02-28 11:46:03 +0000100#ifndef HAVE_LINK
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100101# define HAVE_LINK 1
Calin Juravle627d37c2014-02-28 11:46:03 +0000102#endif /* !defined HAVE_LINK */
103
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000104#ifndef HAVE_MALLOC_ERRNO
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100105# define HAVE_MALLOC_ERRNO 1
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000106#endif
107
Elliott Hughes0a610d02016-07-29 14:04:17 -0700108#ifndef HAVE_POSIX_DECLS
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100109# define HAVE_POSIX_DECLS 1
Elliott Hughes0a610d02016-07-29 14:04:17 -0700110#endif
111
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100112#ifndef HAVE_SETENV
113# define HAVE_SETENV 1
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000114#endif
115
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700116#ifndef HAVE_STRDUP
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100117# define HAVE_STRDUP 1
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000118#endif
119
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120#ifndef HAVE_SYMLINK
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100121# define HAVE_SYMLINK 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122#endif /* !defined HAVE_SYMLINK */
123
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100124#if !defined HAVE_SYS_STAT_H && defined __has_include
125# if !__has_include(<sys/stat.h>)
126# define HAVE_SYS_STAT_H false
127# endif
128#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129#ifndef HAVE_SYS_STAT_H
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100130# define HAVE_SYS_STAT_H true
131#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100133#if !defined HAVE_UNISTD_H && defined __has_include
134# if !__has_include(<unistd.h>)
135# define HAVE_UNISTD_H false
136# endif
137#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138#ifndef HAVE_UNISTD_H
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100139# define HAVE_UNISTD_H true
140#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700142#ifndef NETBSD_INSPIRED
143# define NETBSD_INSPIRED 1
144#endif
Elliott Hughesce4783c2013-07-12 17:31:11 -0700145
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146#if HAVE_INCOMPATIBLE_CTIME_R
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100147# define asctime_r _incompatible_asctime_r
148# define ctime_r _incompatible_ctime_r
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149#endif /* HAVE_INCOMPATIBLE_CTIME_R */
150
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000151/* Enable tm_gmtoff, tm_zone, and environ on GNUish systems. */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700152#define _GNU_SOURCE 1
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000153/* Fix asctime_r on Solaris 11. */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700154#define _POSIX_PTHREAD_SEMANTICS 1
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000155/* Enable strtoimax on pre-C99 Solaris 11. */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700156#define __EXTENSIONS__ 1
157
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100158/* On GNUish systems where time_t might be 32 or 64 bits, use 64.
159 On these platforms _FILE_OFFSET_BITS must also be 64; otherwise
160 setting _TIME_BITS to 64 does not work. The code does not
161 otherwise rely on _FILE_OFFSET_BITS being 64, since it does not
162 use off_t or functions like 'stat' that depend on off_t. */
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000163#ifndef _FILE_OFFSET_BITS
164# define _FILE_OFFSET_BITS 64
165#endif
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100166#if !defined _TIME_BITS && _FILE_OFFSET_BITS == 64
167# define _TIME_BITS 64
168#endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000169
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170/*
171** Nested includes
172*/
173
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000174/* Avoid clashes with NetBSD by renaming NetBSD's declarations.
175 If defining the 'timezone' variable, avoid a clash with FreeBSD's
176 'timezone' function by renaming its declaration. */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700177#define localtime_rz sys_localtime_rz
178#define mktime_z sys_mktime_z
179#define posix2time_z sys_posix2time_z
180#define time2posix_z sys_time2posix_z
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000181#if defined USG_COMPAT && USG_COMPAT == 2
182# define timezone sys_timezone
183#endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700184#define timezone_t sys_timezone_t
185#define tzalloc sys_tzalloc
186#define tzfree sys_tzfree
187#include <time.h>
188#undef localtime_rz
189#undef mktime_z
190#undef posix2time_z
191#undef time2posix_z
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000192#if defined USG_COMPAT && USG_COMPAT == 2
193# undef timezone
194#endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700195#undef timezone_t
196#undef tzalloc
197#undef tzfree
198
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100199#include <stddef.h>
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000200#include <string.h>
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100201#if !PORT_TO_C89
202# include <inttypes.h>
203#endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000204#include <limits.h> /* for CHAR_BIT et al. */
205#include <stdlib.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000207#include <errno.h>
208
209#ifndef EINVAL
210# define EINVAL ERANGE
211#endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700212
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100213#ifndef ELOOP
214# define ELOOP EINVAL
215#endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700216#ifndef ENAMETOOLONG
217# define ENAMETOOLONG EINVAL
218#endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000219#ifndef ENOMEM
220# define ENOMEM EINVAL
221#endif
Elliott Hughes0a610d02016-07-29 14:04:17 -0700222#ifndef ENOTSUP
223# define ENOTSUP EINVAL
224#endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700225#ifndef EOVERFLOW
226# define EOVERFLOW EINVAL
227#endif
228
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229#if HAVE_GETTEXT
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100230# include <libintl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231#endif /* HAVE_GETTEXT */
232
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233#if HAVE_UNISTD_H
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100234# include <unistd.h> /* for R_OK, and other POSIX goodness */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800235#endif /* HAVE_UNISTD_H */
236
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700237#ifndef HAVE_STRFTIME_L
238# if _POSIX_VERSION < 200809
239# define HAVE_STRFTIME_L 0
240# else
241# define HAVE_STRFTIME_L 1
242# endif
243#endif
244
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000245#ifndef USG_COMPAT
246# ifndef _XOPEN_VERSION
247# define USG_COMPAT 0
248# else
249# define USG_COMPAT 1
250# endif
251#endif
252
253#ifndef HAVE_TZNAME
254# if _POSIX_VERSION < 198808 && !USG_COMPAT
255# define HAVE_TZNAME 0
256# else
257# define HAVE_TZNAME 1
258# endif
259#endif
260
261#ifndef ALTZONE
262# if defined __sun || defined _M_XENIX
263# define ALTZONE 1
264# else
265# define ALTZONE 0
266# endif
267#endif
268
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269#ifndef R_OK
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100270# define R_OK 4
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271#endif /* !defined R_OK */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800272
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100273#if PORT_TO_C89
274
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800275/*
276** Define HAVE_STDINT_H's default value here, rather than at the
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000277** start, since __GLIBC__ and INTMAX_MAX's values depend on
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100278** previously included files. glibc 2.1 and Solaris 10 and later have
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000279** stdint.h, even with pre-C99 compilers.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800280*/
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100281#if !defined HAVE_STDINT_H && defined __has_include
282# define HAVE_STDINT_H true /* C23 __has_include implies C99 stdint.h. */
283#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800284#ifndef HAVE_STDINT_H
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100285# define HAVE_STDINT_H \
Elliott Hughes5f564542014-06-19 13:54:10 -0700286 (199901 <= __STDC_VERSION__ \
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100287 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000288 || __CYGWIN__ || INTMAX_MAX)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800289#endif /* !defined HAVE_STDINT_H */
290
291#if HAVE_STDINT_H
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100292# include <stdint.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800293#endif /* !HAVE_STDINT_H */
294
Elliott Hughesce4783c2013-07-12 17:31:11 -0700295#ifndef HAVE_INTTYPES_H
296# define HAVE_INTTYPES_H HAVE_STDINT_H
297#endif
298#if HAVE_INTTYPES_H
299# include <inttypes.h>
300#endif
301
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800302/* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100303#if defined __LONG_LONG_MAX__ && !defined __STRICT_ANSI__
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700304# ifndef LLONG_MAX
305# define LLONG_MAX __LONG_LONG_MAX__
306# endif
307# ifndef LLONG_MIN
308# define LLONG_MIN (-1 - LLONG_MAX)
309# endif
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100310# ifndef ULLONG_MAX
311# define ULLONG_MAX (LLONG_MAX * 2ull + 1)
312# endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700313#endif
314
315#ifndef INT_FAST64_MAX
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100316# if 1 <= LONG_MAX >> 31 >> 31
317typedef long int_fast64_t;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700318# define INT_FAST64_MIN LONG_MIN
319# define INT_FAST64_MAX LONG_MAX
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100320# else
321/* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
322typedef long long int_fast64_t;
323# define INT_FAST64_MIN LLONG_MIN
324# define INT_FAST64_MAX LLONG_MAX
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700325# endif
326#endif
327
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000328#ifndef PRIdFAST64
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100329# if INT_FAST64_MAX == LONG_MAX
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000330# define PRIdFAST64 "ld"
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100331# else
332# define PRIdFAST64 "lld"
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700333# endif
334#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800335
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000336#ifndef SCNdFAST64
337# define SCNdFAST64 PRIdFAST64
338#endif
339
Elliott Hughesce4783c2013-07-12 17:31:11 -0700340#ifndef INT_FAST32_MAX
341# if INT_MAX >> 31 == 0
342typedef long int_fast32_t;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700343# define INT_FAST32_MAX LONG_MAX
344# define INT_FAST32_MIN LONG_MIN
Elliott Hughesce4783c2013-07-12 17:31:11 -0700345# else
346typedef int int_fast32_t;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700347# define INT_FAST32_MAX INT_MAX
348# define INT_FAST32_MIN INT_MIN
Elliott Hughesce4783c2013-07-12 17:31:11 -0700349# endif
350#endif
351
352#ifndef INTMAX_MAX
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700353# ifdef LLONG_MAX
Elliott Hughesce4783c2013-07-12 17:31:11 -0700354typedef long long intmax_t;
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100355# ifndef HAVE_STRTOLL
356# define HAVE_STRTOLL true
357# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000358# if HAVE_STRTOLL
359# define strtoimax strtoll
360# endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700361# define INTMAX_MAX LLONG_MAX
362# define INTMAX_MIN LLONG_MIN
Elliott Hughesce4783c2013-07-12 17:31:11 -0700363# else
364typedef long intmax_t;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700365# define INTMAX_MAX LONG_MAX
366# define INTMAX_MIN LONG_MIN
Elliott Hughesce4783c2013-07-12 17:31:11 -0700367# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000368# ifndef strtoimax
369# define strtoimax strtol
370# endif
Elliott Hughesce4783c2013-07-12 17:31:11 -0700371#endif
372
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700373#ifndef PRIdMAX
374# if INTMAX_MAX == LLONG_MAX
375# define PRIdMAX "lld"
376# else
377# define PRIdMAX "ld"
378# endif
379#endif
380
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100381#ifndef PTRDIFF_MAX
382# define PTRDIFF_MAX MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t))
383#endif
384
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000385#ifndef UINT_FAST32_MAX
386typedef unsigned long uint_fast32_t;
387#endif
388
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700389#ifndef UINT_FAST64_MAX
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100390# if 3 <= ULONG_MAX >> 31 >> 31
391typedef unsigned long uint_fast64_t;
392# define UINT_FAST64_MAX ULONG_MAX
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700393# else
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100394/* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
395typedef unsigned long long uint_fast64_t;
396# define UINT_FAST64_MAX ULLONG_MAX
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700397# endif
398#endif
399
Elliott Hughesce4783c2013-07-12 17:31:11 -0700400#ifndef UINTMAX_MAX
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100401# ifdef ULLONG_MAX
Elliott Hughesce4783c2013-07-12 17:31:11 -0700402typedef unsigned long long uintmax_t;
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100403# define UINTMAX_MAX ULLONG_MAX
Elliott Hughesce4783c2013-07-12 17:31:11 -0700404# else
405typedef unsigned long uintmax_t;
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100406# define UINTMAX_MAX ULONG_MAX
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700407# endif
408#endif
409
410#ifndef PRIuMAX
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100411# ifdef ULLONG_MAX
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700412# define PRIuMAX "llu"
413# else
Elliott Hughesce4783c2013-07-12 17:31:11 -0700414# define PRIuMAX "lu"
415# endif
416#endif
417
Elliott Hughes5f564542014-06-19 13:54:10 -0700418#ifndef SIZE_MAX
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100419# define SIZE_MAX ((size_t) -1)
420#endif
421
422#endif /* PORT_TO_C89 */
423
424/* The maximum size of any created object, as a signed integer.
425 Although the C standard does not outright prohibit larger objects,
426 behavior is undefined if the result of pointer subtraction does not
427 fit into ptrdiff_t, and the code assumes in several places that
428 pointer subtraction works. As a practical matter it's OK to not
429 support objects larger than this. */
430#define INDEX_MAX ((ptrdiff_t) min(PTRDIFF_MAX, SIZE_MAX))
431
432/* Support ckd_add, ckd_sub, ckd_mul on C23 or recent-enough GCC-like
433 hosts, unless compiled with -DHAVE_STDCKDINT_H=0 or with pre-C23 EDG. */
434#if !defined HAVE_STDCKDINT_H && defined __has_include
435# if __has_include(<stdckdint.h>)
436# define HAVE_STDCKDINT_H true
437# endif
438#endif
439#ifdef HAVE_STDCKDINT_H
440# if HAVE_STDCKDINT_H
441# include <stdckdint.h>
442# endif
443#elif defined __EDG__
444/* Do nothing, to work around EDG bug <https://bugs.gnu.org/53256>. */
445#elif defined __has_builtin
446# if __has_builtin(__builtin_add_overflow)
447# define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
448# endif
449# if __has_builtin(__builtin_sub_overflow)
450# define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
451# endif
452# if __has_builtin(__builtin_mul_overflow)
453# define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
454# endif
455#elif 7 <= __GNUC__
456# define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
457# define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
458# define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
Elliott Hughes5f564542014-06-19 13:54:10 -0700459#endif
460
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000461#if 3 <= __GNUC__
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100462# define ATTRIBUTE_MALLOC __attribute__((malloc))
463# define ATTRIBUTE_FORMAT(spec) __attribute__((format spec))
Elliott Hughesce4783c2013-07-12 17:31:11 -0700464#else
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000465# define ATTRIBUTE_MALLOC /* empty */
Elliott Hughese0d0b152013-09-27 00:04:30 -0700466# define ATTRIBUTE_FORMAT(spec) /* empty */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700467#endif
468
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100469#if (defined __has_c_attribute \
470 && (202311 <= __STDC_VERSION__ || !defined __STRICT_ANSI__))
471# define HAVE___HAS_C_ATTRIBUTE true
472#else
473# define HAVE___HAS_C_ATTRIBUTE false
474#endif
475
476#if HAVE___HAS_C_ATTRIBUTE
477# if __has_c_attribute(deprecated)
478# define ATTRIBUTE_DEPRECATED [[deprecated]]
479# endif
480#endif
481#ifndef ATTRIBUTE_DEPRECATED
482# if 3 < __GNUC__ + (2 <= __GNUC_MINOR__)
483# define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
Elliott Hughesce4783c2013-07-12 17:31:11 -0700484# else
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100485# define ATTRIBUTE_DEPRECATED /* empty */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700486# endif
487#endif
488
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100489#if HAVE___HAS_C_ATTRIBUTE
490# if __has_c_attribute(fallthrough)
491# define ATTRIBUTE_FALLTHROUGH [[fallthrough]]
492# endif
493#endif
494#ifndef ATTRIBUTE_FALLTHROUGH
495# if 7 <= __GNUC__
496# define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
497# else
498# define ATTRIBUTE_FALLTHROUGH ((void) 0)
499# endif
500#endif
501
502#if HAVE___HAS_C_ATTRIBUTE
503# if __has_c_attribute(maybe_unused)
504# define ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]]
505# endif
506#endif
507#ifndef ATTRIBUTE_MAYBE_UNUSED
508# if 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
509# define ATTRIBUTE_MAYBE_UNUSED __attribute__((unused))
510# else
511# define ATTRIBUTE_MAYBE_UNUSED /* empty */
512# endif
513#endif
514
515#if HAVE___HAS_C_ATTRIBUTE
516# if __has_c_attribute(noreturn)
517# define ATTRIBUTE_NORETURN [[noreturn]]
518# endif
519#endif
520#ifndef ATTRIBUTE_NORETURN
521# if 201112 <= __STDC_VERSION__
522# define ATTRIBUTE_NORETURN _Noreturn
523# elif 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
524# define ATTRIBUTE_NORETURN __attribute__((noreturn))
525# else
526# define ATTRIBUTE_NORETURN /* empty */
527# endif
528#endif
529
530#if HAVE___HAS_C_ATTRIBUTE
531# if __has_c_attribute(reproducible)
532# define ATTRIBUTE_REPRODUCIBLE [[reproducible]]
533# endif
534#endif
535#ifndef ATTRIBUTE_REPRODUCIBLE
536# if 3 <= __GNUC__
537# define ATTRIBUTE_REPRODUCIBLE __attribute__((pure))
538# else
539# define ATTRIBUTE_REPRODUCIBLE /* empty */
540# endif
541#endif
542
543#if HAVE___HAS_C_ATTRIBUTE
544# if __has_c_attribute(unsequenced)
545# define ATTRIBUTE_UNSEQUENCED [[unsequenced]]
546# endif
547#endif
548#ifndef ATTRIBUTE_UNSEQUENCED
549# if 3 <= __GNUC__
550# define ATTRIBUTE_UNSEQUENCED __attribute__((const))
551# else
552# define ATTRIBUTE_UNSEQUENCED /* empty */
553# endif
554#endif
555
556#if (__STDC_VERSION__ < 199901 && !defined restrict \
557 && (PORT_TO_C89 || defined _MSC_VER))
Elliott Hughesce4783c2013-07-12 17:31:11 -0700558# define restrict /* empty */
559#endif
560
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800561/*
562** Workarounds for compilers/systems.
563*/
564
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000565#ifndef EPOCH_LOCAL
566# define EPOCH_LOCAL 0
567#endif
568#ifndef EPOCH_OFFSET
569# define EPOCH_OFFSET 0
570#endif
571#ifndef RESERVE_STD_EXT_IDS
572# define RESERVE_STD_EXT_IDS 0
573#endif
574
575/* If standard C identifiers with external linkage (e.g., localtime)
576 are reserved and are not already being renamed anyway, rename them
577 as if compiling with '-Dtime_tz=time_t'. */
578#if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
579# define time_tz time_t
580#endif
581
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800582/*
Elliott Hughesce4783c2013-07-12 17:31:11 -0700583** Compile with -Dtime_tz=T to build the tz package with a private
584** time_t type equivalent to T rather than the system-supplied time_t.
585** This debugging feature can test unusual design decisions
586** (e.g., time_t wider than 'long', or unsigned time_t) even on
587** typical platforms.
588*/
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000589#if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
590# define TZ_TIME_T 1
591#else
592# define TZ_TIME_T 0
593#endif
594
595#if defined LOCALTIME_IMPLEMENTATION && TZ_TIME_T
Elliott Hughesce4783c2013-07-12 17:31:11 -0700596static time_t sys_time(time_t *x) { return time(x); }
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000597#endif
598
599#if TZ_TIME_T
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700600
601typedef time_tz tz_time_t;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700602
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000603# undef asctime
604# define asctime tz_asctime
605# undef asctime_r
606# define asctime_r tz_asctime_r
Elliott Hughesce4783c2013-07-12 17:31:11 -0700607# undef ctime
608# define ctime tz_ctime
609# undef ctime_r
610# define ctime_r tz_ctime_r
611# undef difftime
612# define difftime tz_difftime
613# undef gmtime
614# define gmtime tz_gmtime
615# undef gmtime_r
616# define gmtime_r tz_gmtime_r
617# undef localtime
618# define localtime tz_localtime
619# undef localtime_r
620# define localtime_r tz_localtime_r
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700621# undef localtime_rz
622# define localtime_rz tz_localtime_rz
Elliott Hughesce4783c2013-07-12 17:31:11 -0700623# undef mktime
624# define mktime tz_mktime
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700625# undef mktime_z
626# define mktime_z tz_mktime_z
627# undef offtime
628# define offtime tz_offtime
629# undef posix2time
630# define posix2time tz_posix2time
631# undef posix2time_z
632# define posix2time_z tz_posix2time_z
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000633# undef strftime
634# define strftime tz_strftime
Elliott Hughesce4783c2013-07-12 17:31:11 -0700635# undef time
636# define time tz_time
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700637# undef time2posix
638# define time2posix tz_time2posix
639# undef time2posix_z
640# define time2posix_z tz_time2posix_z
Elliott Hughesce4783c2013-07-12 17:31:11 -0700641# undef time_t
642# define time_t tz_time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700643# undef timegm
644# define timegm tz_timegm
645# undef timelocal
646# define timelocal tz_timelocal
647# undef timeoff
648# define timeoff tz_timeoff
649# undef tzalloc
650# define tzalloc tz_tzalloc
651# undef tzfree
652# define tzfree tz_tzfree
653# undef tzset
654# define tzset tz_tzset
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000655# if HAVE_STRFTIME_L
656# undef strftime_l
657# define strftime_l tz_strftime_l
658# endif
659# if HAVE_TZNAME
660# undef tzname
661# define tzname tz_tzname
662# endif
663# if USG_COMPAT
664# undef daylight
665# define daylight tz_daylight
666# undef timezone
667# define timezone tz_timezone
668# endif
669# if ALTZONE
670# undef altzone
671# define altzone tz_altzone
672# endif
Elliott Hughesce4783c2013-07-12 17:31:11 -0700673
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100674# if __STDC_VERSION__ < 202311
675# define DEPRECATED_IN_C23 /* empty */
676# else
677# define DEPRECATED_IN_C23 ATTRIBUTE_DEPRECATED
678# endif
679DEPRECATED_IN_C23 char *asctime(struct tm const *);
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000680char *asctime_r(struct tm const *restrict, char *restrict);
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100681DEPRECATED_IN_C23 char *ctime(time_t const *);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700682char *ctime_r(time_t const *, char *);
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100683ATTRIBUTE_UNSEQUENCED double difftime(time_t, time_t);
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000684size_t strftime(char *restrict, size_t, char const *restrict,
685 struct tm const *restrict);
686# if HAVE_STRFTIME_L
687size_t strftime_l(char *restrict, size_t, char const *restrict,
688 struct tm const *restrict, locale_t);
689# endif
Elliott Hughesce4783c2013-07-12 17:31:11 -0700690struct tm *gmtime(time_t const *);
691struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
692struct tm *localtime(time_t const *);
693struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
694time_t mktime(struct tm *);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700695time_t time(time_t *);
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100696time_t timegm(struct tm *);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700697void tzset(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800698#endif
699
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100700#ifndef HAVE_DECL_TIMEGM
701# if (202311 <= __STDC_VERSION__ \
702 || defined __GLIBC__ || defined __tm_zone /* musl */ \
703 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
704 || (defined __APPLE__ && defined __MACH__))
705# define HAVE_DECL_TIMEGM true
706# else
707# define HAVE_DECL_TIMEGM false
708# endif
709#endif
710#if !HAVE_DECL_TIMEGM && !defined timegm
711time_t timegm(struct tm *);
712#endif
713
Elliott Hughes0a610d02016-07-29 14:04:17 -0700714#if !HAVE_DECL_ASCTIME_R && !defined asctime_r
715extern char *asctime_r(struct tm const *restrict, char *restrict);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700716#endif
717
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000718#ifndef HAVE_DECL_ENVIRON
719# if defined environ || defined __USE_GNU
720# define HAVE_DECL_ENVIRON 1
721# else
722# define HAVE_DECL_ENVIRON 0
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700723# endif
724#endif
Elliott Hughes0a610d02016-07-29 14:04:17 -0700725
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000726#if !HAVE_DECL_ENVIRON
727extern char **environ;
728#endif
729
730#if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
731extern char *tzname[];
732#endif
733#if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
734extern long timezone;
735extern int daylight;
736#endif
737#if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700738extern long altzone;
739#endif
740
741/*
742** The STD_INSPIRED functions are similar, but most also need
743** declarations if time_tz is defined.
744*/
745
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100746#ifndef STD_INSPIRED
747# define STD_INSPIRED 0
748#endif
749#if STD_INSPIRED
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000750# if TZ_TIME_T || !defined offtime
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700751struct tm *offtime(time_t const *, long);
752# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000753# if TZ_TIME_T || !defined timelocal
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700754time_t timelocal(struct tm *);
755# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000756# if TZ_TIME_T || !defined timeoff
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700757time_t timeoff(struct tm *, long);
758# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000759# if TZ_TIME_T || !defined time2posix
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700760time_t time2posix(time_t);
761# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000762# if TZ_TIME_T || !defined posix2time
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700763time_t posix2time(time_t);
764# endif
765#endif
766
767/* Infer TM_ZONE on systems where this information is known, but suppress
768 guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
769#if (defined __GLIBC__ \
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100770 || defined __tm_zone /* musl */ \
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700771 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
772 || (defined __APPLE__ && defined __MACH__))
773# if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
774# define TM_GMTOFF tm_gmtoff
775# endif
776# if !defined TM_ZONE && !defined NO_TM_ZONE
777# define TM_ZONE tm_zone
778# endif
779#endif
780
781/*
782** Define functions that are ABI compatible with NetBSD but have
783** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
784** and labors under the misconception that 'const timezone_t' is a
785** pointer to a constant. This use of 'const' is ineffective, so it
786** is not done here. What we call 'struct state' NetBSD calls
787** 'struct __state', but this is a private name so it doesn't matter.
788*/
789#if NETBSD_INSPIRED
790typedef struct state *timezone_t;
791struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
792 struct tm *restrict);
793time_t mktime_z(timezone_t restrict, struct tm *restrict);
794timezone_t tzalloc(char const *);
795void tzfree(timezone_t);
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100796# if STD_INSPIRED
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000797# if TZ_TIME_T || !defined posix2time_z
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100798ATTRIBUTE_REPRODUCIBLE time_t posix2time_z(timezone_t, time_t);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700799# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000800# if TZ_TIME_T || !defined time2posix_z
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100801ATTRIBUTE_REPRODUCIBLE time_t time2posix_z(timezone_t, time_t);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700802# endif
803# endif
804#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800805
806/*
807** Finally, some convenience items.
808*/
809
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100810#define TYPE_BIT(type) (CHAR_BIT * (ptrdiff_t) sizeof(type))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800811#define TYPE_SIGNED(type) (((type) -1) < 0)
Stephen Hines23360cc2015-09-29 09:28:41 -0700812#define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
813
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100814/* Minimum and maximum of two values. Use lower case to avoid
815 naming clashes with standard include files. */
816#define max(a, b) ((a) > (b) ? (a) : (b))
817#define min(a, b) ((a) < (b) ? (a) : (b))
818
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700819/* Max and min values of the integer type T, of which only the bottom
820 B bits are used, and where the highest-order used bit is considered
821 to be a sign bit if T is signed. */
822#define MAXVAL(t, b) \
823 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
824 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
825#define MINVAL(t, b) \
826 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
Stephen Hines23360cc2015-09-29 09:28:41 -0700827
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000828/* The extreme time values, assuming no padding. */
829#define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
830#define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
Elliott Hughes713fe642013-08-22 14:13:50 -0700831
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000832/* The extreme time values. These are macros, not constants, so that
833 any portability problems occur only when compiling .c files that use
834 the macros, which is safer for applications that need only zdump and zic.
835 This implementation assumes no padding if time_t is signed and
836 either the compiler lacks support for _Generic or time_t is not one
837 of the standard signed integer types. */
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100838#if HAVE__GENERIC
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000839# define TIME_T_MIN \
840 _Generic((time_t) 0, \
841 signed char: SCHAR_MIN, short: SHRT_MIN, \
842 int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
843 default: TIME_T_MIN_NO_PADDING)
844# define TIME_T_MAX \
845 (TYPE_SIGNED(time_t) \
846 ? _Generic((time_t) 0, \
847 signed char: SCHAR_MAX, short: SHRT_MAX, \
848 int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
849 default: TIME_T_MAX_NO_PADDING) \
850 : (time_t) -1)
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100851enum { SIGNED_PADDING_CHECK_NEEDED
852 = _Generic((time_t) 0,
853 signed char: false, short: false,
854 int: false, long: false, long long: false,
855 default: true) };
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000856#else
857# define TIME_T_MIN TIME_T_MIN_NO_PADDING
858# define TIME_T_MAX TIME_T_MAX_NO_PADDING
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100859enum { SIGNED_PADDING_CHECK_NEEDED = true };
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000860#endif
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100861/* Try to check the padding assumptions. Although TIME_T_MAX and the
862 following check can both have undefined behavior on oddball
863 platforms due to shifts exceeding widths of signed integers, these
864 platforms' compilers are likely to diagnose these issues in integer
865 constant expressions, so it shouldn't hurt to check statically. */
866static_assert(! TYPE_SIGNED(time_t) || ! SIGNED_PADDING_CHECK_NEEDED
867 || TIME_T_MAX >> (TYPE_BIT(time_t) - 2) == 1);
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000868
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800869/*
870** 302 / 1000 is log10(2.0) rounded up.
871** Subtract one for the sign bit if the type is signed;
872** add one for integer division truncation;
873** add one more for a minus sign if the type is signed.
874*/
875#define INT_STRLEN_MAXIMUM(type) \
Elliott Hughesce4783c2013-07-12 17:31:11 -0700876 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
877 1 + TYPE_SIGNED(type))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800878
879/*
880** INITIALIZE(x)
881*/
882
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000883#ifdef GCC_LINT
Elliott Hughes5f564542014-06-19 13:54:10 -0700884# define INITIALIZE(x) ((x) = 0)
885#else
886# define INITIALIZE(x)
887#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800888
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100889/* Whether memory access must strictly follow the C standard.
890 If 0, it's OK to read uninitialized storage so long as the value is
891 not relied upon. Defining it to 0 lets mktime access parts of
892 struct tm that might be uninitialized, as a heuristic when the
893 standard doesn't say what to return and when tm_gmtoff can help
894 mktime likely infer a better value. */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700895#ifndef UNINIT_TRAP
896# define UNINIT_TRAP 0
897#endif
898
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000899#ifdef DEBUG
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100900# undef unreachable
901# define unreachable() abort()
902#elif !defined unreachable
903# ifdef __has_builtin
904# if __has_builtin(__builtin_unreachable)
905# define unreachable() __builtin_unreachable()
906# endif
907# elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
908# define unreachable() __builtin_unreachable()
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000909# endif
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100910# ifndef unreachable
911# define unreachable() ((void) 0)
912# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000913#endif
914
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800915/*
916** For the benefit of GNU folk...
Elliott Hughes5f564542014-06-19 13:54:10 -0700917** '_(MSGID)' uses the current locale's message library string for MSGID.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800918** The default is to use gettext if available, and use MSGID otherwise.
919*/
920
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800921#if HAVE_GETTEXT
922#define _(msgid) gettext(msgid)
923#else /* !HAVE_GETTEXT */
924#define _(msgid) msgid
925#endif /* !HAVE_GETTEXT */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800926
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700927#if !defined TZ_DOMAIN && defined HAVE_GETTEXT
Elliott Hughes5f564542014-06-19 13:54:10 -0700928# define TZ_DOMAIN "tz"
929#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800930
931#if HAVE_INCOMPATIBLE_CTIME_R
932#undef asctime_r
933#undef ctime_r
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100934char *asctime_r(struct tm const *restrict, char *restrict);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700935char *ctime_r(time_t const *, char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800936#endif /* HAVE_INCOMPATIBLE_CTIME_R */
937
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000938/* Handy macros that are independent of tzfile implementation. */
939
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100940enum {
941 SECSPERMIN = 60,
942 MINSPERHOUR = 60,
943 SECSPERHOUR = SECSPERMIN * MINSPERHOUR,
944 HOURSPERDAY = 24,
945 DAYSPERWEEK = 7,
946 DAYSPERNYEAR = 365,
947 DAYSPERLYEAR = DAYSPERNYEAR + 1,
948 MONSPERYEAR = 12,
949 YEARSPERREPEAT = 400 /* years before a Gregorian repeat */
950};
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000951
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100952#define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
953
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000954#define DAYSPERREPEAT ((int_fast32_t) 400 * 365 + 100 - 4 + 1)
955#define SECSPERREPEAT ((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
956#define AVGSECSPERYEAR (SECSPERREPEAT / YEARSPERREPEAT)
957
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100958enum {
959 TM_SUNDAY,
960 TM_MONDAY,
961 TM_TUESDAY,
962 TM_WEDNESDAY,
963 TM_THURSDAY,
964 TM_FRIDAY,
965 TM_SATURDAY
966};
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000967
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100968enum {
969 TM_JANUARY,
970 TM_FEBRUARY,
971 TM_MARCH,
972 TM_APRIL,
973 TM_MAY,
974 TM_JUNE,
975 TM_JULY,
976 TM_AUGUST,
977 TM_SEPTEMBER,
978 TM_OCTOBER,
979 TM_NOVEMBER,
980 TM_DECEMBER
981};
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000982
Almaz Mingaleevd86a3ab2023-04-27 11:24:48 +0100983enum {
984 TM_YEAR_BASE = 1900,
985 TM_WDAY_BASE = TM_MONDAY,
986 EPOCH_YEAR = 1970,
987 EPOCH_WDAY = TM_THURSDAY
988};
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000989
990#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800991
992/*
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000993** Since everything in isleap is modulo 400 (or a factor of 400), we know that
994** isleap(y) == isleap(y % 400)
995** and so
996** isleap(a + b) == isleap((a + b) % 400)
997** or
998** isleap(a + b) == isleap(a % 400 + b % 400)
999** This is true even if % means modulo rather than Fortran remainder
1000** (which is allowed by C89 but not by C99 or later).
1001** We use this to avoid addition overflow problems.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001002*/
1003
Almaz Mingaleev5411aff2022-02-11 12:27:12 +00001004#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001005
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001006#endif /* !defined PRIVATE_H */