6 ** This file is in the public domain, so clarified as of
7 ** 1996-06-05 by Arthur David Olson.
11 ** This header is for use ONLY with the time conversion code.
12 ** There is no guarantee that it will remain unchanged,
13 ** or that it will remain at all.
14 ** Do NOT copy it to any system include directory.
18 /* This string was in the Factory zone through version 2016f. */
19 #define GRANDPARENTED "Local time zone must be set--see zic manual page"
22 ** Defaults for preprocessor symbols.
23 ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
26 #ifndef HAVE_DECL_ASCTIME_R
27 #define HAVE_DECL_ASCTIME_R 1
30 #if !defined HAVE_GENERIC && defined __has_extension
31 # if __has_extension(c_generic_selections)
32 # define HAVE_GENERIC 1
34 # define HAVE_GENERIC 0
37 /* _Generic is buggy in pre-4.9 GCC. */
38 #if !defined HAVE_GENERIC && defined __GNUC__
39 # define HAVE_GENERIC (4 < __GNUC__ + (9 <= __GNUC_MINOR__))
42 # define HAVE_GENERIC (201112 <= __STDC_VERSION__)
46 #define HAVE_GETTEXT 0
47 #endif /* !defined HAVE_GETTEXT */
49 #ifndef HAVE_INCOMPATIBLE_CTIME_R
50 #define HAVE_INCOMPATIBLE_CTIME_R 0
55 #endif /* !defined HAVE_LINK */
57 #ifndef HAVE_POSIX_DECLS
58 #define HAVE_POSIX_DECLS 1
61 #ifndef HAVE_STDBOOL_H
62 #define HAVE_STDBOOL_H (199901 <= __STDC_VERSION__)
70 #define HAVE_SYMLINK 1
71 #endif /* !defined HAVE_SYMLINK */
73 #ifndef HAVE_SYS_STAT_H
74 #define HAVE_SYS_STAT_H 1
75 #endif /* !defined HAVE_SYS_STAT_H */
77 #ifndef HAVE_SYS_WAIT_H
78 #define HAVE_SYS_WAIT_H 1
79 #endif /* !defined HAVE_SYS_WAIT_H */
82 #define HAVE_UNISTD_H 1
83 #endif /* !defined HAVE_UNISTD_H */
86 #define HAVE_UTMPX_H 1
87 #endif /* !defined HAVE_UTMPX_H */
89 #ifndef NETBSD_INSPIRED
90 # define NETBSD_INSPIRED 1
93 #if HAVE_INCOMPATIBLE_CTIME_R
94 #define asctime_r _incompatible_asctime_r
95 #define ctime_r _incompatible_ctime_r
96 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
98 /* Enable tm_gmtoff, tm_zone, and environ on GNUish systems. */
100 /* Fix asctime_r on Solaris 11. */
101 #define _POSIX_PTHREAD_SEMANTICS 1
102 /* Enable strtoimax on pre-C99 Solaris 11. */
103 #define __EXTENSIONS__ 1
105 /* To avoid having 'stat' fail unnecessarily with errno == EOVERFLOW,
106 enable large files on GNUish systems ... */
107 #ifndef _FILE_OFFSET_BITS
108 # define _FILE_OFFSET_BITS 64
110 /* ... and on AIX ... */
111 #define _LARGE_FILES 1
112 /* ... and enable large inode numbers on Mac OS X 10.5 and later. */
113 #define _DARWIN_USE_64_BIT_INODE 1
119 /* Avoid clashes with NetBSD by renaming NetBSD's declarations. */
120 #define localtime_rz sys_localtime_rz
121 #define mktime_z sys_mktime_z
122 #define posix2time_z sys_posix2time_z
123 #define time2posix_z sys_time2posix_z
124 #define timezone_t sys_timezone_t
125 #define tzalloc sys_tzalloc
126 #define tzfree sys_tzfree
136 #include <sys/types.h> /* for time_t */
138 #include <limits.h> /* for CHAR_BIT et al. */
144 # define ENAMETOOLONG EINVAL
147 # define ENOTSUP EINVAL
150 # define EOVERFLOW EINVAL
155 #endif /* HAVE_GETTEXT */
158 #include <unistd.h> /* for R_OK, and other POSIX goodness */
159 #endif /* HAVE_UNISTD_H */
161 #ifndef HAVE_STRFTIME_L
162 # if _POSIX_VERSION < 200809
163 # define HAVE_STRFTIME_L 0
165 # define HAVE_STRFTIME_L 1
170 # ifndef _XOPEN_VERSION
171 # define USG_COMPAT 0
173 # define USG_COMPAT 1
178 # if _POSIX_VERSION < 198808 && !USG_COMPAT
179 # define HAVE_TZNAME 0
181 # define HAVE_TZNAME 1
187 #endif /* !defined R_OK */
189 /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
190 #define is_digit(c) ((unsigned)(c) - '0' <= 9)
193 ** Define HAVE_STDINT_H's default value here, rather than at the
194 ** start, since __GLIBC__ and INTMAX_MAX's values depend on
195 ** previously-included files. glibc 2.1 and Solaris 10 and later have
196 ** stdint.h, even with pre-C99 compilers.
198 #ifndef HAVE_STDINT_H
199 #define HAVE_STDINT_H \
200 (199901 <= __STDC_VERSION__ \
201 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
202 || __CYGWIN__ || INTMAX_MAX)
203 #endif /* !defined HAVE_STDINT_H */
207 #endif /* !HAVE_STDINT_H */
209 #ifndef HAVE_INTTYPES_H
210 # define HAVE_INTTYPES_H HAVE_STDINT_H
213 # include <inttypes.h>
216 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
217 #ifdef __LONG_LONG_MAX__
219 # define LLONG_MAX __LONG_LONG_MAX__
222 # define LLONG_MIN (-1 - LLONG_MAX)
226 #ifndef INT_FAST64_MAX
228 typedef long long int_fast64_t;
229 # define INT_FAST64_MIN LLONG_MIN
230 # define INT_FAST64_MAX LLONG_MAX
232 # if LONG_MAX >> 31 < 0xffffffff
233 Please use a compiler that supports a 64-bit integer type (or wider);
234 you may need to compile with "-DHAVE_STDINT_H".
236 typedef long int_fast64_t;
237 # define INT_FAST64_MIN LONG_MIN
238 # define INT_FAST64_MAX LONG_MAX
243 # if INT_FAST64_MAX == LLONG_MAX
244 # define PRIdFAST64 "lld"
246 # define PRIdFAST64 "ld"
251 # define SCNdFAST64 PRIdFAST64
254 #ifndef INT_FAST32_MAX
255 # if INT_MAX >> 31 == 0
256 typedef long int_fast32_t;
257 # define INT_FAST32_MAX LONG_MAX
258 # define INT_FAST32_MIN LONG_MIN
260 typedef int int_fast32_t;
261 # define INT_FAST32_MAX INT_MAX
262 # define INT_FAST32_MIN INT_MIN
268 typedef long long intmax_t;
269 # define strtoimax strtoll
270 # define INTMAX_MAX LLONG_MAX
271 # define INTMAX_MIN LLONG_MIN
273 typedef long intmax_t;
274 # define strtoimax strtol
275 # define INTMAX_MAX LONG_MAX
276 # define INTMAX_MIN LONG_MIN
281 # if INTMAX_MAX == LLONG_MAX
282 # define PRIdMAX "lld"
284 # define PRIdMAX "ld"
288 #ifndef UINT_FAST64_MAX
289 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
290 typedef unsigned long long uint_fast64_t;
292 # if ULONG_MAX >> 31 >> 1 < 0xffffffff
293 Please use a compiler that supports a 64-bit integer type (or wider);
294 you may need to compile with "-DHAVE_STDINT_H".
296 typedef unsigned long uint_fast64_t;
301 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
302 typedef unsigned long long uintmax_t;
304 typedef unsigned long uintmax_t;
309 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
310 # define PRIuMAX "llu"
312 # define PRIuMAX "lu"
317 #define INT32_MAX 0x7fffffff
318 #endif /* !defined INT32_MAX */
320 #define INT32_MIN (-1 - INT32_MAX)
321 #endif /* !defined INT32_MIN */
324 #define SIZE_MAX ((size_t) -1)
327 #if 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
328 # define ATTRIBUTE_CONST __attribute__ ((const))
329 # define ATTRIBUTE_PURE __attribute__ ((__pure__))
330 # define ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
332 # define ATTRIBUTE_CONST /* empty */
333 # define ATTRIBUTE_PURE /* empty */
334 # define ATTRIBUTE_FORMAT(spec) /* empty */
337 #if !defined _Noreturn && __STDC_VERSION__ < 201112
338 # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
339 # define _Noreturn __attribute__ ((__noreturn__))
345 #if __STDC_VERSION__ < 199901 && !defined restrict
346 # define restrict /* empty */
350 ** Workarounds for compilers/systems.
354 # define EPOCH_LOCAL 0
357 # define EPOCH_OFFSET 0
361 ** Compile with -Dtime_tz=T to build the tz package with a private
362 ** time_t type equivalent to T rather than the system-supplied time_t.
363 ** This debugging feature can test unusual design decisions
364 ** (e.g., time_t wider than 'long', or unsigned time_t) even on
365 ** typical platforms.
367 #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
374 # ifdef LOCALTIME_IMPLEMENTATION
375 static time_t sys_time(time_t *x) { return time(x); }
378 typedef time_tz tz_time_t;
381 # define ctime tz_ctime
383 # define ctime_r tz_ctime_r
385 # define difftime tz_difftime
387 # define gmtime tz_gmtime
389 # define gmtime_r tz_gmtime_r
391 # define localtime tz_localtime
393 # define localtime_r tz_localtime_r
395 # define localtime_rz tz_localtime_rz
397 # define mktime tz_mktime
399 # define mktime_z tz_mktime_z
401 # define offtime tz_offtime
403 # define posix2time tz_posix2time
405 # define posix2time_z tz_posix2time_z
407 # define strftime tz_strftime
409 # define time tz_time
411 # define time2posix tz_time2posix
413 # define time2posix_z tz_time2posix_z
415 # define time_t tz_time_t
417 # define timegm tz_timegm
419 # define timelocal tz_timelocal
421 # define timeoff tz_timeoff
423 # define tzalloc tz_tzalloc
425 # define tzfree tz_tzfree
427 # define tzset tz_tzset
429 # define tzsetwall tz_tzsetwall
432 # define strftime_l tz_strftime_l
436 # define tzname tz_tzname
440 # define daylight tz_daylight
442 # define timezone tz_timezone
446 # define altzone tz_altzone
449 char *ctime(time_t const *);
450 char *ctime_r(time_t const *, char *);
451 double difftime(time_t, time_t) ATTRIBUTE_CONST;
452 size_t strftime(char *restrict, size_t, char const *restrict,
453 struct tm const *restrict);
455 size_t strftime_l(char *restrict, size_t, char const *restrict,
456 struct tm const *restrict, locale_t);
458 struct tm *gmtime(time_t const *);
459 struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
460 struct tm *localtime(time_t const *);
461 struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
462 time_t mktime(struct tm *);
463 time_t time(time_t *);
467 #if !HAVE_DECL_ASCTIME_R && !defined asctime_r
468 extern char *asctime_r(struct tm const *restrict, char *restrict);
471 #ifndef HAVE_DECL_ENVIRON
472 # if defined environ || defined __USE_GNU
473 # define HAVE_DECL_ENVIRON 1
475 # define HAVE_DECL_ENVIRON 0
479 #if !HAVE_DECL_ENVIRON
480 extern char **environ;
483 #if TZ_TIME_T || !HAVE_POSIX_DECLS
485 extern char *tzname[];
488 extern long timezone;
498 ** The STD_INSPIRED functions are similar, but most also need
499 ** declarations if time_tz is defined.
503 # if TZ_TIME_T || !defined tzsetwall
504 void tzsetwall(void);
506 # if TZ_TIME_T || !defined offtime
507 struct tm *offtime(time_t const *, long);
509 # if TZ_TIME_T || !defined timegm
510 time_t timegm(struct tm *);
512 # if TZ_TIME_T || !defined timelocal
513 time_t timelocal(struct tm *);
515 # if TZ_TIME_T || !defined timeoff
516 time_t timeoff(struct tm *, long);
518 # if TZ_TIME_T || !defined time2posix
519 time_t time2posix(time_t);
521 # if TZ_TIME_T || !defined posix2time
522 time_t posix2time(time_t);
526 /* Infer TM_ZONE on systems where this information is known, but suppress
527 guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
528 #if (defined __GLIBC__ \
529 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
530 || (defined __APPLE__ && defined __MACH__))
531 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
532 # define TM_GMTOFF tm_gmtoff
534 # if !defined TM_ZONE && !defined NO_TM_ZONE
535 # define TM_ZONE tm_zone
540 ** Define functions that are ABI compatible with NetBSD but have
541 ** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
542 ** and labors under the misconception that 'const timezone_t' is a
543 ** pointer to a constant. This use of 'const' is ineffective, so it
544 ** is not done here. What we call 'struct state' NetBSD calls
545 ** 'struct __state', but this is a private name so it doesn't matter.
548 typedef struct state *timezone_t;
549 struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
550 struct tm *restrict);
551 time_t mktime_z(timezone_t restrict, struct tm *restrict);
552 timezone_t tzalloc(char const *);
553 void tzfree(timezone_t);
555 # if TZ_TIME_T || !defined posix2time_z
556 time_t posix2time_z(timezone_t, time_t) ATTRIBUTE_PURE;
558 # if TZ_TIME_T || !defined time2posix_z
559 time_t time2posix_z(timezone_t, time_t) ATTRIBUTE_PURE;
565 ** Finally, some convenience items.
569 # include <stdbool.h>
576 #define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
577 #define TYPE_SIGNED(type) (((type) -1) < 0)
578 #define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
580 /* Max and min values of the integer type T, of which only the bottom
581 B bits are used, and where the highest-order used bit is considered
582 to be a sign bit if T is signed. */
583 #define MAXVAL(t, b) \
584 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
585 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
586 #define MINVAL(t, b) \
587 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
589 /* The extreme time values, assuming no padding. */
590 #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
591 #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
593 /* The extreme time values. These are macros, not constants, so that
594 any portability problem occur only when compiling .c files that use
595 the macros, which is safer for applications that need only zdump and zic.
596 This implementation assumes no padding if time_t is signed and
597 either the compiler lacks support for _Generic or time_t is not one
598 of the standard signed integer types. */
600 # define TIME_T_MIN \
601 _Generic((time_t) 0, \
602 signed char: SCHAR_MIN, short: SHRT_MIN, \
603 int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
604 default: TIME_T_MIN_NO_PADDING)
605 # define TIME_T_MAX \
606 (TYPE_SIGNED(time_t) \
607 ? _Generic((time_t) 0, \
608 signed char: SCHAR_MAX, short: SHRT_MAX, \
609 int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
610 default: TIME_T_MAX_NO_PADDING) \
613 # define TIME_T_MIN TIME_T_MIN_NO_PADDING
614 # define TIME_T_MAX TIME_T_MAX_NO_PADDING
618 ** 302 / 1000 is log10(2.0) rounded up.
619 ** Subtract one for the sign bit if the type is signed;
620 ** add one for integer division truncation;
621 ** add one more for a minus sign if the type is signed.
623 #define INT_STRLEN_MAXIMUM(type) \
624 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
625 1 + TYPE_SIGNED(type))
632 # define INITIALIZE(x) ((x) = 0)
634 # define INITIALIZE(x)
638 # define UNINIT_TRAP 0
642 ** For the benefit of GNU folk...
643 ** '_(MSGID)' uses the current locale's message library string for MSGID.
644 ** The default is to use gettext if available, and use MSGID otherwise.
648 #define _(msgid) gettext(msgid)
649 #else /* !HAVE_GETTEXT */
650 #define _(msgid) msgid
651 #endif /* !HAVE_GETTEXT */
653 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
654 # define TZ_DOMAIN "tz"
657 #if HAVE_INCOMPATIBLE_CTIME_R
660 char *asctime_r(struct tm const *, char *);
661 char *ctime_r(time_t const *, char *);
662 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
664 /* Handy macros that are independent of tzfile implementation. */
666 #define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
668 #define SECSPERMIN 60
669 #define MINSPERHOUR 60
670 #define HOURSPERDAY 24
671 #define DAYSPERWEEK 7
672 #define DAYSPERNYEAR 365
673 #define DAYSPERLYEAR 366
674 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
675 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
676 #define MONSPERYEAR 12
681 #define TM_WEDNESDAY 3
682 #define TM_THURSDAY 4
684 #define TM_SATURDAY 6
687 #define TM_FEBRUARY 1
694 #define TM_SEPTEMBER 8
696 #define TM_NOVEMBER 10
697 #define TM_DECEMBER 11
699 #define TM_YEAR_BASE 1900
701 #define EPOCH_YEAR 1970
702 #define EPOCH_WDAY TM_THURSDAY
704 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
707 ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
708 ** isleap(y) == isleap(y % 400)
710 ** isleap(a + b) == isleap((a + b) % 400)
712 ** isleap(a + b) == isleap(a % 400 + b % 400)
713 ** This is true even if % means modulo rather than Fortran remainder
714 ** (which is allowed by C89 but not by C99 or later).
715 ** We use this to avoid addition overflow problems.
718 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
722 ** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
725 #define AVGSECSPERYEAR 31556952L
726 #define SECSPERREPEAT \
727 ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
728 #define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */
730 #endif /* !defined PRIVATE_H */