1 /* Private header for tzdb code. */
8 ** This file is in the public domain, so clarified as of
9 ** 1996-06-05 by Arthur David Olson.
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.
20 /* PORT_TO_C89 means the code should work even if the underlying
21 compiler and library support only C89 plus C99's 'long long'
22 and perhaps a few other extensions to C89. SUPPORT_C89 means the
23 tzcode library should support C89 callers in addition to the usual
24 support for C99-and-later callers; however, C89 support can trigger
25 latent bugs in C99-and-later callers. These macros are obsolescent,
26 and the plan is to remove them along with any code needed only when
27 they are nonzero. A good time to do that might be in the year 2029
28 because RHEL 7 (whose GCC defaults to C89) extended life cycle
29 support (ELS) is scheduled to end on 2028-06-30. */
31 # define PORT_TO_C89 0
34 # define SUPPORT_C89 0
37 #ifndef __STDC_VERSION__
38 # define __STDC_VERSION__ 0
41 /* Define true, false and bool if they don't work out of the box. */
42 #if PORT_TO_C89 && __STDC_VERSION__ < 199901
46 #elif __STDC_VERSION__ < 202311
50 #if __STDC_VERSION__ < 202311
51 # define static_assert(cond) extern int static_assert_check[(cond) ? 1 : -1]
55 ** zdump has been made independent of the rest of the time
56 ** conversion package to increase confidence in the verification it provides.
57 ** You can use zdump to help in verifying other implementations.
58 ** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
64 /* This string was in the Factory zone through version 2016f. */
65 #define GRANDPARENTED "Local time zone must be set--see zic manual page"
68 ** Defaults for preprocessor symbols.
69 ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
72 #ifndef HAVE_DECL_ASCTIME_R
73 # define HAVE_DECL_ASCTIME_R 1
76 #if !defined HAVE__GENERIC && defined __has_extension
77 # if !__has_extension(c_generic_selections)
78 # define HAVE__GENERIC 0
81 /* _Generic is buggy in pre-4.9 GCC. */
82 #if !defined HAVE__GENERIC && defined __GNUC__ && !defined __STRICT_ANSI__
83 # define HAVE__GENERIC (4 < __GNUC__ + (9 <= __GNUC_MINOR__))
86 # define HAVE__GENERIC (201112 <= __STDC_VERSION__)
89 #if !defined HAVE_GETTEXT && defined __has_include
90 # if __has_include(<libintl.h>)
91 # define HAVE_GETTEXT true
95 # define HAVE_GETTEXT false
98 #ifndef HAVE_INCOMPATIBLE_CTIME_R
99 # define HAVE_INCOMPATIBLE_CTIME_R 0
104 #endif /* !defined HAVE_LINK */
106 #ifndef HAVE_MALLOC_ERRNO
107 # define HAVE_MALLOC_ERRNO 1
110 #ifndef HAVE_POSIX_DECLS
111 # define HAVE_POSIX_DECLS 1
115 # define HAVE_SETENV 1
119 # define HAVE_STRDUP 1
123 # define HAVE_SYMLINK 1
124 #endif /* !defined HAVE_SYMLINK */
126 #if !defined HAVE_SYS_STAT_H && defined __has_include
127 # if !__has_include(<sys/stat.h>)
128 # define HAVE_SYS_STAT_H false
131 #ifndef HAVE_SYS_STAT_H
132 # define HAVE_SYS_STAT_H true
135 #if !defined HAVE_UNISTD_H && defined __has_include
136 # if !__has_include(<unistd.h>)
137 # define HAVE_UNISTD_H false
140 #ifndef HAVE_UNISTD_H
141 # define HAVE_UNISTD_H true
144 #ifndef NETBSD_INSPIRED
145 # define NETBSD_INSPIRED 1
148 #if HAVE_INCOMPATIBLE_CTIME_R
149 # define asctime_r _incompatible_asctime_r
150 # define ctime_r _incompatible_ctime_r
151 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
153 /* Enable tm_gmtoff, tm_zone, and environ on GNUish systems. */
154 #define _GNU_SOURCE 1
155 /* Fix asctime_r on Solaris 11. */
156 #define _POSIX_PTHREAD_SEMANTICS 1
157 /* Enable strtoimax on pre-C99 Solaris 11. */
158 #define __EXTENSIONS__ 1
160 /* On GNUish systems where time_t might be 32 or 64 bits, use 64.
161 On these platforms _FILE_OFFSET_BITS must also be 64; otherwise
162 setting _TIME_BITS to 64 does not work. The code does not
163 otherwise rely on _FILE_OFFSET_BITS being 64, since it does not
164 use off_t or functions like 'stat' that depend on off_t. */
165 #ifndef _FILE_OFFSET_BITS
166 # define _FILE_OFFSET_BITS 64
168 #if !defined _TIME_BITS && _FILE_OFFSET_BITS == 64
169 # define _TIME_BITS 64
176 /* Avoid clashes with NetBSD by renaming NetBSD's declarations.
177 If defining the 'timezone' variable, avoid a clash with FreeBSD's
178 'timezone' function by renaming its declaration. */
179 #define localtime_rz sys_localtime_rz
180 #define mktime_z sys_mktime_z
181 #define posix2time_z sys_posix2time_z
182 #define time2posix_z sys_time2posix_z
183 #if defined USG_COMPAT && USG_COMPAT == 2
184 # define timezone sys_timezone
186 #define timezone_t sys_timezone_t
187 #define tzalloc sys_tzalloc
188 #define tzfree sys_tzfree
194 #if defined USG_COMPAT && USG_COMPAT == 2
204 # include <inttypes.h>
206 #include <limits.h> /* for CHAR_BIT et al. */
212 # define EINVAL ERANGE
216 # define ELOOP EINVAL
219 # define ENAMETOOLONG EINVAL
222 # define ENOMEM EINVAL
225 # define ENOTSUP EINVAL
228 # define EOVERFLOW EINVAL
232 # include <libintl.h>
233 #endif /* HAVE_GETTEXT */
236 # include <unistd.h> /* for R_OK, and other POSIX goodness */
237 #endif /* HAVE_UNISTD_H */
239 #ifndef HAVE_STRFTIME_L
240 # if _POSIX_VERSION < 200809
241 # define HAVE_STRFTIME_L 0
243 # define HAVE_STRFTIME_L 1
248 # ifndef _XOPEN_VERSION
249 # define USG_COMPAT 0
251 # define USG_COMPAT 1
256 # if _POSIX_VERSION < 198808 && !USG_COMPAT
257 # define HAVE_TZNAME 0
259 # define HAVE_TZNAME 1
264 # if defined __sun || defined _M_XENIX
273 #endif /* !defined R_OK */
278 ** Define HAVE_STDINT_H's default value here, rather than at the
279 ** start, since __GLIBC__ and INTMAX_MAX's values depend on
280 ** previously included files. glibc 2.1 and Solaris 10 and later have
281 ** stdint.h, even with pre-C99 compilers.
283 #if !defined HAVE_STDINT_H && defined __has_include
284 # define HAVE_STDINT_H true /* C23 __has_include implies C99 stdint.h. */
286 #ifndef HAVE_STDINT_H
287 # define HAVE_STDINT_H \
288 (199901 <= __STDC_VERSION__ \
289 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
290 || __CYGWIN__ || INTMAX_MAX)
291 #endif /* !defined HAVE_STDINT_H */
295 #endif /* !HAVE_STDINT_H */
297 #ifndef HAVE_INTTYPES_H
298 # define HAVE_INTTYPES_H HAVE_STDINT_H
301 # include <inttypes.h>
304 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
305 #if defined __LONG_LONG_MAX__ && !defined __STRICT_ANSI__
307 # define LLONG_MAX __LONG_LONG_MAX__
310 # define LLONG_MIN (-1 - LLONG_MAX)
313 # define ULLONG_MAX (LLONG_MAX * 2ull + 1)
317 #ifndef INT_FAST64_MAX
318 # if 1 <= LONG_MAX >> 31 >> 31
319 typedef long int_fast64_t;
320 # define INT_FAST64_MIN LONG_MIN
321 # define INT_FAST64_MAX LONG_MAX
323 /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
324 typedef long long int_fast64_t;
325 # define INT_FAST64_MIN LLONG_MIN
326 # define INT_FAST64_MAX LLONG_MAX
331 # if INT_FAST64_MAX == LONG_MAX
332 # define PRIdFAST64 "ld"
334 # define PRIdFAST64 "lld"
339 # define SCNdFAST64 PRIdFAST64
342 #ifndef INT_FAST32_MAX
343 # if INT_MAX >> 31 == 0
344 typedef long int_fast32_t;
345 # define INT_FAST32_MAX LONG_MAX
346 # define INT_FAST32_MIN LONG_MIN
348 typedef int int_fast32_t;
349 # define INT_FAST32_MAX INT_MAX
350 # define INT_FAST32_MIN INT_MIN
356 typedef long long intmax_t;
357 # ifndef HAVE_STRTOLL
358 # define HAVE_STRTOLL true
361 # define strtoimax strtoll
363 # define INTMAX_MAX LLONG_MAX
364 # define INTMAX_MIN LLONG_MIN
366 typedef long intmax_t;
367 # define INTMAX_MAX LONG_MAX
368 # define INTMAX_MIN LONG_MIN
371 # define strtoimax strtol
376 # if INTMAX_MAX == LLONG_MAX
377 # define PRIdMAX "lld"
379 # define PRIdMAX "ld"
384 # define PTRDIFF_MAX MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t))
387 #ifndef UINT_FAST32_MAX
388 typedef unsigned long uint_fast32_t;
391 #ifndef UINT_FAST64_MAX
392 # if 3 <= ULONG_MAX >> 31 >> 31
393 typedef unsigned long uint_fast64_t;
394 # define UINT_FAST64_MAX ULONG_MAX
396 /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
397 typedef unsigned long long uint_fast64_t;
398 # define UINT_FAST64_MAX ULLONG_MAX
404 typedef unsigned long long uintmax_t;
405 # define UINTMAX_MAX ULLONG_MAX
407 typedef unsigned long uintmax_t;
408 # define UINTMAX_MAX ULONG_MAX
414 # define PRIuMAX "llu"
416 # define PRIuMAX "lu"
421 # define SIZE_MAX ((size_t) -1)
424 #endif /* PORT_TO_C89 */
426 /* The maximum size of any created object, as a signed integer.
427 Although the C standard does not outright prohibit larger objects,
428 behavior is undefined if the result of pointer subtraction does not
429 fit into ptrdiff_t, and the code assumes in several places that
430 pointer subtraction works. As a practical matter it's OK to not
431 support objects larger than this. */
432 #define INDEX_MAX ((ptrdiff_t) min(PTRDIFF_MAX, SIZE_MAX))
434 /* Support ckd_add, ckd_sub, ckd_mul on C23 or recent-enough GCC-like
435 hosts, unless compiled with -DHAVE_STDCKDINT_H=0 or with pre-C23 EDG. */
436 #if !defined HAVE_STDCKDINT_H && defined __has_include
437 # if __has_include(<stdckdint.h>)
438 # define HAVE_STDCKDINT_H true
441 #ifdef HAVE_STDCKDINT_H
442 # if HAVE_STDCKDINT_H
443 # include <stdckdint.h>
445 #elif defined __EDG__
446 /* Do nothing, to work around EDG bug <https://bugs.gnu.org/53256>. */
447 #elif defined __has_builtin
448 # if __has_builtin(__builtin_add_overflow)
449 # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
451 # if __has_builtin(__builtin_sub_overflow)
452 # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
454 # if __has_builtin(__builtin_mul_overflow)
455 # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
458 # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
459 # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
460 # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
464 # define ATTRIBUTE_MALLOC __attribute__((malloc))
465 # define ATTRIBUTE_FORMAT(spec) __attribute__((format spec))
467 # define ATTRIBUTE_MALLOC /* empty */
468 # define ATTRIBUTE_FORMAT(spec) /* empty */
471 #if (defined __has_c_attribute \
472 && (202311 <= __STDC_VERSION__ || !defined __STRICT_ANSI__))
473 # define HAVE___HAS_C_ATTRIBUTE true
475 # define HAVE___HAS_C_ATTRIBUTE false
478 #if HAVE___HAS_C_ATTRIBUTE
479 # if __has_c_attribute(deprecated)
480 # define ATTRIBUTE_DEPRECATED [[deprecated]]
483 #ifndef ATTRIBUTE_DEPRECATED
484 # if 3 < __GNUC__ + (2 <= __GNUC_MINOR__)
485 # define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
487 # define ATTRIBUTE_DEPRECATED /* empty */
491 #if HAVE___HAS_C_ATTRIBUTE
492 # if __has_c_attribute(fallthrough)
493 # define ATTRIBUTE_FALLTHROUGH [[fallthrough]]
496 #ifndef ATTRIBUTE_FALLTHROUGH
498 # define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
500 # define ATTRIBUTE_FALLTHROUGH ((void) 0)
504 #if HAVE___HAS_C_ATTRIBUTE
505 # if __has_c_attribute(maybe_unused)
506 # define ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]]
509 #ifndef ATTRIBUTE_MAYBE_UNUSED
510 # if 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
511 # define ATTRIBUTE_MAYBE_UNUSED __attribute__((unused))
513 # define ATTRIBUTE_MAYBE_UNUSED /* empty */
517 #if HAVE___HAS_C_ATTRIBUTE
518 # if __has_c_attribute(noreturn)
519 # define ATTRIBUTE_NORETURN [[noreturn]]
522 #ifndef ATTRIBUTE_NORETURN
523 # if 201112 <= __STDC_VERSION__
524 # define ATTRIBUTE_NORETURN _Noreturn
525 # elif 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
526 # define ATTRIBUTE_NORETURN __attribute__((noreturn))
528 # define ATTRIBUTE_NORETURN /* empty */
532 #if HAVE___HAS_C_ATTRIBUTE
533 # if __has_c_attribute(reproducible)
534 # define ATTRIBUTE_REPRODUCIBLE [[reproducible]]
537 #ifndef ATTRIBUTE_REPRODUCIBLE
539 # define ATTRIBUTE_REPRODUCIBLE __attribute__((pure))
541 # define ATTRIBUTE_REPRODUCIBLE /* empty */
545 #if HAVE___HAS_C_ATTRIBUTE
546 # if __has_c_attribute(unsequenced)
547 # define ATTRIBUTE_UNSEQUENCED [[unsequenced]]
550 #ifndef ATTRIBUTE_UNSEQUENCED
552 # define ATTRIBUTE_UNSEQUENCED __attribute__((const))
554 # define ATTRIBUTE_UNSEQUENCED /* empty */
558 #if (__STDC_VERSION__ < 199901 && !defined restrict \
559 && (PORT_TO_C89 || defined _MSC_VER))
560 # define restrict /* empty */
564 ** Workarounds for compilers/systems.
568 # define EPOCH_LOCAL 0
571 # define EPOCH_OFFSET 0
573 #ifndef RESERVE_STD_EXT_IDS
574 # define RESERVE_STD_EXT_IDS 0
577 /* If standard C identifiers with external linkage (e.g., localtime)
578 are reserved and are not already being renamed anyway, rename them
579 as if compiling with '-Dtime_tz=time_t'. */
580 #if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
581 # define time_tz time_t
585 ** Compile with -Dtime_tz=T to build the tz package with a private
586 ** time_t type equivalent to T rather than the system-supplied time_t.
587 ** This debugging feature can test unusual design decisions
588 ** (e.g., time_t wider than 'long', or unsigned time_t) even on
589 ** typical platforms.
591 #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
597 #if defined LOCALTIME_IMPLEMENTATION && TZ_TIME_T
598 static time_t sys_time(time_t *x
) { return time(x
); }
603 typedef time_tz tz_time_t
;
606 # define asctime tz_asctime
608 # define asctime_r tz_asctime_r
610 # define ctime tz_ctime
612 # define ctime_r tz_ctime_r
614 # define difftime tz_difftime
616 # define gmtime tz_gmtime
618 # define gmtime_r tz_gmtime_r
620 # define localtime tz_localtime
622 # define localtime_r tz_localtime_r
624 # define localtime_rz tz_localtime_rz
626 # define mktime tz_mktime
628 # define mktime_z tz_mktime_z
630 # define offtime tz_offtime
632 # define posix2time tz_posix2time
634 # define posix2time_z tz_posix2time_z
636 # define strftime tz_strftime
638 # define time tz_time
640 # define time2posix tz_time2posix
642 # define time2posix_z tz_time2posix_z
644 # define time_t tz_time_t
646 # define timegm tz_timegm
648 # define timelocal tz_timelocal
650 # define timeoff tz_timeoff
652 # define tzalloc tz_tzalloc
654 # define tzfree tz_tzfree
656 # define tzset tz_tzset
659 # define strftime_l tz_strftime_l
663 # define tzname tz_tzname
667 # define daylight tz_daylight
669 # define timezone tz_timezone
673 # define altzone tz_altzone
676 # if __STDC_VERSION__ < 202311
677 # define DEPRECATED_IN_C23 /* empty */
679 # define DEPRECATED_IN_C23 ATTRIBUTE_DEPRECATED
681 DEPRECATED_IN_C23
char *asctime(struct tm
const *);
682 char *asctime_r(struct tm
const *restrict
, char *restrict
);
683 DEPRECATED_IN_C23
char *ctime(time_t const *);
684 char *ctime_r(time_t const *, char *);
685 ATTRIBUTE_UNSEQUENCED
double difftime(time_t, time_t);
686 size_t strftime(char *restrict
, size_t, char const *restrict
,
687 struct tm
const *restrict
);
689 size_t strftime_l(char *restrict
, size_t, char const *restrict
,
690 struct tm
const *restrict
, locale_t
);
692 struct tm
*gmtime(time_t const *);
693 struct tm
*gmtime_r(time_t const *restrict
, struct tm
*restrict
);
694 struct tm
*localtime(time_t const *);
695 struct tm
*localtime_r(time_t const *restrict
, struct tm
*restrict
);
696 time_t mktime(struct tm
*);
697 time_t time(time_t *);
698 time_t timegm(struct tm
*);
702 #ifndef HAVE_DECL_TIMEGM
703 # if (202311 <= __STDC_VERSION__ \
704 || defined __GLIBC__ || defined __tm_zone /* musl */ \
705 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
706 || (defined __APPLE__ && defined __MACH__))
707 # define HAVE_DECL_TIMEGM true
709 # define HAVE_DECL_TIMEGM false
712 #if !HAVE_DECL_TIMEGM && !defined timegm
713 time_t timegm(struct tm
*);
716 #if !HAVE_DECL_ASCTIME_R && !defined asctime_r
717 extern char *asctime_r(struct tm
const *restrict
, char *restrict
);
720 #ifndef HAVE_DECL_ENVIRON
721 # if defined environ || defined __USE_GNU
722 # define HAVE_DECL_ENVIRON 1
724 # define HAVE_DECL_ENVIRON 0
728 #if !HAVE_DECL_ENVIRON
729 extern char **environ
;
732 #if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
733 extern char *tzname
[];
735 #if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
736 extern long timezone
;
739 #if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
744 ** The STD_INSPIRED functions are similar, but most also need
745 ** declarations if time_tz is defined.
749 # define STD_INSPIRED 0
752 # if TZ_TIME_T || !defined offtime
753 struct tm
*offtime(time_t const *, long);
755 # if TZ_TIME_T || !defined timelocal
756 time_t timelocal(struct tm
*);
758 # if TZ_TIME_T || !defined timeoff
759 # define EXTERN_TIMEOFF
761 # if TZ_TIME_T || !defined time2posix
762 time_t time2posix(time_t);
764 # if TZ_TIME_T || !defined posix2time
765 time_t posix2time(time_t);
769 /* Infer TM_ZONE on systems where this information is known, but suppress
770 guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
771 #if (200809 < _POSIX_VERSION \
772 || defined __GLIBC__ \
773 || defined __tm_zone /* musl */ \
774 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
775 || (defined __APPLE__ && defined __MACH__))
776 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
777 # define TM_GMTOFF tm_gmtoff
779 # if !defined TM_ZONE && !defined NO_TM_ZONE
780 # define TM_ZONE tm_zone
785 ** Define functions that are ABI compatible with NetBSD but have
786 ** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
787 ** and labors under the misconception that 'const timezone_t' is a
788 ** pointer to a constant. This use of 'const' is ineffective, so it
789 ** is not done here. What we call 'struct state' NetBSD calls
790 ** 'struct __state', but this is a private name so it doesn't matter.
793 typedef struct state
*timezone_t
;
794 struct tm
*localtime_rz(timezone_t restrict
, time_t const *restrict
,
795 struct tm
*restrict
);
796 time_t mktime_z(timezone_t restrict
, struct tm
*restrict
);
797 timezone_t
tzalloc(char const *);
798 void tzfree(timezone_t
);
800 # if TZ_TIME_T || !defined posix2time_z
801 ATTRIBUTE_REPRODUCIBLE
time_t posix2time_z(timezone_t
, time_t);
803 # if TZ_TIME_T || !defined time2posix_z
804 ATTRIBUTE_REPRODUCIBLE
time_t time2posix_z(timezone_t
, time_t);
810 ** Finally, some convenience items.
813 #define TYPE_BIT(type) (CHAR_BIT * (ptrdiff_t) sizeof(type))
814 #define TYPE_SIGNED(type) (((type) -1) < 0)
815 #define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
817 /* Minimum and maximum of two values. Use lower case to avoid
818 naming clashes with standard include files. */
819 #define max(a, b) ((a) > (b) ? (a) : (b))
820 #define min(a, b) ((a) < (b) ? (a) : (b))
822 /* Max and min values of the integer type T, of which only the bottom
823 B bits are used, and where the highest-order used bit is considered
824 to be a sign bit if T is signed. */
825 #define MAXVAL(t, b) \
826 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
827 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
828 #define MINVAL(t, b) \
829 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
831 /* The extreme time values, assuming no padding. */
832 #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
833 #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
835 /* The extreme time values. These are macros, not constants, so that
836 any portability problems occur only when compiling .c files that use
837 the macros, which is safer for applications that need only zdump and zic.
838 This implementation assumes no padding if time_t is signed and
839 either the compiler lacks support for _Generic or time_t is not one
840 of the standard signed integer types. */
842 # define TIME_T_MIN \
843 _Generic((time_t) 0, \
844 signed char: SCHAR_MIN, short: SHRT_MIN, \
845 int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
846 default: TIME_T_MIN_NO_PADDING)
847 # define TIME_T_MAX \
848 (TYPE_SIGNED(time_t) \
849 ? _Generic((time_t) 0, \
850 signed char: SCHAR_MAX, short: SHRT_MAX, \
851 int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
852 default: TIME_T_MAX_NO_PADDING) \
854 enum { SIGNED_PADDING_CHECK_NEEDED
855 = _Generic((time_t) 0,
856 signed char: false, short: false,
857 int: false, long: false, long long: false,
860 # define TIME_T_MIN TIME_T_MIN_NO_PADDING
861 # define TIME_T_MAX TIME_T_MAX_NO_PADDING
862 enum { SIGNED_PADDING_CHECK_NEEDED
= true };
864 /* Try to check the padding assumptions. Although TIME_T_MAX and the
865 following check can both have undefined behavior on oddball
866 platforms due to shifts exceeding widths of signed integers, these
867 platforms' compilers are likely to diagnose these issues in integer
868 constant expressions, so it shouldn't hurt to check statically. */
869 static_assert(! TYPE_SIGNED(time_t) || ! SIGNED_PADDING_CHECK_NEEDED
870 || TIME_T_MAX
>> (TYPE_BIT(time_t) - 2) == 1);
873 ** 302 / 1000 is log10(2.0) rounded up.
874 ** Subtract one for the sign bit if the type is signed;
875 ** add one for integer division truncation;
876 ** add one more for a minus sign if the type is signed.
878 #define INT_STRLEN_MAXIMUM(type) \
879 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
880 1 + TYPE_SIGNED(type))
887 # define INITIALIZE(x) ((x) = 0)
889 # define INITIALIZE(x)
892 /* Whether memory access must strictly follow the C standard.
893 If 0, it's OK to read uninitialized storage so long as the value is
894 not relied upon. Defining it to 0 lets mktime access parts of
895 struct tm that might be uninitialized, as a heuristic when the
896 standard doesn't say what to return and when tm_gmtoff can help
897 mktime likely infer a better value. */
899 # define UNINIT_TRAP 0
902 /* localtime.c sometimes needs access to timeoff if it is not already public.
903 tz_private_timeoff should be used only by localtime.c. */
904 #if (!defined EXTERN_TIMEOFF \
905 && defined TM_GMTOFF && (200809 < _POSIX_VERSION || ! UNINIT_TRAP))
907 # define timeoff tz_private_timeoff
909 # define EXTERN_TIMEOFF
911 #ifdef EXTERN_TIMEOFF
912 time_t timeoff(struct tm
*, long);
917 # define unreachable() abort()
918 #elif !defined unreachable
919 # ifdef __has_builtin
920 # if __has_builtin(__builtin_unreachable)
921 # define unreachable() __builtin_unreachable()
923 # elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
924 # define unreachable() __builtin_unreachable()
927 # define unreachable() ((void) 0)
932 ** For the benefit of GNU folk...
933 ** '_(MSGID)' uses the current locale's message library string for MSGID.
934 ** The default is to use gettext if available, and use MSGID otherwise.
938 #define _(msgid) gettext(msgid)
939 #else /* !HAVE_GETTEXT */
940 #define _(msgid) msgid
941 #endif /* !HAVE_GETTEXT */
943 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
944 # define TZ_DOMAIN "tz"
947 #if HAVE_INCOMPATIBLE_CTIME_R
950 char *asctime_r(struct tm
const *restrict
, char *restrict
);
951 char *ctime_r(time_t const *, char *);
952 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
954 /* Handy macros that are independent of tzfile implementation. */
959 SECSPERHOUR
= SECSPERMIN
* MINSPERHOUR
,
963 DAYSPERLYEAR
= DAYSPERNYEAR
+ 1,
965 YEARSPERREPEAT
= 400 /* years before a Gregorian repeat */
968 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
970 #define DAYSPERREPEAT ((int_fast32_t) 400 * 365 + 100 - 4 + 1)
971 #define SECSPERREPEAT ((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
972 #define AVGSECSPERYEAR (SECSPERREPEAT / YEARSPERREPEAT)
974 /* How many years to generate (in zic.c) or search through (in localtime.c).
975 This is two years larger than the obvious 400, to avoid edge cases.
976 E.g., suppose a non-POSIX.1-2017 rule applies from 2012 on with transitions
977 in March and September, plus one-off transitions in November 2013.
978 If zic looked only at the last 400 years, it would set max_year=2413,
979 with the intent that the 400 years 2014 through 2413 will be repeated.
980 The last transition listed in the tzfile would be in 2413-09,
981 less than 400 years after the last one-off transition in 2013-11.
982 Two years is not overkill for localtime.c, as a one-year bump
983 would mishandle 2023d's America/Ciudad_Juarez for November 2422. */
984 enum { years_of_observations
= YEARSPERREPEAT
+ 2 };
1012 TM_YEAR_BASE
= 1900,
1013 TM_WDAY_BASE
= TM_MONDAY
,
1015 EPOCH_WDAY
= TM_THURSDAY
1018 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
1021 ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
1022 ** isleap(y) == isleap(y % 400)
1024 ** isleap(a + b) == isleap((a + b) % 400)
1026 ** isleap(a + b) == isleap(a % 400 + b % 400)
1027 ** This is true even if % means modulo rather than Fortran remainder
1028 ** (which is allowed by C89 but not by C99 or later).
1029 ** We use this to avoid addition overflow problems.
1032 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
1034 #endif /* !defined PRIVATE_H */