blob: 4c033242f3d29971cc82bbc592a49da300088715 [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 Mingaleev5411aff2022-02-11 12:27:12 +000020/*
21** zdump has been made independent of the rest of the time
22** conversion package to increase confidence in the verification it provides.
23** You can use zdump to help in verifying other implementations.
24** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
25*/
26#ifndef USE_LTZ
27# define USE_LTZ 1
28#endif
29
30/* This string was in the Factory zone through version 2016f. */
Elliott Hughesce4783c2013-07-12 17:31:11 -070031#define GRANDPARENTED "Local time zone must be set--see zic manual page"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032
33/*
34** Defaults for preprocessor symbols.
Elliott Hughes9fb22a32015-10-07 17:13:40 -070035** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036*/
37
Elliott Hughes0a610d02016-07-29 14:04:17 -070038#ifndef HAVE_DECL_ASCTIME_R
39#define HAVE_DECL_ASCTIME_R 1
40#endif
41
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000042#if !defined HAVE_GENERIC && defined __has_extension
43# if __has_extension(c_generic_selections)
44# define HAVE_GENERIC 1
45# else
46# define HAVE_GENERIC 0
47# endif
48#endif
49/* _Generic is buggy in pre-4.9 GCC. */
50#if !defined HAVE_GENERIC && defined __GNUC__
51# define HAVE_GENERIC (4 < __GNUC__ + (9 <= __GNUC_MINOR__))
52#endif
53#ifndef HAVE_GENERIC
54# define HAVE_GENERIC (201112 <= __STDC_VERSION__)
55#endif
56
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057#ifndef HAVE_GETTEXT
Elliott Hughesce4783c2013-07-12 17:31:11 -070058#define HAVE_GETTEXT 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059#endif /* !defined HAVE_GETTEXT */
60
61#ifndef HAVE_INCOMPATIBLE_CTIME_R
Elliott Hughesce4783c2013-07-12 17:31:11 -070062#define HAVE_INCOMPATIBLE_CTIME_R 0
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000063#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
Calin Juravle627d37c2014-02-28 11:46:03 +000065#ifndef HAVE_LINK
66#define HAVE_LINK 1
67#endif /* !defined HAVE_LINK */
68
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000069#ifndef HAVE_MALLOC_ERRNO
70#define HAVE_MALLOC_ERRNO 1
71#endif
72
Elliott Hughes0a610d02016-07-29 14:04:17 -070073#ifndef HAVE_POSIX_DECLS
74#define HAVE_POSIX_DECLS 1
75#endif
76
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000077#ifndef HAVE_STDBOOL_H
78#define HAVE_STDBOOL_H (199901 <= __STDC_VERSION__)
79#endif
80
Elliott Hughes9fb22a32015-10-07 17:13:40 -070081#ifndef HAVE_STRDUP
82#define HAVE_STRDUP 1
83#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084
Almaz Mingaleev5411aff2022-02-11 12:27:12 +000085#ifndef HAVE_STRTOLL
86#define HAVE_STRTOLL 1
87#endif
88
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089#ifndef HAVE_SYMLINK
Elliott Hughesce4783c2013-07-12 17:31:11 -070090#define HAVE_SYMLINK 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091#endif /* !defined HAVE_SYMLINK */
92
93#ifndef HAVE_SYS_STAT_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070094#define HAVE_SYS_STAT_H 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095#endif /* !defined HAVE_SYS_STAT_H */
96
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097#ifndef HAVE_UNISTD_H
Elliott Hughesce4783c2013-07-12 17:31:11 -070098#define HAVE_UNISTD_H 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099#endif /* !defined HAVE_UNISTD_H */
100
101#ifndef HAVE_UTMPX_H
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700102#define HAVE_UTMPX_H 1
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103#endif /* !defined HAVE_UTMPX_H */
104
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700105#ifndef NETBSD_INSPIRED
106# define NETBSD_INSPIRED 1
107#endif
Elliott Hughesce4783c2013-07-12 17:31:11 -0700108
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109#if HAVE_INCOMPATIBLE_CTIME_R
110#define asctime_r _incompatible_asctime_r
111#define ctime_r _incompatible_ctime_r
112#endif /* HAVE_INCOMPATIBLE_CTIME_R */
113
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000114/* Enable tm_gmtoff, tm_zone, and environ on GNUish systems. */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700115#define _GNU_SOURCE 1
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000116/* Fix asctime_r on Solaris 11. */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700117#define _POSIX_PTHREAD_SEMANTICS 1
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000118/* Enable strtoimax on pre-C99 Solaris 11. */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700119#define __EXTENSIONS__ 1
120
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000121/* To avoid having 'stat' fail unnecessarily with errno == EOVERFLOW,
122 enable large files on GNUish systems ... */
123#ifndef _FILE_OFFSET_BITS
124# define _FILE_OFFSET_BITS 64
125#endif
126/* ... and on AIX ... */
127#define _LARGE_FILES 1
128/* ... and enable large inode numbers on Mac OS X 10.5 and later. */
129#define _DARWIN_USE_64_BIT_INODE 1
130
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131/*
132** Nested includes
133*/
134
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000135/* Avoid clashes with NetBSD by renaming NetBSD's declarations.
136 If defining the 'timezone' variable, avoid a clash with FreeBSD's
137 'timezone' function by renaming its declaration. */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700138#define localtime_rz sys_localtime_rz
139#define mktime_z sys_mktime_z
140#define posix2time_z sys_posix2time_z
141#define time2posix_z sys_time2posix_z
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000142#if defined USG_COMPAT && USG_COMPAT == 2
143# define timezone sys_timezone
144#endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700145#define timezone_t sys_timezone_t
146#define tzalloc sys_tzalloc
147#define tzfree sys_tzfree
148#include <time.h>
149#undef localtime_rz
150#undef mktime_z
151#undef posix2time_z
152#undef time2posix_z
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000153#if defined USG_COMPAT && USG_COMPAT == 2
154# undef timezone
155#endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700156#undef timezone_t
157#undef tzalloc
158#undef tzfree
159
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000160#include <sys/types.h> /* for time_t */
161#include <string.h>
162#include <limits.h> /* for CHAR_BIT et al. */
163#include <stdlib.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000165#include <errno.h>
166
167#ifndef EINVAL
168# define EINVAL ERANGE
169#endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700170
171#ifndef ENAMETOOLONG
172# define ENAMETOOLONG EINVAL
173#endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000174#ifndef ENOMEM
175# define ENOMEM EINVAL
176#endif
Elliott Hughes0a610d02016-07-29 14:04:17 -0700177#ifndef ENOTSUP
178# define ENOTSUP EINVAL
179#endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700180#ifndef EOVERFLOW
181# define EOVERFLOW EINVAL
182#endif
183
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184#if HAVE_GETTEXT
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000185#include <libintl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186#endif /* HAVE_GETTEXT */
187
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188#if HAVE_UNISTD_H
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000189#include <unistd.h> /* for R_OK, and other POSIX goodness */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190#endif /* HAVE_UNISTD_H */
191
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700192#ifndef HAVE_STRFTIME_L
193# if _POSIX_VERSION < 200809
194# define HAVE_STRFTIME_L 0
195# else
196# define HAVE_STRFTIME_L 1
197# endif
198#endif
199
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000200#ifndef USG_COMPAT
201# ifndef _XOPEN_VERSION
202# define USG_COMPAT 0
203# else
204# define USG_COMPAT 1
205# endif
206#endif
207
208#ifndef HAVE_TZNAME
209# if _POSIX_VERSION < 198808 && !USG_COMPAT
210# define HAVE_TZNAME 0
211# else
212# define HAVE_TZNAME 1
213# endif
214#endif
215
216#ifndef ALTZONE
217# if defined __sun || defined _M_XENIX
218# define ALTZONE 1
219# else
220# define ALTZONE 0
221# endif
222#endif
223
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224#ifndef R_OK
Elliott Hughesce4783c2013-07-12 17:31:11 -0700225#define R_OK 4
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226#endif /* !defined R_OK */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228/*
229** Define HAVE_STDINT_H's default value here, rather than at the
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000230** start, since __GLIBC__ and INTMAX_MAX's values depend on
231** previously-included files. glibc 2.1 and Solaris 10 and later have
232** stdint.h, even with pre-C99 compilers.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233*/
234#ifndef HAVE_STDINT_H
235#define HAVE_STDINT_H \
Elliott Hughes5f564542014-06-19 13:54:10 -0700236 (199901 <= __STDC_VERSION__ \
237 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000238 || __CYGWIN__ || INTMAX_MAX)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239#endif /* !defined HAVE_STDINT_H */
240
241#if HAVE_STDINT_H
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000242#include <stdint.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243#endif /* !HAVE_STDINT_H */
244
Elliott Hughesce4783c2013-07-12 17:31:11 -0700245#ifndef HAVE_INTTYPES_H
246# define HAVE_INTTYPES_H HAVE_STDINT_H
247#endif
248#if HAVE_INTTYPES_H
249# include <inttypes.h>
250#endif
251
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800252/* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700253#ifdef __LONG_LONG_MAX__
254# ifndef LLONG_MAX
255# define LLONG_MAX __LONG_LONG_MAX__
256# endif
257# ifndef LLONG_MIN
258# define LLONG_MIN (-1 - LLONG_MAX)
259# endif
260#endif
261
262#ifndef INT_FAST64_MAX
Elliott Hughesce4783c2013-07-12 17:31:11 -0700263# ifdef LLONG_MAX
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700264typedef long long int_fast64_t;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700265# define INT_FAST64_MIN LLONG_MIN
266# define INT_FAST64_MAX LLONG_MAX
267# else
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700268# if LONG_MAX >> 31 < 0xffffffff
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800269Please use a compiler that supports a 64-bit integer type (or wider);
270you may need to compile with "-DHAVE_STDINT_H".
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700271# endif
Elliott Hughesce4783c2013-07-12 17:31:11 -0700272typedef long int_fast64_t;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700273# define INT_FAST64_MIN LONG_MIN
274# define INT_FAST64_MAX LONG_MAX
275# endif
276#endif
277
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000278#ifndef PRIdFAST64
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700279# if INT_FAST64_MAX == LLONG_MAX
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000280# define PRIdFAST64 "lld"
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700281# else
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000282# define PRIdFAST64 "ld"
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700283# endif
284#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000286#ifndef SCNdFAST64
287# define SCNdFAST64 PRIdFAST64
288#endif
289
Elliott Hughesce4783c2013-07-12 17:31:11 -0700290#ifndef INT_FAST32_MAX
291# if INT_MAX >> 31 == 0
292typedef long int_fast32_t;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700293# define INT_FAST32_MAX LONG_MAX
294# define INT_FAST32_MIN LONG_MIN
Elliott Hughesce4783c2013-07-12 17:31:11 -0700295# else
296typedef int int_fast32_t;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700297# define INT_FAST32_MAX INT_MAX
298# define INT_FAST32_MIN INT_MIN
Elliott Hughesce4783c2013-07-12 17:31:11 -0700299# endif
300#endif
301
302#ifndef INTMAX_MAX
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700303# ifdef LLONG_MAX
Elliott Hughesce4783c2013-07-12 17:31:11 -0700304typedef long long intmax_t;
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000305# if HAVE_STRTOLL
306# define strtoimax strtoll
307# endif
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700308# define INTMAX_MAX LLONG_MAX
309# define INTMAX_MIN LLONG_MIN
Elliott Hughesce4783c2013-07-12 17:31:11 -0700310# else
311typedef long intmax_t;
Elliott Hughese0d0b152013-09-27 00:04:30 -0700312# define INTMAX_MAX LONG_MAX
313# define INTMAX_MIN LONG_MIN
Elliott Hughesce4783c2013-07-12 17:31:11 -0700314# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000315# ifndef strtoimax
316# define strtoimax strtol
317# endif
Elliott Hughesce4783c2013-07-12 17:31:11 -0700318#endif
319
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700320#ifndef PRIdMAX
321# if INTMAX_MAX == LLONG_MAX
322# define PRIdMAX "lld"
323# else
324# define PRIdMAX "ld"
325# endif
326#endif
327
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000328#ifndef UINT_FAST32_MAX
329typedef unsigned long uint_fast32_t;
330#endif
331
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700332#ifndef UINT_FAST64_MAX
333# if defined ULLONG_MAX || defined __LONG_LONG_MAX__
334typedef unsigned long long uint_fast64_t;
335# else
336# if ULONG_MAX >> 31 >> 1 < 0xffffffff
337Please use a compiler that supports a 64-bit integer type (or wider);
338you may need to compile with "-DHAVE_STDINT_H".
339# endif
340typedef unsigned long uint_fast64_t;
341# endif
342#endif
343
Elliott Hughesce4783c2013-07-12 17:31:11 -0700344#ifndef UINTMAX_MAX
345# if defined ULLONG_MAX || defined __LONG_LONG_MAX__
346typedef unsigned long long uintmax_t;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700347# else
348typedef unsigned long uintmax_t;
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700349# endif
350#endif
351
352#ifndef PRIuMAX
353# if defined ULLONG_MAX || defined __LONG_LONG_MAX__
354# define PRIuMAX "llu"
355# else
Elliott Hughesce4783c2013-07-12 17:31:11 -0700356# define PRIuMAX "lu"
357# endif
358#endif
359
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800360#ifndef INT32_MAX
361#define INT32_MAX 0x7fffffff
362#endif /* !defined INT32_MAX */
363#ifndef INT32_MIN
364#define INT32_MIN (-1 - INT32_MAX)
365#endif /* !defined INT32_MIN */
366
Elliott Hughes5f564542014-06-19 13:54:10 -0700367#ifndef SIZE_MAX
368#define SIZE_MAX ((size_t) -1)
369#endif
370
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000371#if 3 <= __GNUC__
372# define ATTRIBUTE_CONST __attribute__((const))
373# define ATTRIBUTE_MALLOC __attribute__((__malloc__))
374# define ATTRIBUTE_PURE __attribute__((__pure__))
375# define ATTRIBUTE_FORMAT(spec) __attribute__((__format__ spec))
Elliott Hughesce4783c2013-07-12 17:31:11 -0700376#else
377# define ATTRIBUTE_CONST /* empty */
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000378# define ATTRIBUTE_MALLOC /* empty */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700379# define ATTRIBUTE_PURE /* empty */
Elliott Hughese0d0b152013-09-27 00:04:30 -0700380# define ATTRIBUTE_FORMAT(spec) /* empty */
Elliott Hughesce4783c2013-07-12 17:31:11 -0700381#endif
382
383#if !defined _Noreturn && __STDC_VERSION__ < 201112
384# if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000385# define _Noreturn __attribute__((__noreturn__))
Elliott Hughesce4783c2013-07-12 17:31:11 -0700386# else
387# define _Noreturn
388# endif
389#endif
390
391#if __STDC_VERSION__ < 199901 && !defined restrict
392# define restrict /* empty */
393#endif
394
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800395/*
396** Workarounds for compilers/systems.
397*/
398
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000399#ifndef EPOCH_LOCAL
400# define EPOCH_LOCAL 0
401#endif
402#ifndef EPOCH_OFFSET
403# define EPOCH_OFFSET 0
404#endif
405#ifndef RESERVE_STD_EXT_IDS
406# define RESERVE_STD_EXT_IDS 0
407#endif
408
409/* If standard C identifiers with external linkage (e.g., localtime)
410 are reserved and are not already being renamed anyway, rename them
411 as if compiling with '-Dtime_tz=time_t'. */
412#if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
413# define time_tz time_t
414#endif
415
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416/*
Elliott Hughesce4783c2013-07-12 17:31:11 -0700417** Compile with -Dtime_tz=T to build the tz package with a private
418** time_t type equivalent to T rather than the system-supplied time_t.
419** This debugging feature can test unusual design decisions
420** (e.g., time_t wider than 'long', or unsigned time_t) even on
421** typical platforms.
422*/
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000423#if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
424# define TZ_TIME_T 1
425#else
426# define TZ_TIME_T 0
427#endif
428
429#if defined LOCALTIME_IMPLEMENTATION && TZ_TIME_T
Elliott Hughesce4783c2013-07-12 17:31:11 -0700430static time_t sys_time(time_t *x) { return time(x); }
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000431#endif
432
433#if TZ_TIME_T
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700434
435typedef time_tz tz_time_t;
Elliott Hughesce4783c2013-07-12 17:31:11 -0700436
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000437# undef asctime
438# define asctime tz_asctime
439# undef asctime_r
440# define asctime_r tz_asctime_r
Elliott Hughesce4783c2013-07-12 17:31:11 -0700441# undef ctime
442# define ctime tz_ctime
443# undef ctime_r
444# define ctime_r tz_ctime_r
445# undef difftime
446# define difftime tz_difftime
447# undef gmtime
448# define gmtime tz_gmtime
449# undef gmtime_r
450# define gmtime_r tz_gmtime_r
451# undef localtime
452# define localtime tz_localtime
453# undef localtime_r
454# define localtime_r tz_localtime_r
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700455# undef localtime_rz
456# define localtime_rz tz_localtime_rz
Elliott Hughesce4783c2013-07-12 17:31:11 -0700457# undef mktime
458# define mktime tz_mktime
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700459# undef mktime_z
460# define mktime_z tz_mktime_z
461# undef offtime
462# define offtime tz_offtime
463# undef posix2time
464# define posix2time tz_posix2time
465# undef posix2time_z
466# define posix2time_z tz_posix2time_z
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000467# undef strftime
468# define strftime tz_strftime
Elliott Hughesce4783c2013-07-12 17:31:11 -0700469# undef time
470# define time tz_time
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700471# undef time2posix
472# define time2posix tz_time2posix
473# undef time2posix_z
474# define time2posix_z tz_time2posix_z
Elliott Hughesce4783c2013-07-12 17:31:11 -0700475# undef time_t
476# define time_t tz_time_t
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700477# undef timegm
478# define timegm tz_timegm
479# undef timelocal
480# define timelocal tz_timelocal
481# undef timeoff
482# define timeoff tz_timeoff
483# undef tzalloc
484# define tzalloc tz_tzalloc
485# undef tzfree
486# define tzfree tz_tzfree
487# undef tzset
488# define tzset tz_tzset
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000489# if HAVE_STRFTIME_L
490# undef strftime_l
491# define strftime_l tz_strftime_l
492# endif
493# if HAVE_TZNAME
494# undef tzname
495# define tzname tz_tzname
496# endif
497# if USG_COMPAT
498# undef daylight
499# define daylight tz_daylight
500# undef timezone
501# define timezone tz_timezone
502# endif
503# if ALTZONE
504# undef altzone
505# define altzone tz_altzone
506# endif
Elliott Hughesce4783c2013-07-12 17:31:11 -0700507
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000508char *asctime(struct tm const *);
509char *asctime_r(struct tm const *restrict, char *restrict);
Elliott Hughesce4783c2013-07-12 17:31:11 -0700510char *ctime(time_t const *);
511char *ctime_r(time_t const *, char *);
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000512double difftime(time_t, time_t) ATTRIBUTE_CONST;
513size_t strftime(char *restrict, size_t, char const *restrict,
514 struct tm const *restrict);
515# if HAVE_STRFTIME_L
516size_t strftime_l(char *restrict, size_t, char const *restrict,
517 struct tm const *restrict, locale_t);
518# endif
Elliott Hughesce4783c2013-07-12 17:31:11 -0700519struct tm *gmtime(time_t const *);
520struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
521struct tm *localtime(time_t const *);
522struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
523time_t mktime(struct tm *);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700524time_t time(time_t *);
525void tzset(void);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800526#endif
527
Elliott Hughes0a610d02016-07-29 14:04:17 -0700528#if !HAVE_DECL_ASCTIME_R && !defined asctime_r
529extern char *asctime_r(struct tm const *restrict, char *restrict);
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700530#endif
531
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000532#ifndef HAVE_DECL_ENVIRON
533# if defined environ || defined __USE_GNU
534# define HAVE_DECL_ENVIRON 1
535# else
536# define HAVE_DECL_ENVIRON 0
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700537# endif
538#endif
Elliott Hughes0a610d02016-07-29 14:04:17 -0700539
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000540#if !HAVE_DECL_ENVIRON
541extern char **environ;
542#endif
543
544#if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
545extern char *tzname[];
546#endif
547#if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
548extern long timezone;
549extern int daylight;
550#endif
551#if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700552extern long altzone;
553#endif
554
555/*
556** The STD_INSPIRED functions are similar, but most also need
557** declarations if time_tz is defined.
558*/
559
560#ifdef STD_INSPIRED
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000561# if TZ_TIME_T || !defined offtime
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700562struct tm *offtime(time_t const *, long);
563# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000564# if TZ_TIME_T || !defined timegm
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700565time_t timegm(struct tm *);
566# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000567# if TZ_TIME_T || !defined timelocal
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700568time_t timelocal(struct tm *);
569# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000570# if TZ_TIME_T || !defined timeoff
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700571time_t timeoff(struct tm *, long);
572# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000573# if TZ_TIME_T || !defined time2posix
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700574time_t time2posix(time_t);
575# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000576# if TZ_TIME_T || !defined posix2time
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700577time_t posix2time(time_t);
578# endif
579#endif
580
581/* Infer TM_ZONE on systems where this information is known, but suppress
582 guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
583#if (defined __GLIBC__ \
584 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
585 || (defined __APPLE__ && defined __MACH__))
586# if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
587# define TM_GMTOFF tm_gmtoff
588# endif
589# if !defined TM_ZONE && !defined NO_TM_ZONE
590# define TM_ZONE tm_zone
591# endif
592#endif
593
594/*
595** Define functions that are ABI compatible with NetBSD but have
596** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
597** and labors under the misconception that 'const timezone_t' is a
598** pointer to a constant. This use of 'const' is ineffective, so it
599** is not done here. What we call 'struct state' NetBSD calls
600** 'struct __state', but this is a private name so it doesn't matter.
601*/
602#if NETBSD_INSPIRED
603typedef struct state *timezone_t;
604struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
605 struct tm *restrict);
606time_t mktime_z(timezone_t restrict, struct tm *restrict);
607timezone_t tzalloc(char const *);
608void tzfree(timezone_t);
609# ifdef STD_INSPIRED
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000610# if TZ_TIME_T || !defined posix2time_z
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700611time_t posix2time_z(timezone_t, time_t) ATTRIBUTE_PURE;
612# endif
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000613# if TZ_TIME_T || !defined time2posix_z
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700614time_t time2posix_z(timezone_t, time_t) ATTRIBUTE_PURE;
615# endif
616# endif
617#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800618
619/*
620** Finally, some convenience items.
621*/
622
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000623#if HAVE_STDBOOL_H
624# include <stdbool.h>
625#else
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700626# define true 1
627# define false 0
628# define bool int
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700629#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000631#define TYPE_BIT(type) (sizeof(type) * CHAR_BIT)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800632#define TYPE_SIGNED(type) (((type) -1) < 0)
Stephen Hines23360cc2015-09-29 09:28:41 -0700633#define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
634
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700635/* Max and min values of the integer type T, of which only the bottom
636 B bits are used, and where the highest-order used bit is considered
637 to be a sign bit if T is signed. */
638#define MAXVAL(t, b) \
639 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
640 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
641#define MINVAL(t, b) \
642 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
Stephen Hines23360cc2015-09-29 09:28:41 -0700643
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000644/* The extreme time values, assuming no padding. */
645#define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
646#define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
Elliott Hughes713fe642013-08-22 14:13:50 -0700647
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000648/* The extreme time values. These are macros, not constants, so that
649 any portability problems occur only when compiling .c files that use
650 the macros, which is safer for applications that need only zdump and zic.
651 This implementation assumes no padding if time_t is signed and
652 either the compiler lacks support for _Generic or time_t is not one
653 of the standard signed integer types. */
654#if HAVE_GENERIC
655# define TIME_T_MIN \
656 _Generic((time_t) 0, \
657 signed char: SCHAR_MIN, short: SHRT_MIN, \
658 int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
659 default: TIME_T_MIN_NO_PADDING)
660# define TIME_T_MAX \
661 (TYPE_SIGNED(time_t) \
662 ? _Generic((time_t) 0, \
663 signed char: SCHAR_MAX, short: SHRT_MAX, \
664 int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
665 default: TIME_T_MAX_NO_PADDING) \
666 : (time_t) -1)
667#else
668# define TIME_T_MIN TIME_T_MIN_NO_PADDING
669# define TIME_T_MAX TIME_T_MAX_NO_PADDING
670#endif
671
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800672/*
673** 302 / 1000 is log10(2.0) rounded up.
674** Subtract one for the sign bit if the type is signed;
675** add one for integer division truncation;
676** add one more for a minus sign if the type is signed.
677*/
678#define INT_STRLEN_MAXIMUM(type) \
Elliott Hughesce4783c2013-07-12 17:31:11 -0700679 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
680 1 + TYPE_SIGNED(type))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800681
682/*
683** INITIALIZE(x)
684*/
685
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000686#ifdef GCC_LINT
Elliott Hughes5f564542014-06-19 13:54:10 -0700687# define INITIALIZE(x) ((x) = 0)
688#else
689# define INITIALIZE(x)
690#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800691
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700692#ifndef UNINIT_TRAP
693# define UNINIT_TRAP 0
694#endif
695
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000696#ifdef DEBUG
697# define UNREACHABLE() abort()
698#elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
699# define UNREACHABLE() __builtin_unreachable()
700#elif defined __has_builtin
701# if __has_builtin(__builtin_unreachable)
702# define UNREACHABLE() __builtin_unreachable()
703# endif
704#endif
705#ifndef UNREACHABLE
706# define UNREACHABLE() ((void) 0)
707#endif
708
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800709/*
710** For the benefit of GNU folk...
Elliott Hughes5f564542014-06-19 13:54:10 -0700711** '_(MSGID)' uses the current locale's message library string for MSGID.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800712** The default is to use gettext if available, and use MSGID otherwise.
713*/
714
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800715#if HAVE_GETTEXT
716#define _(msgid) gettext(msgid)
717#else /* !HAVE_GETTEXT */
718#define _(msgid) msgid
719#endif /* !HAVE_GETTEXT */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800720
Elliott Hughes9fb22a32015-10-07 17:13:40 -0700721#if !defined TZ_DOMAIN && defined HAVE_GETTEXT
Elliott Hughes5f564542014-06-19 13:54:10 -0700722# define TZ_DOMAIN "tz"
723#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800724
725#if HAVE_INCOMPATIBLE_CTIME_R
726#undef asctime_r
727#undef ctime_r
Elliott Hughesce4783c2013-07-12 17:31:11 -0700728char *asctime_r(struct tm const *, char *);
729char *ctime_r(time_t const *, char *);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800730#endif /* HAVE_INCOMPATIBLE_CTIME_R */
731
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000732/* Handy macros that are independent of tzfile implementation. */
733
734#define SECSPERMIN 60
735#define MINSPERHOUR 60
736#define HOURSPERDAY 24
737#define DAYSPERWEEK 7
738#define DAYSPERNYEAR 365
739#define DAYSPERLYEAR 366
740#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
741#define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
742#define MONSPERYEAR 12
743
Elliott Hughesce4783c2013-07-12 17:31:11 -0700744#define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000745#define DAYSPERREPEAT ((int_fast32_t) 400 * 365 + 100 - 4 + 1)
746#define SECSPERREPEAT ((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
747#define AVGSECSPERYEAR (SECSPERREPEAT / YEARSPERREPEAT)
748
749#define TM_SUNDAY 0
750#define TM_MONDAY 1
751#define TM_TUESDAY 2
752#define TM_WEDNESDAY 3
753#define TM_THURSDAY 4
754#define TM_FRIDAY 5
755#define TM_SATURDAY 6
756
757#define TM_JANUARY 0
758#define TM_FEBRUARY 1
759#define TM_MARCH 2
760#define TM_APRIL 3
761#define TM_MAY 4
762#define TM_JUNE 5
763#define TM_JULY 6
764#define TM_AUGUST 7
765#define TM_SEPTEMBER 8
766#define TM_OCTOBER 9
767#define TM_NOVEMBER 10
768#define TM_DECEMBER 11
769
770#define TM_YEAR_BASE 1900
771#define TM_WDAY_BASE TM_MONDAY
772
773#define EPOCH_YEAR 1970
774#define EPOCH_WDAY TM_THURSDAY
775
776#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800777
778/*
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000779** Since everything in isleap is modulo 400 (or a factor of 400), we know that
780** isleap(y) == isleap(y % 400)
781** and so
782** isleap(a + b) == isleap((a + b) % 400)
783** or
784** isleap(a + b) == isleap(a % 400 + b % 400)
785** This is true even if % means modulo rather than Fortran remainder
786** (which is allowed by C89 but not by C99 or later).
787** We use this to avoid addition overflow problems.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800788*/
789
Almaz Mingaleev5411aff2022-02-11 12:27:12 +0000790#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800791
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800792#endif /* !defined PRIVATE_H */