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
31 #define HAVE_GETTEXT 0
32 #endif /* !defined HAVE_GETTEXT */
34 #ifndef HAVE_INCOMPATIBLE_CTIME_R
35 #define HAVE_INCOMPATIBLE_CTIME_R 0
36 #endif /* !defined INCOMPATIBLE_CTIME_R */
40 #endif /* !defined HAVE_LINK */
42 #ifndef HAVE_POSIX_DECLS
43 #define HAVE_POSIX_DECLS 1
51 #define HAVE_SYMLINK 1
52 #endif /* !defined HAVE_SYMLINK */
54 #ifndef HAVE_SYS_STAT_H
55 #define HAVE_SYS_STAT_H 1
56 #endif /* !defined HAVE_SYS_STAT_H */
58 #ifndef HAVE_SYS_WAIT_H
59 #define HAVE_SYS_WAIT_H 1
60 #endif /* !defined HAVE_SYS_WAIT_H */
63 #define HAVE_UNISTD_H 1
64 #endif /* !defined HAVE_UNISTD_H */
67 #define HAVE_UTMPX_H 1
68 #endif /* !defined HAVE_UTMPX_H */
70 #ifndef NETBSD_INSPIRED
71 # define NETBSD_INSPIRED 1
74 #if HAVE_INCOMPATIBLE_CTIME_R
75 #define asctime_r _incompatible_asctime_r
76 #define ctime_r _incompatible_ctime_r
77 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
79 /* Enable tm_gmtoff and tm_zone on GNUish systems. */
81 /* Fix asctime_r on Solaris 11. */
82 #define _POSIX_PTHREAD_SEMANTICS 1
83 /* Enable strtoimax on pre-C99 Solaris 11. */
84 #define __EXTENSIONS__ 1
90 /* Avoid clashes with NetBSD by renaming NetBSD's declarations. */
91 #define localtime_rz sys_localtime_rz
92 #define mktime_z sys_mktime_z
93 #define posix2time_z sys_posix2time_z
94 #define time2posix_z sys_time2posix_z
95 #define timezone_t sys_timezone_t
96 #define tzalloc sys_tzalloc
97 #define tzfree sys_tzfree
107 #include <sys/types.h> /* for time_t */
110 #include <limits.h> /* for CHAR_BIT et al. */
116 # define ENAMETOOLONG EINVAL
119 # define ENOTSUP EINVAL
122 # define EOVERFLOW EINVAL
127 #endif /* HAVE_GETTEXT */
130 #include <sys/wait.h> /* for WIFEXITED and WEXITSTATUS */
131 #endif /* HAVE_SYS_WAIT_H */
134 #define WIFEXITED(status) (((status) & 0xff) == 0)
135 #endif /* !defined WIFEXITED */
137 #define WEXITSTATUS(status) (((status) >> 8) & 0xff)
138 #endif /* !defined WEXITSTATUS */
141 #include <unistd.h> /* for F_OK, R_OK, and other POSIX goodness */
142 #endif /* HAVE_UNISTD_H */
144 #ifndef HAVE_STRFTIME_L
145 # if _POSIX_VERSION < 200809
146 # define HAVE_STRFTIME_L 0
148 # define HAVE_STRFTIME_L 1
154 #endif /* !defined F_OK */
157 #endif /* !defined R_OK */
159 /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
160 #define is_digit(c) ((unsigned)(c) - '0' <= 9)
163 ** Define HAVE_STDINT_H's default value here, rather than at the
164 ** start, since __GLIBC__ and INTMAX_MAX's values depend on
165 ** previously-included files. glibc 2.1 and Solaris 10 and later have
166 ** stdint.h, even with pre-C99 compilers.
168 #ifndef HAVE_STDINT_H
169 #define HAVE_STDINT_H \
170 (199901 <= __STDC_VERSION__ \
171 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
172 || __CYGWIN__ || INTMAX_MAX)
173 #endif /* !defined HAVE_STDINT_H */
177 #endif /* !HAVE_STDINT_H */
179 #ifndef HAVE_INTTYPES_H
180 # define HAVE_INTTYPES_H HAVE_STDINT_H
183 # include <inttypes.h>
186 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
187 #ifdef __LONG_LONG_MAX__
189 # define LLONG_MAX __LONG_LONG_MAX__
192 # define LLONG_MIN (-1 - LLONG_MAX)
196 #ifndef INT_FAST64_MAX
198 typedef long long int_fast64_t;
199 # define INT_FAST64_MIN LLONG_MIN
200 # define INT_FAST64_MAX LLONG_MAX
202 # if LONG_MAX >> 31 < 0xffffffff
203 Please use a compiler that supports a
64-bit integer
type (or wider
);
204 you may need to compile with
"-DHAVE_STDINT_H".
206 typedef long int_fast64_t;
207 # define INT_FAST64_MIN LONG_MIN
208 # define INT_FAST64_MAX LONG_MAX
213 # if INT_FAST64_MAX == LLONG_MAX
214 # define PRIdFAST64 "lld"
216 # define PRIdFAST64 "ld"
221 # define SCNdFAST64 PRIdFAST64
224 #ifndef INT_FAST32_MAX
225 # if INT_MAX >> 31 == 0
226 typedef long int_fast32_t;
227 # define INT_FAST32_MAX LONG_MAX
228 # define INT_FAST32_MIN LONG_MIN
230 typedef int int_fast32_t;
231 # define INT_FAST32_MAX INT_MAX
232 # define INT_FAST32_MIN INT_MIN
238 typedef long long intmax_t;
239 # define strtoimax strtoll
240 # define INTMAX_MAX LLONG_MAX
241 # define INTMAX_MIN LLONG_MIN
243 typedef long intmax_t;
244 # define strtoimax strtol
245 # define INTMAX_MAX LONG_MAX
246 # define INTMAX_MIN LONG_MIN
251 # if INTMAX_MAX == LLONG_MAX
252 # define PRIdMAX "lld"
254 # define PRIdMAX "ld"
258 #ifndef UINT_FAST64_MAX
259 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
260 typedef unsigned long long uint_fast64_t;
262 # if ULONG_MAX >> 31 >> 1 < 0xffffffff
263 Please use a compiler that supports a
64-bit integer
type (or wider
);
264 you may need to compile with
"-DHAVE_STDINT_H".
266 typedef unsigned long uint_fast64_t;
271 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
272 typedef unsigned long long uintmax_t;
274 typedef unsigned long uintmax_t;
279 # if defined ULLONG_MAX || defined __LONG_LONG_MAX__
280 # define PRIuMAX "llu"
282 # define PRIuMAX "lu"
287 #define INT32_MAX 0x7fffffff
288 #endif /* !defined INT32_MAX */
290 #define INT32_MIN (-1 - INT32_MAX)
291 #endif /* !defined INT32_MIN */
294 #define SIZE_MAX ((size_t) -1)
297 #if 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
298 # define ATTRIBUTE_CONST __attribute__ ((const))
299 # define ATTRIBUTE_PURE __attribute__ ((__pure__))
300 # define ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
302 # define ATTRIBUTE_CONST /* empty */
303 # define ATTRIBUTE_PURE /* empty */
304 # define ATTRIBUTE_FORMAT(spec) /* empty */
307 #if !defined _Noreturn && __STDC_VERSION__ < 201112
308 # if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
309 # define _Noreturn __attribute__ ((__noreturn__))
315 #if __STDC_VERSION__ < 199901 && !defined restrict
316 # define restrict /* empty */
320 ** Workarounds for compilers/systems.
324 # define EPOCH_LOCAL 0
327 # define EPOCH_OFFSET 0
331 ** Compile with -Dtime_tz=T to build the tz package with a private
332 ** time_t type equivalent to T rather than the system-supplied time_t.
333 ** This debugging feature can test unusual design decisions
334 ** (e.g., time_t wider than 'long', or unsigned time_t) even on
335 ** typical platforms.
337 #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
338 # ifdef LOCALTIME_IMPLEMENTATION
339 static time_t sys_time(time_t *x
) { return time(x
); }
342 typedef time_tz tz_time_t
;
345 # define ctime tz_ctime
347 # define ctime_r tz_ctime_r
349 # define difftime tz_difftime
351 # define gmtime tz_gmtime
353 # define gmtime_r tz_gmtime_r
355 # define localtime tz_localtime
357 # define localtime_r tz_localtime_r
359 # define localtime_rz tz_localtime_rz
361 # define mktime tz_mktime
363 # define mktime_z tz_mktime_z
365 # define offtime tz_offtime
367 # define posix2time tz_posix2time
369 # define posix2time_z tz_posix2time_z
371 # define time tz_time
373 # define time2posix tz_time2posix
375 # define time2posix_z tz_time2posix_z
377 # define time_t tz_time_t
379 # define timegm tz_timegm
381 # define timelocal tz_timelocal
383 # define timeoff tz_timeoff
385 # define tzalloc tz_tzalloc
387 # define tzfree tz_tzfree
389 # define tzset tz_tzset
391 # define tzsetwall tz_tzsetwall
393 char *ctime(time_t const *);
394 char *ctime_r(time_t const *, char *);
395 double difftime(time_t, time_t);
396 struct tm
*gmtime(time_t const *);
397 struct tm
*gmtime_r(time_t const *restrict
, struct tm
*restrict
);
398 struct tm
*localtime(time_t const *);
399 struct tm
*localtime_r(time_t const *restrict
, struct tm
*restrict
);
400 time_t mktime(struct tm
*);
401 time_t time(time_t *);
405 #if !HAVE_DECL_ASCTIME_R && !defined asctime_r
406 extern char *asctime_r(struct tm
const *restrict
, char *restrict
);
409 #if !HAVE_POSIX_DECLS
412 extern long timezone
;
420 #if defined ALTZONE && !defined altzone
425 ** The STD_INSPIRED functions are similar, but most also need
426 ** declarations if time_tz is defined.
430 # if !defined tzsetwall || defined time_tz
431 void tzsetwall(void);
433 # if !defined offtime || defined time_tz
434 struct tm
*offtime(time_t const *, long);
436 # if !defined timegm || defined time_tz
437 time_t timegm(struct tm
*);
439 # if !defined timelocal || defined time_tz
440 time_t timelocal(struct tm
*);
442 # if !defined timeoff || defined time_tz
443 time_t timeoff(struct tm
*, long);
445 # if !defined time2posix || defined time_tz
446 time_t time2posix(time_t);
448 # if !defined posix2time || defined time_tz
449 time_t posix2time(time_t);
453 /* Infer TM_ZONE on systems where this information is known, but suppress
454 guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
455 #if (defined __GLIBC__ \
456 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
457 || (defined __APPLE__ && defined __MACH__))
458 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
459 # define TM_GMTOFF tm_gmtoff
461 # if !defined TM_ZONE && !defined NO_TM_ZONE
462 # define TM_ZONE tm_zone
467 ** Define functions that are ABI compatible with NetBSD but have
468 ** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
469 ** and labors under the misconception that 'const timezone_t' is a
470 ** pointer to a constant. This use of 'const' is ineffective, so it
471 ** is not done here. What we call 'struct state' NetBSD calls
472 ** 'struct __state', but this is a private name so it doesn't matter.
475 typedef struct state
*timezone_t
;
476 struct tm
*localtime_rz(timezone_t restrict
, time_t const *restrict
,
477 struct tm
*restrict
);
478 time_t mktime_z(timezone_t restrict
, struct tm
*restrict
);
479 timezone_t
tzalloc(char const *);
480 void tzfree(timezone_t
);
482 # if !defined posix2time_z || defined time_tz
483 time_t posix2time_z(timezone_t
, time_t) ATTRIBUTE_PURE
;
485 # if !defined time2posix_z || defined time_tz
486 time_t time2posix_z(timezone_t
, time_t) ATTRIBUTE_PURE
;
492 ** Finally, some convenience items.
495 #if __STDC_VERSION__ < 199901
500 # include <stdbool.h>
503 #define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
504 #define TYPE_SIGNED(type) (((type) -1) < 0)
505 #define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
507 /* Max and min values of the integer type T, of which only the bottom
508 B bits are used, and where the highest-order used bit is considered
509 to be a sign bit if T is signed. */
510 #define MAXVAL(t, b) \
511 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
512 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
513 #define MINVAL(t, b) \
514 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
516 /* The minimum and maximum finite time values. This implementation
517 assumes no padding if time_t is signed and either the compiler is
518 pre-C11 or time_t is not one of the standard signed integer types. */
519 #if 201112 <= __STDC_VERSION__
520 static time_t const time_t_min
521 = (TYPE_SIGNED(time_t)
522 ? _Generic((time_t) 0,
523 signed char: SCHAR_MIN
, short: SHRT_MIN
,
524 int: INT_MIN
, long: LONG_MIN
, long long: LLONG_MIN
,
525 default: MINVAL(time_t, TYPE_BIT(time_t)))
527 static time_t const time_t_max
528 = (TYPE_SIGNED(time_t)
529 ? _Generic((time_t) 0,
530 signed char: SCHAR_MAX
, short: SHRT_MAX
,
531 int: INT_MAX
, long: LONG_MAX
, long long: LLONG_MAX
,
532 default: MAXVAL(time_t, TYPE_BIT(time_t)))
535 static time_t const time_t_min
= MINVAL(time_t, TYPE_BIT(time_t));
536 static time_t const time_t_max
= MAXVAL(time_t, TYPE_BIT(time_t));
540 ** 302 / 1000 is log10(2.0) rounded up.
541 ** Subtract one for the sign bit if the type is signed;
542 ** add one for integer division truncation;
543 ** add one more for a minus sign if the type is signed.
545 #define INT_STRLEN_MAXIMUM(type) \
546 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
547 1 + TYPE_SIGNED(type))
554 # define INITIALIZE(x) ((x) = 0)
556 # define INITIALIZE(x)
560 # define UNINIT_TRAP 0
564 ** For the benefit of GNU folk...
565 ** '_(MSGID)' uses the current locale's message library string for MSGID.
566 ** The default is to use gettext if available, and use MSGID otherwise.
570 #define _(msgid) gettext(msgid)
571 #else /* !HAVE_GETTEXT */
572 #define _(msgid) msgid
573 #endif /* !HAVE_GETTEXT */
575 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
576 # define TZ_DOMAIN "tz"
579 #if HAVE_INCOMPATIBLE_CTIME_R
582 char *asctime_r(struct tm
const *, char *);
583 char *ctime_r(time_t const *, char *);
584 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
586 /* Handy macros that are independent of tzfile implementation. */
588 #define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
590 #define SECSPERMIN 60
591 #define MINSPERHOUR 60
592 #define HOURSPERDAY 24
593 #define DAYSPERWEEK 7
594 #define DAYSPERNYEAR 365
595 #define DAYSPERLYEAR 366
596 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
597 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
598 #define MONSPERYEAR 12
603 #define TM_WEDNESDAY 3
604 #define TM_THURSDAY 4
606 #define TM_SATURDAY 6
609 #define TM_FEBRUARY 1
616 #define TM_SEPTEMBER 8
618 #define TM_NOVEMBER 10
619 #define TM_DECEMBER 11
621 #define TM_YEAR_BASE 1900
623 #define EPOCH_YEAR 1970
624 #define EPOCH_WDAY TM_THURSDAY
626 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
629 ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
630 ** isleap(y) == isleap(y % 400)
632 ** isleap(a + b) == isleap((a + b) % 400)
634 ** isleap(a + b) == isleap(a % 400 + b % 400)
635 ** This is true even if % means modulo rather than Fortran remainder
636 ** (which is allowed by C89 but not C99).
637 ** We use this to avoid addition overflow problems.
640 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
644 ** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
647 #define AVGSECSPERYEAR 31556952L
648 #define SECSPERREPEAT \
649 ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
650 #define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */
652 #endif /* !defined PRIVATE_H */