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.
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.
30 /* This string was in the Factory zone through version 2016f. */
31 #define GRANDPARENTED "Local time zone must be set--see zic manual page"
34 ** Defaults for preprocessor symbols.
35 ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
38 #ifndef HAVE_DECL_ASCTIME_R
39 #define HAVE_DECL_ASCTIME_R 1
42 #if !defined HAVE_GENERIC && defined __has_extension
43 # if __has_extension(c_generic_selections)
44 # define HAVE_GENERIC 1
46 # define HAVE_GENERIC 0
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__))
54 # define HAVE_GENERIC (201112 <= __STDC_VERSION__)
58 #define HAVE_GETTEXT 0
59 #endif /* !defined HAVE_GETTEXT */
61 #ifndef HAVE_INCOMPATIBLE_CTIME_R
62 #define HAVE_INCOMPATIBLE_CTIME_R 0
67 #endif /* !defined HAVE_LINK */
69 #ifndef HAVE_POSIX_DECLS
70 #define HAVE_POSIX_DECLS 1
73 #ifndef HAVE_STDBOOL_H
74 #define HAVE_STDBOOL_H (199901 <= __STDC_VERSION__)
82 #define HAVE_STRTOLL 1
86 #define HAVE_SYMLINK 1
87 #endif /* !defined HAVE_SYMLINK */
89 #ifndef HAVE_SYS_STAT_H
90 #define HAVE_SYS_STAT_H 1
91 #endif /* !defined HAVE_SYS_STAT_H */
93 #ifndef HAVE_SYS_WAIT_H
94 #define HAVE_SYS_WAIT_H 1
95 #endif /* !defined HAVE_SYS_WAIT_H */
98 #define HAVE_UNISTD_H 1
99 #endif /* !defined HAVE_UNISTD_H */
102 #define HAVE_UTMPX_H 1
103 #endif /* !defined HAVE_UTMPX_H */
105 #ifndef NETBSD_INSPIRED
106 # define NETBSD_INSPIRED 1
109 #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 */
114 /* Enable tm_gmtoff, tm_zone, and environ on GNUish systems. */
115 #define _GNU_SOURCE 1
116 /* Fix asctime_r on Solaris 11. */
117 #define _POSIX_PTHREAD_SEMANTICS 1
118 /* Enable strtoimax on pre-C99 Solaris 11. */
119 #define __EXTENSIONS__ 1
121 /* 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
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
135 /* Avoid clashes with NetBSD by renaming NetBSD's declarations. */
136 #define localtime_rz sys_localtime_rz
137 #define mktime_z sys_mktime_z
138 #define posix2time_z sys_posix2time_z
139 #define time2posix_z sys_time2posix_z
140 #define timezone_t sys_timezone_t
141 #define tzalloc sys_tzalloc
142 #define tzfree sys_tzfree
152 #include <sys/types.h> /* for time_t */
154 #include <limits.h> /* for CHAR_BIT et al. */
160 # define ENAMETOOLONG EINVAL
163 # define ENOTSUP EINVAL
166 # define EOVERFLOW EINVAL
171 #endif /* HAVE_GETTEXT */
174 #include <unistd.h> /* for R_OK, and other POSIX goodness */
175 #endif /* HAVE_UNISTD_H */
177 #ifndef HAVE_STRFTIME_L
178 # if _POSIX_VERSION < 200809
179 # define HAVE_STRFTIME_L 0
181 # define HAVE_STRFTIME_L 1
186 # ifndef _XOPEN_VERSION
187 # define USG_COMPAT 0
189 # define USG_COMPAT 1
194 # if _POSIX_VERSION < 198808 && !USG_COMPAT
195 # define HAVE_TZNAME 0
197 # define HAVE_TZNAME 1
203 #endif /* !defined R_OK */
205 /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
206 #define is_digit(c) ((unsigned)(c) - '0' <= 9)
209 ** Define HAVE_STDINT_H's default value here, rather than at the
210 ** start, since __GLIBC__ and INTMAX_MAX's values depend on
211 ** previously-included files. glibc 2.1 and Solaris 10 and later have
212 ** stdint.h, even with pre-C99 compilers.
214 #ifndef HAVE_STDINT_H
215 #define HAVE_STDINT_H \
216 (199901 <= __STDC_VERSION__ \
217 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
218 || __CYGWIN__ || INTMAX_MAX)
219 #endif /* !defined HAVE_STDINT_H */
223 #endif /* !HAVE_STDINT_H */
225 #ifndef HAVE_INTTYPES_H
226 # define HAVE_INTTYPES_H HAVE_STDINT_H
229 # include <inttypes.h>
232 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
233 #ifdef __LONG_LONG_MAX__
235 # define LLONG_MAX __LONG_LONG_MAX__
238 # define LLONG_MIN (-1 - LLONG_MAX)
242 #ifndef INT_FAST64_MAX
244 typedef long long int_fast64_t;
245 # define INT_FAST64_MIN LLONG_MIN
246 # define INT_FAST64_MAX LLONG_MAX
248 # if LONG_MAX >> 31 < 0xffffffff
249 Please use a compiler that supports a
64-bit integer
type (or wider
);
250 you may need to compile with
"-DHAVE_STDINT_H".
252 typedef long int_fast64_t;
253 # define INT_FAST64_MIN LONG_MIN
254 # define INT_FAST64_MAX LONG_MAX
259 # if INT_FAST64_MAX == LLONG_MAX
260 # define PRIdFAST64 "lld"
262 # define PRIdFAST64 "ld"
267 # define SCNdFAST64 PRIdFAST64
270 #ifndef INT_FAST32_MAX
271 # if INT_MAX >> 31 == 0
272 typedef long int_fast32_t;
273 # define INT_FAST32_MAX LONG_MAX
274 # define INT_FAST32_MIN LONG_MIN
276 typedef int int_fast32_t;
277 # define INT_FAST32_MAX INT_MAX
278 # define INT_FAST32_MIN INT_MIN
284 typedef long long intmax_t;
286 # define strtoimax strtoll
288 # define INTMAX_MAX LLONG_MAX
289 # define INTMAX_MIN LLONG_MIN
291 typedef long intmax_t;
292 # define INTMAX_MAX LONG_MAX
293 # define INTMAX_MIN LONG_MIN
296 # define strtoimax strtol
301 # if INTMAX_MAX == LLONG_MAX
302 # define PRIdMAX "lld"
304 # define PRIdMAX "ld"
308 #ifndef UINT_FAST64_MAX
309 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
310 typedef unsigned long long uint_fast64_t;
312 # if ULONG_MAX >> 31 >> 1 < 0xffffffff
313 Please use a compiler that supports a
64-bit integer
type (or wider
);
314 you may need to compile with
"-DHAVE_STDINT_H".
316 typedef unsigned long uint_fast64_t;
321 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
322 typedef unsigned long long uintmax_t;
324 typedef unsigned long uintmax_t;
329 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
330 # define PRIuMAX "llu"
332 # define PRIuMAX "lu"
337 #define INT32_MAX 0x7fffffff
338 #endif /* !defined INT32_MAX */
340 #define INT32_MIN (-1 - INT32_MAX)
341 #endif /* !defined INT32_MIN */
344 #define SIZE_MAX ((size_t) -1)
348 # define ATTRIBUTE_CONST __attribute__ ((const))
349 # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
350 # define ATTRIBUTE_PURE __attribute__ ((__pure__))
351 # define ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
353 # define ATTRIBUTE_CONST /* empty */
354 # define ATTRIBUTE_MALLOC /* empty */
355 # define ATTRIBUTE_PURE /* empty */
356 # define ATTRIBUTE_FORMAT(spec) /* empty */
359 #if !defined _Noreturn && __STDC_VERSION__ < 201112
360 # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
361 # define _Noreturn __attribute__ ((__noreturn__))
367 #if __STDC_VERSION__ < 199901 && !defined restrict
368 # define restrict /* empty */
372 ** Workarounds for compilers/systems.
376 # define EPOCH_LOCAL 0
379 # define EPOCH_OFFSET 0
381 #ifndef RESERVE_STD_EXT_IDS
382 # define RESERVE_STD_EXT_IDS 0
385 /* If standard C identifiers with external linkage (e.g., localtime)
386 are reserved and are not already being renamed anyway, rename them
387 as if compiling with '-Dtime_tz=time_t'. */
388 #if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
389 # define time_tz time_t
393 ** Compile with -Dtime_tz=T to build the tz package with a private
394 ** time_t type equivalent to T rather than the system-supplied time_t.
395 ** This debugging feature can test unusual design decisions
396 ** (e.g., time_t wider than 'long', or unsigned time_t) even on
397 ** typical platforms.
399 #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
406 # ifdef LOCALTIME_IMPLEMENTATION
407 static time_t sys_time(time_t *x
) { return time(x
); }
410 typedef time_tz tz_time_t
;
413 # define ctime tz_ctime
415 # define ctime_r tz_ctime_r
417 # define difftime tz_difftime
419 # define gmtime tz_gmtime
421 # define gmtime_r tz_gmtime_r
423 # define localtime tz_localtime
425 # define localtime_r tz_localtime_r
427 # define localtime_rz tz_localtime_rz
429 # define mktime tz_mktime
431 # define mktime_z tz_mktime_z
433 # define offtime tz_offtime
435 # define posix2time tz_posix2time
437 # define posix2time_z tz_posix2time_z
439 # define strftime tz_strftime
441 # define time tz_time
443 # define time2posix tz_time2posix
445 # define time2posix_z tz_time2posix_z
447 # define time_t tz_time_t
449 # define timegm tz_timegm
451 # define timelocal tz_timelocal
453 # define timeoff tz_timeoff
455 # define tzalloc tz_tzalloc
457 # define tzfree tz_tzfree
459 # define tzset tz_tzset
461 # define tzsetwall tz_tzsetwall
464 # define strftime_l tz_strftime_l
468 # define tzname tz_tzname
472 # define daylight tz_daylight
474 # define timezone tz_timezone
478 # define altzone tz_altzone
481 char *ctime(time_t const *);
482 char *ctime_r(time_t const *, char *);
483 double difftime(time_t, time_t) ATTRIBUTE_CONST
;
484 size_t strftime(char *restrict
, size_t, char const *restrict
,
485 struct tm
const *restrict
);
487 size_t strftime_l(char *restrict
, size_t, char const *restrict
,
488 struct tm
const *restrict
, locale_t
);
490 struct tm
*gmtime(time_t const *);
491 struct tm
*gmtime_r(time_t const *restrict
, struct tm
*restrict
);
492 struct tm
*localtime(time_t const *);
493 struct tm
*localtime_r(time_t const *restrict
, struct tm
*restrict
);
494 time_t mktime(struct tm
*);
495 time_t time(time_t *);
499 #if !HAVE_DECL_ASCTIME_R && !defined asctime_r
500 extern char *asctime_r(struct tm
const *restrict
, char *restrict
);
503 #ifndef HAVE_DECL_ENVIRON
504 # if defined environ || defined __USE_GNU
505 # define HAVE_DECL_ENVIRON 1
507 # define HAVE_DECL_ENVIRON 0
511 #if !HAVE_DECL_ENVIRON
512 extern char **environ
;
515 #if TZ_TIME_T || !HAVE_POSIX_DECLS
517 extern char *tzname
[];
520 extern long timezone
;
530 ** The STD_INSPIRED functions are similar, but most also need
531 ** declarations if time_tz is defined.
535 # if TZ_TIME_T || !defined tzsetwall
536 void tzsetwall(void);
538 # if TZ_TIME_T || !defined offtime
539 struct tm
*offtime(time_t const *, long);
541 # if TZ_TIME_T || !defined timegm
542 time_t timegm(struct tm
*);
544 # if TZ_TIME_T || !defined timelocal
545 time_t timelocal(struct tm
*);
547 # if TZ_TIME_T || !defined timeoff
548 time_t timeoff(struct tm
*, long);
550 # if TZ_TIME_T || !defined time2posix
551 time_t time2posix(time_t);
553 # if TZ_TIME_T || !defined posix2time
554 time_t posix2time(time_t);
558 /* Infer TM_ZONE on systems where this information is known, but suppress
559 guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
560 #if (defined __GLIBC__ \
561 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
562 || (defined __APPLE__ && defined __MACH__))
563 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
564 # define TM_GMTOFF tm_gmtoff
566 # if !defined TM_ZONE && !defined NO_TM_ZONE
567 # define TM_ZONE tm_zone
572 ** Define functions that are ABI compatible with NetBSD but have
573 ** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
574 ** and labors under the misconception that 'const timezone_t' is a
575 ** pointer to a constant. This use of 'const' is ineffective, so it
576 ** is not done here. What we call 'struct state' NetBSD calls
577 ** 'struct __state', but this is a private name so it doesn't matter.
580 typedef struct state
*timezone_t
;
581 struct tm
*localtime_rz(timezone_t restrict
, time_t const *restrict
,
582 struct tm
*restrict
);
583 time_t mktime_z(timezone_t restrict
, struct tm
*restrict
);
584 timezone_t
tzalloc(char const *);
585 void tzfree(timezone_t
);
587 # if TZ_TIME_T || !defined posix2time_z
588 time_t posix2time_z(timezone_t
, time_t) ATTRIBUTE_PURE
;
590 # if TZ_TIME_T || !defined time2posix_z
591 time_t time2posix_z(timezone_t
, time_t) ATTRIBUTE_PURE
;
597 ** Finally, some convenience items.
601 # include <stdbool.h>
608 #define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
609 #define TYPE_SIGNED(type) (((type) -1) < 0)
610 #define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
612 /* Max and min values of the integer type T, of which only the bottom
613 B bits are used, and where the highest-order used bit is considered
614 to be a sign bit if T is signed. */
615 #define MAXVAL(t, b) \
616 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
617 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
618 #define MINVAL(t, b) \
619 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
621 /* The extreme time values, assuming no padding. */
622 #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
623 #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
625 /* The extreme time values. These are macros, not constants, so that
626 any portability problem occur only when compiling .c files that use
627 the macros, which is safer for applications that need only zdump and zic.
628 This implementation assumes no padding if time_t is signed and
629 either the compiler lacks support for _Generic or time_t is not one
630 of the standard signed integer types. */
632 # define TIME_T_MIN \
633 _Generic((time_t) 0, \
634 signed char: SCHAR_MIN, short: SHRT_MIN, \
635 int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
636 default: TIME_T_MIN_NO_PADDING)
637 # define TIME_T_MAX \
638 (TYPE_SIGNED(time_t) \
639 ? _Generic((time_t) 0, \
640 signed char: SCHAR_MAX, short: SHRT_MAX, \
641 int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
642 default: TIME_T_MAX_NO_PADDING) \
645 # define TIME_T_MIN TIME_T_MIN_NO_PADDING
646 # define TIME_T_MAX TIME_T_MAX_NO_PADDING
650 ** 302 / 1000 is log10(2.0) rounded up.
651 ** Subtract one for the sign bit if the type is signed;
652 ** add one for integer division truncation;
653 ** add one more for a minus sign if the type is signed.
655 #define INT_STRLEN_MAXIMUM(type) \
656 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
657 1 + TYPE_SIGNED(type))
664 # define INITIALIZE(x) ((x) = 0)
666 # define INITIALIZE(x)
670 # define UNINIT_TRAP 0
674 ** For the benefit of GNU folk...
675 ** '_(MSGID)' uses the current locale's message library string for MSGID.
676 ** The default is to use gettext if available, and use MSGID otherwise.
680 #define _(msgid) gettext(msgid)
681 #else /* !HAVE_GETTEXT */
682 #define _(msgid) msgid
683 #endif /* !HAVE_GETTEXT */
685 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
686 # define TZ_DOMAIN "tz"
689 #if HAVE_INCOMPATIBLE_CTIME_R
692 char *asctime_r(struct tm
const *, char *);
693 char *ctime_r(time_t const *, char *);
694 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
696 /* Handy macros that are independent of tzfile implementation. */
698 #define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
700 #define SECSPERMIN 60
701 #define MINSPERHOUR 60
702 #define HOURSPERDAY 24
703 #define DAYSPERWEEK 7
704 #define DAYSPERNYEAR 365
705 #define DAYSPERLYEAR 366
706 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
707 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
708 #define MONSPERYEAR 12
713 #define TM_WEDNESDAY 3
714 #define TM_THURSDAY 4
716 #define TM_SATURDAY 6
719 #define TM_FEBRUARY 1
726 #define TM_SEPTEMBER 8
728 #define TM_NOVEMBER 10
729 #define TM_DECEMBER 11
731 #define TM_YEAR_BASE 1900
733 #define EPOCH_YEAR 1970
734 #define EPOCH_WDAY TM_THURSDAY
736 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
739 ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
740 ** isleap(y) == isleap(y % 400)
742 ** isleap(a + b) == isleap((a + b) % 400)
744 ** isleap(a + b) == isleap(a % 400 + b % 400)
745 ** This is true even if % means modulo rather than Fortran remainder
746 ** (which is allowed by C89 but not by C99 or later).
747 ** We use this to avoid addition overflow problems.
750 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
754 ** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
757 #define AVGSECSPERYEAR 31556952L
758 #define SECSPERREPEAT \
759 ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
760 #define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */
762 #endif /* !defined PRIVATE_H */