1#ifndef PRIVATE_H
2
3#define PRIVATE_H
4
5/*
6** This file is in the public domain, so clarified as of
7** 1996-06-05 by Arthur David Olson.
8*/
9
10/*
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.
15** Thank you!
16*/
17
18/* This string was in the Factory zone through version 2016f. */
19#define GRANDPARENTED "Local time zone must be set--see zic manual page"
20
21/*
22** Defaults for preprocessor symbols.
23** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
24*/
25
26#ifndef HAVE_DECL_ASCTIME_R
27#define HAVE_DECL_ASCTIME_R 1
28#endif
29
30#ifndef HAVE_GETTEXT
31#define HAVE_GETTEXT 0
32#endif /* !defined HAVE_GETTEXT */
33
34#ifndef HAVE_INCOMPATIBLE_CTIME_R
35#define HAVE_INCOMPATIBLE_CTIME_R 0
36#endif /* !defined INCOMPATIBLE_CTIME_R */
37
38#ifndef HAVE_LINK
39#define HAVE_LINK 1
40#endif /* !defined HAVE_LINK */
41
42#ifndef HAVE_POSIX_DECLS
43#define HAVE_POSIX_DECLS 1
44#endif
45
46#ifndef HAVE_STRDUP
47#define HAVE_STRDUP 1
48#endif
49
50#ifndef HAVE_SYMLINK
51#define HAVE_SYMLINK 1
52#endif /* !defined HAVE_SYMLINK */
53
54#ifndef HAVE_SYS_STAT_H
55#define HAVE_SYS_STAT_H 1
56#endif /* !defined HAVE_SYS_STAT_H */
57
58#ifndef HAVE_SYS_WAIT_H
59#define HAVE_SYS_WAIT_H 1
60#endif /* !defined HAVE_SYS_WAIT_H */
61
62#ifndef HAVE_UNISTD_H
63#define HAVE_UNISTD_H 1
64#endif /* !defined HAVE_UNISTD_H */
65
66#ifndef HAVE_UTMPX_H
67#define HAVE_UTMPX_H 1
68#endif /* !defined HAVE_UTMPX_H */
69
70#ifndef NETBSD_INSPIRED
71# define NETBSD_INSPIRED 1
72#endif
73
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 */
78
79/* Enable tm_gmtoff and tm_zone on GNUish systems. */
80#define _GNU_SOURCE 1
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
85
86/*
87** Nested includes
88*/
89
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
98#include <time.h>
99#undef localtime_rz
100#undef mktime_z
101#undef posix2time_z
102#undef time2posix_z
103#undef timezone_t
104#undef tzalloc
105#undef tzfree
106
107#include <sys/types.h> /* for time_t */
108#include <stdio.h>
109#include <string.h>
110#include <limits.h> /* for CHAR_BIT et al. */
111#include <stdlib.h>
112
113#include <errno.h>
114
115#ifndef ENAMETOOLONG
116# define ENAMETOOLONG EINVAL
117#endif
118#ifndef ENOTSUP
119# define ENOTSUP EINVAL
120#endif
121#ifndef EOVERFLOW
122# define EOVERFLOW EINVAL
123#endif
124
125#if HAVE_GETTEXT
126#include <libintl.h>
127#endif /* HAVE_GETTEXT */
128
129#if HAVE_SYS_WAIT_H
130#include <sys/wait.h> /* for WIFEXITED and WEXITSTATUS */
131#endif /* HAVE_SYS_WAIT_H */
132
133#ifndef WIFEXITED
134#define WIFEXITED(status) (((status) & 0xff) == 0)
135#endif /* !defined WIFEXITED */
136#ifndef WEXITSTATUS
137#define WEXITSTATUS(status) (((status) >> 8) & 0xff)
138#endif /* !defined WEXITSTATUS */
139
140#if HAVE_UNISTD_H
141#include <unistd.h> /* for F_OK, R_OK, and other POSIX goodness */
142#endif /* HAVE_UNISTD_H */
143
144#ifndef HAVE_STRFTIME_L
145# if _POSIX_VERSION < 200809
146# define HAVE_STRFTIME_L 0
147# else
148# define HAVE_STRFTIME_L 1
149# endif
150#endif
151
152#ifndef F_OK
153#define F_OK 0
154#endif /* !defined F_OK */
155#ifndef R_OK
156#define R_OK 4
157#endif /* !defined R_OK */
158
159/* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
160#define is_digit(c) ((unsigned)(c) - '0' <= 9)
161
162/*
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.
167*/
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 */
174
175#if HAVE_STDINT_H
176#include <stdint.h>
177#endif /* !HAVE_STDINT_H */
178
179#ifndef HAVE_INTTYPES_H
180# define HAVE_INTTYPES_H HAVE_STDINT_H
181#endif
182#if HAVE_INTTYPES_H
183# include <inttypes.h>
184#endif
185
186/* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
187#ifdef __LONG_LONG_MAX__
188# ifndef LLONG_MAX
189# define LLONG_MAX __LONG_LONG_MAX__
190# endif
191# ifndef LLONG_MIN
192# define LLONG_MIN (-1 - LLONG_MAX)
193# endif
194#endif
195
196#ifndef INT_FAST64_MAX
197# ifdef LLONG_MAX
198typedef long long int_fast64_t;
199# define INT_FAST64_MIN LLONG_MIN
200# define INT_FAST64_MAX LLONG_MAX
201# else
202# if LONG_MAX >> 31 < 0xffffffff
203Please use a compiler that supports a 64-bit integer type (or wider);
204you may need to compile with "-DHAVE_STDINT_H".
205# endif
206typedef long int_fast64_t;
207# define INT_FAST64_MIN LONG_MIN
208# define INT_FAST64_MAX LONG_MAX
209# endif
210#endif
211
212#ifndef PRIdFAST64
213# if INT_FAST64_MAX == LLONG_MAX
214# define PRIdFAST64 "lld"
215# else
216# define PRIdFAST64 "ld"
217# endif
218#endif
219
220#ifndef SCNdFAST64
221# define SCNdFAST64 PRIdFAST64
222#endif
223
224#ifndef INT_FAST32_MAX
225# if INT_MAX >> 31 == 0
226typedef long int_fast32_t;
227# define INT_FAST32_MAX LONG_MAX
228# define INT_FAST32_MIN LONG_MIN
229# else
230typedef int int_fast32_t;
231# define INT_FAST32_MAX INT_MAX
232# define INT_FAST32_MIN INT_MIN
233# endif
234#endif
235
236#ifndef INTMAX_MAX
237# ifdef LLONG_MAX
238typedef long long intmax_t;
239# define strtoimax strtoll
240# define INTMAX_MAX LLONG_MAX
241# define INTMAX_MIN LLONG_MIN
242# else
243typedef long intmax_t;
244# define strtoimax strtol
245# define INTMAX_MAX LONG_MAX
246# define INTMAX_MIN LONG_MIN
247# endif
248#endif
249
250#ifndef PRIdMAX
251# if INTMAX_MAX == LLONG_MAX
252# define PRIdMAX "lld"
253# else
254# define PRIdMAX "ld"
255# endif
256#endif
257
258#ifndef UINT_FAST64_MAX
259# if defined ULLONG_MAX || defined __LONG_LONG_MAX__
260typedef unsigned long long uint_fast64_t;
261# else
262# if ULONG_MAX >> 31 >> 1 < 0xffffffff
263Please use a compiler that supports a 64-bit integer type (or wider);
264you may need to compile with "-DHAVE_STDINT_H".
265# endif
266typedef unsigned long uint_fast64_t;
267# endif
268#endif
269
270#ifndef UINTMAX_MAX
271# if defined ULLONG_MAX || defined __LONG_LONG_MAX__
272typedef unsigned long long uintmax_t;
273# else
274typedef unsigned long uintmax_t;
275# endif
276#endif
277
278#ifndef PRIuMAX
279# if defined ULLONG_MAX || defined __LONG_LONG_MAX__
280# define PRIuMAX "llu"
281# else
282# define PRIuMAX "lu"
283# endif
284#endif
285
286#ifndef INT32_MAX
287#define INT32_MAX 0x7fffffff
288#endif /* !defined INT32_MAX */
289#ifndef INT32_MIN
290#define INT32_MIN (-1 - INT32_MAX)
291#endif /* !defined INT32_MIN */
292
293#ifndef SIZE_MAX
294#define SIZE_MAX ((size_t) -1)
295#endif
296
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))
301#else
302# define ATTRIBUTE_CONST /* empty */
303# define ATTRIBUTE_PURE /* empty */
304# define ATTRIBUTE_FORMAT(spec) /* empty */
305#endif
306
307#if !defined _Noreturn && __STDC_VERSION__ < 201112
308# if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
309# define _Noreturn __attribute__ ((__noreturn__))
310# else
311# define _Noreturn
312# endif
313#endif
314
315#if __STDC_VERSION__ < 199901 && !defined restrict
316# define restrict /* empty */
317#endif
318
319/*
320** Workarounds for compilers/systems.
321*/
322
323#ifndef EPOCH_LOCAL
324# define EPOCH_LOCAL 0
325#endif
326#ifndef EPOCH_OFFSET
327# define EPOCH_OFFSET 0
328#endif
329
330/*
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.
336*/
337#if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
338# ifdef LOCALTIME_IMPLEMENTATION
339static time_t sys_time(time_t *x) { return time(x); }
340# endif
341
342typedef time_tz tz_time_t;
343
344# undef ctime
345# define ctime tz_ctime
346# undef ctime_r
347# define ctime_r tz_ctime_r
348# undef difftime
349# define difftime tz_difftime
350# undef gmtime
351# define gmtime tz_gmtime
352# undef gmtime_r
353# define gmtime_r tz_gmtime_r
354# undef localtime
355# define localtime tz_localtime
356# undef localtime_r
357# define localtime_r tz_localtime_r
358# undef localtime_rz
359# define localtime_rz tz_localtime_rz
360# undef mktime
361# define mktime tz_mktime
362# undef mktime_z
363# define mktime_z tz_mktime_z
364# undef offtime
365# define offtime tz_offtime
366# undef posix2time
367# define posix2time tz_posix2time
368# undef posix2time_z
369# define posix2time_z tz_posix2time_z
370# undef time
371# define time tz_time
372# undef time2posix
373# define time2posix tz_time2posix
374# undef time2posix_z
375# define time2posix_z tz_time2posix_z
376# undef time_t
377# define time_t tz_time_t
378# undef timegm
379# define timegm tz_timegm
380# undef timelocal
381# define timelocal tz_timelocal
382# undef timeoff
383# define timeoff tz_timeoff
384# undef tzalloc
385# define tzalloc tz_tzalloc
386# undef tzfree
387# define tzfree tz_tzfree
388# undef tzset
389# define tzset tz_tzset
390# undef tzsetwall
391# define tzsetwall tz_tzsetwall
392
393char *ctime(time_t const *);
394char *ctime_r(time_t const *, char *);
395double difftime(time_t, time_t);
396struct tm *gmtime(time_t const *);
397struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
398struct tm *localtime(time_t const *);
399struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
400time_t mktime(struct tm *);
401time_t time(time_t *);
402void tzset(void);
403#endif
404
405#if !HAVE_DECL_ASCTIME_R && !defined asctime_r
406extern char *asctime_r(struct tm const *restrict, char *restrict);
407#endif
408
409#if !HAVE_POSIX_DECLS
410# ifdef USG_COMPAT
411# ifndef timezone
412extern long timezone;
413# endif
414# ifndef daylight
415extern int daylight;
416# endif
417# endif
418#endif
419
420#if defined ALTZONE && !defined altzone
421extern long altzone;
422#endif
423
424/*
425** The STD_INSPIRED functions are similar, but most also need
426** declarations if time_tz is defined.
427*/
428
429#ifdef STD_INSPIRED
430# if !defined tzsetwall || defined time_tz
431void tzsetwall(void);
432# endif
433# if !defined offtime || defined time_tz
434struct tm *offtime(time_t const *, long);
435# endif
436# if !defined timegm || defined time_tz
437time_t timegm(struct tm *);
438# endif
439# if !defined timelocal || defined time_tz
440time_t timelocal(struct tm *);
441# endif
442# if !defined timeoff || defined time_tz
443time_t timeoff(struct tm *, long);
444# endif
445# if !defined time2posix || defined time_tz
446time_t time2posix(time_t);
447# endif
448# if !defined posix2time || defined time_tz
449time_t posix2time(time_t);
450# endif
451#endif
452
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
460# endif
461# if !defined TM_ZONE && !defined NO_TM_ZONE
462# define TM_ZONE tm_zone
463# endif
464#endif
465
466/*
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.
473*/
474#if NETBSD_INSPIRED
475typedef struct state *timezone_t;
476struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
477 struct tm *restrict);
478time_t mktime_z(timezone_t restrict, struct tm *restrict);
479timezone_t tzalloc(char const *);
480void tzfree(timezone_t);
481# ifdef STD_INSPIRED
482# if !defined posix2time_z || defined time_tz
483time_t posix2time_z(timezone_t, time_t) ATTRIBUTE_PURE;
484# endif
485# if !defined time2posix_z || defined time_tz
486time_t time2posix_z(timezone_t, time_t) ATTRIBUTE_PURE;
487# endif
488# endif
489#endif
490
491/*
492** Finally, some convenience items.
493*/
494
495#if __STDC_VERSION__ < 199901
496# define true 1
497# define false 0
498# define bool int
499#else
500# include <stdbool.h>
501#endif
502
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)
506
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))
515
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__
520static 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)))
526 : 0);
527static 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)))
533 : -1);
534#else
535static time_t const time_t_min = MINVAL(time_t, TYPE_BIT(time_t));
536static time_t const time_t_max = MAXVAL(time_t, TYPE_BIT(time_t));
537#endif
538
539/*
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.
544*/
545#define INT_STRLEN_MAXIMUM(type) \
546 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
547 1 + TYPE_SIGNED(type))
548
549/*
550** INITIALIZE(x)
551*/
552
553#ifdef lint
554# define INITIALIZE(x) ((x) = 0)
555#else
556# define INITIALIZE(x)
557#endif
558
559#ifndef UNINIT_TRAP
560# define UNINIT_TRAP 0
561#endif
562
563/*
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.
567*/
568
569#if HAVE_GETTEXT
570#define _(msgid) gettext(msgid)
571#else /* !HAVE_GETTEXT */
572#define _(msgid) msgid
573#endif /* !HAVE_GETTEXT */
574
575#if !defined TZ_DOMAIN && defined HAVE_GETTEXT
576# define TZ_DOMAIN "tz"
577#endif
578
579#if HAVE_INCOMPATIBLE_CTIME_R
580#undef asctime_r
581#undef ctime_r
582char *asctime_r(struct tm const *, char *);
583char *ctime_r(time_t const *, char *);
584#endif /* HAVE_INCOMPATIBLE_CTIME_R */
585
586/* Handy macros that are independent of tzfile implementation. */
587
588#define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
589
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
599
600#define TM_SUNDAY 0
601#define TM_MONDAY 1
602#define TM_TUESDAY 2
603#define TM_WEDNESDAY 3
604#define TM_THURSDAY 4
605#define TM_FRIDAY 5
606#define TM_SATURDAY 6
607
608#define TM_JANUARY 0
609#define TM_FEBRUARY 1
610#define TM_MARCH 2
611#define TM_APRIL 3
612#define TM_MAY 4
613#define TM_JUNE 5
614#define TM_JULY 6
615#define TM_AUGUST 7
616#define TM_SEPTEMBER 8
617#define TM_OCTOBER 9
618#define TM_NOVEMBER 10
619#define TM_DECEMBER 11
620
621#define TM_YEAR_BASE 1900
622
623#define EPOCH_YEAR 1970
624#define EPOCH_WDAY TM_THURSDAY
625
626#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
627
628/*
629** Since everything in isleap is modulo 400 (or a factor of 400), we know that
630** isleap(y) == isleap(y % 400)
631** and so
632** isleap(a + b) == isleap((a + b) % 400)
633** or
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.
638*/
639
640#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
641
642
643/*
644** The Gregorian year averages 365.2425 days, which is 31556952 seconds.
645*/
646
647#define AVGSECSPERYEAR 31556952L
648#define SECSPERREPEAT \
649 ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
650#define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */
651
652#endif /* !defined PRIVATE_H */
653