1 /* Private header for tzdb code. */
2
3 #ifndef PRIVATE_H
4
5 #define PRIVATE_H
6
7 /*
8 ** This file is in the public domain, so clarified as of
9 ** 1996-06-05 by Arthur David Olson.
10 */
11
12 /*
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.
17 ** Thank you!
18 */
19
20 /* PORT_TO_C89 means the code should work even if the underlying
21 compiler and library support only C89. SUPPORT_C89 means the
22 tzcode library should support C89 callers in addition to the usual
23 support for C99-and-later callers. These macros are obsolescent,
24 and the plan is to remove them along with any code needed only when
25 they are nonzero. */
26 #ifndef PORT_TO_C89
27 # define PORT_TO_C89 0
28 #endif
29 #ifndef SUPPORT_C89
30 # define SUPPORT_C89 0
31 #endif
32
33 #ifndef __STDC_VERSION__
34 # define __STDC_VERSION__ 0
35 #endif
36
37 /* Define true, false and bool if they don't work out of the box. */
38 #if PORT_TO_C89 && __STDC_VERSION__ < 199901
39 # define true 1
40 # define false 0
41 # define bool int
42 #elif __STDC_VERSION__ < 202311
43 # include <stdbool.h>
44 #endif
45
46 #if __STDC_VERSION__ < 202311
47 # define static_assert(cond) extern int static_assert_check[(cond) ? 1 : -1]
48 #endif
49
50 /*
51 ** zdump has been made independent of the rest of the time
52 ** conversion package to increase confidence in the verification it provides.
53 ** You can use zdump to help in verifying other implementations.
54 ** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
55 */
56 #ifndef USE_LTZ
57 # define USE_LTZ 1
58 #endif
59
60 /* This string was in the Factory zone through version 2016f. */
61 #define GRANDPARENTED "Local time zone must be set--see zic manual page"
62
63 /*
64 ** Defaults for preprocessor symbols.
65 ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
66 */
67
68 #ifndef HAVE_DECL_ASCTIME_R
69 # define HAVE_DECL_ASCTIME_R 1
70 #endif
71
72 #if !defined HAVE__GENERIC && defined __has_extension
73 # if __has_extension(c_generic_selections)
74 # define HAVE__GENERIC 1
75 # else
76 # define HAVE__GENERIC 0
77 # endif
78 #endif
79 /* _Generic is buggy in pre-4.9 GCC. */
80 #if !defined HAVE__GENERIC && defined __GNUC__ && !defined __STRICT_ANSI__
81 # define HAVE__GENERIC (4 < __GNUC__ + (9 <= __GNUC_MINOR__))
82 #endif
83 #ifndef HAVE__GENERIC
84 # define HAVE__GENERIC (201112 <= __STDC_VERSION__)
85 #endif
86
87 #if !defined HAVE_GETTEXT && defined __has_include
88 # if __has_include(<libintl.h>)
89 # define HAVE_GETTEXT true
90 # endif
91 #endif
92 #ifndef HAVE_GETTEXT
93 # define HAVE_GETTEXT false
94 #endif
95
96 #ifndef HAVE_INCOMPATIBLE_CTIME_R
97 # define HAVE_INCOMPATIBLE_CTIME_R 0
98 #endif
99
100 #ifndef HAVE_LINK
101 # define HAVE_LINK 1
102 #endif /* !defined HAVE_LINK */
103
104 #ifndef HAVE_MALLOC_ERRNO
105 # define HAVE_MALLOC_ERRNO 1
106 #endif
107
108 #ifndef HAVE_POSIX_DECLS
109 # define HAVE_POSIX_DECLS 1
110 #endif
111
112 #ifndef HAVE_SETENV
113 # define HAVE_SETENV 1
114 #endif
115
116 #ifndef HAVE_STRDUP
117 # define HAVE_STRDUP 1
118 #endif
119
120 #ifndef HAVE_SYMLINK
121 # define HAVE_SYMLINK 1
122 #endif /* !defined HAVE_SYMLINK */
123
124 #if !defined HAVE_SYS_STAT_H && defined __has_include
125 # if !__has_include(<sys/stat.h>)
126 # define HAVE_SYS_STAT_H false
127 # endif
128 #endif
129 #ifndef HAVE_SYS_STAT_H
130 # define HAVE_SYS_STAT_H true
131 #endif
132
133 #if !defined HAVE_UNISTD_H && defined __has_include
134 # if !__has_include(<unistd.h>)
135 # define HAVE_UNISTD_H false
136 # endif
137 #endif
138 #ifndef HAVE_UNISTD_H
139 # define HAVE_UNISTD_H true
140 #endif
141
142 #ifndef NETBSD_INSPIRED
143 # define NETBSD_INSPIRED 1
144 #endif
145
146 #if HAVE_INCOMPATIBLE_CTIME_R
147 # define asctime_r _incompatible_asctime_r
148 # define ctime_r _incompatible_ctime_r
149 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
150
151 /* Enable tm_gmtoff, tm_zone, and environ on GNUish systems. */
152 #define _GNU_SOURCE 1
153 /* Fix asctime_r on Solaris 11. */
154 #define _POSIX_PTHREAD_SEMANTICS 1
155 /* Enable strtoimax on pre-C99 Solaris 11. */
156 #define __EXTENSIONS__ 1
157
158 /* On GNUish systems where time_t might be 32 or 64 bits, use 64.
159 On these platforms _FILE_OFFSET_BITS must also be 64; otherwise
160 setting _TIME_BITS to 64 does not work. The code does not
161 otherwise rely on _FILE_OFFSET_BITS being 64, since it does not
162 use off_t or functions like 'stat' that depend on off_t. */
163 #ifndef _FILE_OFFSET_BITS
164 # define _FILE_OFFSET_BITS 64
165 #endif
166 #if !defined _TIME_BITS && _FILE_OFFSET_BITS == 64
167 # define _TIME_BITS 64
168 #endif
169
170 /*
171 ** Nested includes
172 */
173
174 /* Avoid clashes with NetBSD by renaming NetBSD's declarations.
175 If defining the 'timezone' variable, avoid a clash with FreeBSD's
176 'timezone' function by renaming its declaration. */
177 #define localtime_rz sys_localtime_rz
178 #define mktime_z sys_mktime_z
179 #define posix2time_z sys_posix2time_z
180 #define time2posix_z sys_time2posix_z
181 #if defined USG_COMPAT && USG_COMPAT == 2
182 # define timezone sys_timezone
183 #endif
184 #define timezone_t sys_timezone_t
185 #define tzalloc sys_tzalloc
186 #define tzfree sys_tzfree
187 #include <time.h>
188 #undef localtime_rz
189 #undef mktime_z
190 #undef posix2time_z
191 #undef time2posix_z
192 #if defined USG_COMPAT && USG_COMPAT == 2
193 # undef timezone
194 #endif
195 #undef timezone_t
196 #undef tzalloc
197 #undef tzfree
198
199 #include <stddef.h>
200 #include <string.h>
201 #if !PORT_TO_C89
202 # include <inttypes.h>
203 #endif
204 #include <limits.h> /* for CHAR_BIT et al. */
205 #include <stdlib.h>
206
207 #include <errno.h>
208
209 #ifndef EINVAL
210 # define EINVAL ERANGE
211 #endif
212
213 #ifndef ELOOP
214 # define ELOOP EINVAL
215 #endif
216 #ifndef ENAMETOOLONG
217 # define ENAMETOOLONG EINVAL
218 #endif
219 #ifndef ENOMEM
220 # define ENOMEM EINVAL
221 #endif
222 #ifndef ENOTSUP
223 # define ENOTSUP EINVAL
224 #endif
225 #ifndef EOVERFLOW
226 # define EOVERFLOW EINVAL
227 #endif
228
229 #if HAVE_GETTEXT
230 # include <libintl.h>
231 #endif /* HAVE_GETTEXT */
232
233 #if HAVE_UNISTD_H
234 # include <unistd.h> /* for R_OK, and other POSIX goodness */
235 #endif /* HAVE_UNISTD_H */
236
237 #ifndef HAVE_STRFTIME_L
238 # if _POSIX_VERSION < 200809
239 # define HAVE_STRFTIME_L 0
240 # else
241 # define HAVE_STRFTIME_L 1
242 # endif
243 #endif
244
245 #ifndef USG_COMPAT
246 # ifndef _XOPEN_VERSION
247 # define USG_COMPAT 0
248 # else
249 # define USG_COMPAT 1
250 # endif
251 #endif
252
253 #ifndef HAVE_TZNAME
254 # if _POSIX_VERSION < 198808 && !USG_COMPAT
255 # define HAVE_TZNAME 0
256 # else
257 # define HAVE_TZNAME 1
258 # endif
259 #endif
260
261 #ifndef ALTZONE
262 # if defined __sun || defined _M_XENIX
263 # define ALTZONE 1
264 # else
265 # define ALTZONE 0
266 # endif
267 #endif
268
269 #ifndef R_OK
270 # define R_OK 4
271 #endif /* !defined R_OK */
272
273 #if PORT_TO_C89
274
275 /*
276 ** Define HAVE_STDINT_H's default value here, rather than at the
277 ** start, since __GLIBC__ and INTMAX_MAX's values depend on
278 ** previously included files. glibc 2.1 and Solaris 10 and later have
279 ** stdint.h, even with pre-C99 compilers.
280 */
281 #if !defined HAVE_STDINT_H && defined __has_include
282 # define HAVE_STDINT_H true /* C23 __has_include implies C99 stdint.h. */
283 #endif
284 #ifndef HAVE_STDINT_H
285 # define HAVE_STDINT_H \
286 (199901 <= __STDC_VERSION__ \
287 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
288 || __CYGWIN__ || INTMAX_MAX)
289 #endif /* !defined HAVE_STDINT_H */
290
291 #if HAVE_STDINT_H
292 # include <stdint.h>
293 #endif /* !HAVE_STDINT_H */
294
295 #ifndef HAVE_INTTYPES_H
296 # define HAVE_INTTYPES_H HAVE_STDINT_H
297 #endif
298 #if HAVE_INTTYPES_H
299 # include <inttypes.h>
300 #endif
301
302 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
303 #if defined __LONG_LONG_MAX__ && !defined __STRICT_ANSI__
304 # ifndef LLONG_MAX
305 # define LLONG_MAX __LONG_LONG_MAX__
306 # endif
307 # ifndef LLONG_MIN
308 # define LLONG_MIN (-1 - LLONG_MAX)
309 # endif
310 # ifndef ULLONG_MAX
311 # define ULLONG_MAX (LLONG_MAX * 2ull + 1)
312 # endif
313 #endif
314
315 #ifndef INT_FAST64_MAX
316 # if 1 <= LONG_MAX >> 31 >> 31
317 typedef long int_fast64_t;
318 # define INT_FAST64_MIN LONG_MIN
319 # define INT_FAST64_MAX LONG_MAX
320 # else
321 /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
322 typedef long long int_fast64_t;
323 # define INT_FAST64_MIN LLONG_MIN
324 # define INT_FAST64_MAX LLONG_MAX
325 # endif
326 #endif
327
328 #ifndef PRIdFAST64
329 # if INT_FAST64_MAX == LONG_MAX
330 # define PRIdFAST64 "ld"
331 # else
332 # define PRIdFAST64 "lld"
333 # endif
334 #endif
335
336 #ifndef SCNdFAST64
337 # define SCNdFAST64 PRIdFAST64
338 #endif
339
340 #ifndef INT_FAST32_MAX
341 # if INT_MAX >> 31 == 0
342 typedef long int_fast32_t;
343 # define INT_FAST32_MAX LONG_MAX
344 # define INT_FAST32_MIN LONG_MIN
345 # else
346 typedef int int_fast32_t;
347 # define INT_FAST32_MAX INT_MAX
348 # define INT_FAST32_MIN INT_MIN
349 # endif
350 #endif
351
352 #ifndef INTMAX_MAX
353 # ifdef LLONG_MAX
354 typedef long long intmax_t;
355 # ifndef HAVE_STRTOLL
356 # define HAVE_STRTOLL true
357 # endif
358 # if HAVE_STRTOLL
359 # define strtoimax strtoll
360 # endif
361 # define INTMAX_MAX LLONG_MAX
362 # define INTMAX_MIN LLONG_MIN
363 # else
364 typedef long intmax_t;
365 # define INTMAX_MAX LONG_MAX
366 # define INTMAX_MIN LONG_MIN
367 # endif
368 # ifndef strtoimax
369 # define strtoimax strtol
370 # endif
371 #endif
372
373 #ifndef PRIdMAX
374 # if INTMAX_MAX == LLONG_MAX
375 # define PRIdMAX "lld"
376 # else
377 # define PRIdMAX "ld"
378 # endif
379 #endif
380
381 #ifndef PTRDIFF_MAX
382 # define PTRDIFF_MAX MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t))
383 #endif
384
385 #ifndef UINT_FAST32_MAX
386 typedef unsigned long uint_fast32_t;
387 #endif
388
389 #ifndef UINT_FAST64_MAX
390 # if 3 <= ULONG_MAX >> 31 >> 31
391 typedef unsigned long uint_fast64_t;
392 # define UINT_FAST64_MAX ULONG_MAX
393 # else
394 /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
395 typedef unsigned long long uint_fast64_t;
396 # define UINT_FAST64_MAX ULLONG_MAX
397 # endif
398 #endif
399
400 #ifndef UINTMAX_MAX
401 # ifdef ULLONG_MAX
402 typedef unsigned long long uintmax_t;
403 # define UINTMAX_MAX ULLONG_MAX
404 # else
405 typedef unsigned long uintmax_t;
406 # define UINTMAX_MAX ULONG_MAX
407 # endif
408 #endif
409
410 #ifndef PRIuMAX
411 # ifdef ULLONG_MAX
412 # define PRIuMAX "llu"
413 # else
414 # define PRIuMAX "lu"
415 # endif
416 #endif
417
418 #ifndef SIZE_MAX
419 # define SIZE_MAX ((size_t) -1)
420 #endif
421
422 #endif /* PORT_TO_C89 */
423
424 /* The maximum size of any created object, as a signed integer.
425 Although the C standard does not outright prohibit larger objects,
426 behavior is undefined if the result of pointer subtraction does not
427 fit into ptrdiff_t, and the code assumes in several places that
428 pointer subtraction works. As a practical matter it's OK to not
429 support objects larger than this. */
430 #define INDEX_MAX ((ptrdiff_t) min(PTRDIFF_MAX, SIZE_MAX))
431
432 /* Support ckd_add, ckd_sub, ckd_mul on C23 or recent-enough GCC-like
433 hosts, unless compiled with -DHAVE_STDCKDINT_H=0 or with pre-C23 EDG. */
434 #if !defined HAVE_STDCKDINT_H && defined __has_include
435 # if __has_include(<stdckdint.h>)
436 # define HAVE_STDCKDINT_H true
437 # endif
438 #endif
439 #ifdef HAVE_STDCKDINT_H
440 # if HAVE_STDCKDINT_H
441 # include <stdckdint.h>
442 # endif
443 #elif defined __EDG__
444 /* Do nothing, to work around EDG bug <https://bugs.gnu.org/53256>. */
445 #elif defined __has_builtin
446 # if __has_builtin(__builtin_add_overflow)
447 # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
448 # endif
449 # if __has_builtin(__builtin_sub_overflow)
450 # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
451 # endif
452 # if __has_builtin(__builtin_mul_overflow)
453 # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
454 # endif
455 #elif 7 <= __GNUC__
456 # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
457 # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
458 # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
459 #endif
460
461 #if 3 <= __GNUC__
462 # define ATTRIBUTE_MALLOC __attribute__((malloc))
463 # define ATTRIBUTE_FORMAT(spec) __attribute__((format spec))
464 #else
465 # define ATTRIBUTE_MALLOC /* empty */
466 # define ATTRIBUTE_FORMAT(spec) /* empty */
467 #endif
468
469 #if (defined __has_c_attribute \
470 && (202311 <= __STDC_VERSION__ || !defined __STRICT_ANSI__))
471 # define HAVE___HAS_C_ATTRIBUTE true
472 #else
473 # define HAVE___HAS_C_ATTRIBUTE false
474 #endif
475
476 #if HAVE___HAS_C_ATTRIBUTE
477 # if __has_c_attribute(deprecated)
478 # define ATTRIBUTE_DEPRECATED [[deprecated]]
479 # endif
480 #endif
481 #ifndef ATTRIBUTE_DEPRECATED
482 # if 3 < __GNUC__ + (2 <= __GNUC_MINOR__)
483 # define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
484 # else
485 # define ATTRIBUTE_DEPRECATED /* empty */
486 # endif
487 #endif
488
489 #if HAVE___HAS_C_ATTRIBUTE
490 # if __has_c_attribute(fallthrough)
491 # define ATTRIBUTE_FALLTHROUGH [[fallthrough]]
492 # endif
493 #endif
494 #ifndef ATTRIBUTE_FALLTHROUGH
495 # if 7 <= __GNUC__
496 # define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
497 # else
498 # define ATTRIBUTE_FALLTHROUGH ((void) 0)
499 # endif
500 #endif
501
502 #if HAVE___HAS_C_ATTRIBUTE
503 # if __has_c_attribute(maybe_unused)
504 # define ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]]
505 # endif
506 #endif
507 #ifndef ATTRIBUTE_MAYBE_UNUSED
508 # if 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
509 # define ATTRIBUTE_MAYBE_UNUSED __attribute__((unused))
510 # else
511 # define ATTRIBUTE_MAYBE_UNUSED /* empty */
512 # endif
513 #endif
514
515 #if HAVE___HAS_C_ATTRIBUTE
516 # if __has_c_attribute(noreturn)
517 # define ATTRIBUTE_NORETURN [[noreturn]]
518 # endif
519 #endif
520 #ifndef ATTRIBUTE_NORETURN
521 # if 201112 <= __STDC_VERSION__
522 # define ATTRIBUTE_NORETURN _Noreturn
523 # elif 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
524 # define ATTRIBUTE_NORETURN __attribute__((noreturn))
525 # else
526 # define ATTRIBUTE_NORETURN /* empty */
527 # endif
528 #endif
529
530 #if HAVE___HAS_C_ATTRIBUTE
531 # if __has_c_attribute(reproducible)
532 # define ATTRIBUTE_REPRODUCIBLE [[reproducible]]
533 # endif
534 #endif
535 #ifndef ATTRIBUTE_REPRODUCIBLE
536 # if 3 <= __GNUC__
537 # define ATTRIBUTE_REPRODUCIBLE __attribute__((pure))
538 # else
539 # define ATTRIBUTE_REPRODUCIBLE /* empty */
540 # endif
541 #endif
542
543 #if HAVE___HAS_C_ATTRIBUTE
544 # if __has_c_attribute(unsequenced)
545 # define ATTRIBUTE_UNSEQUENCED [[unsequenced]]
546 # endif
547 #endif
548 #ifndef ATTRIBUTE_UNSEQUENCED
549 # if 3 <= __GNUC__
550 # define ATTRIBUTE_UNSEQUENCED __attribute__((const))
551 # else
552 # define ATTRIBUTE_UNSEQUENCED /* empty */
553 # endif
554 #endif
555
556 #if (__STDC_VERSION__ < 199901 && !defined restrict \
557 && (PORT_TO_C89 || defined _MSC_VER))
558 # define restrict /* empty */
559 #endif
560
561 /*
562 ** Workarounds for compilers/systems.
563 */
564
565 #ifndef EPOCH_LOCAL
566 # define EPOCH_LOCAL 0
567 #endif
568 #ifndef EPOCH_OFFSET
569 # define EPOCH_OFFSET 0
570 #endif
571 #ifndef RESERVE_STD_EXT_IDS
572 # define RESERVE_STD_EXT_IDS 0
573 #endif
574
575 /* If standard C identifiers with external linkage (e.g., localtime)
576 are reserved and are not already being renamed anyway, rename them
577 as if compiling with '-Dtime_tz=time_t'. */
578 #if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
579 # define time_tz time_t
580 #endif
581
582 /*
583 ** Compile with -Dtime_tz=T to build the tz package with a private
584 ** time_t type equivalent to T rather than the system-supplied time_t.
585 ** This debugging feature can test unusual design decisions
586 ** (e.g., time_t wider than 'long', or unsigned time_t) even on
587 ** typical platforms.
588 */
589 #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
590 # define TZ_TIME_T 1
591 #else
592 # define TZ_TIME_T 0
593 #endif
594
595 #if defined LOCALTIME_IMPLEMENTATION && TZ_TIME_T
sys_time(time_t * x)596 static time_t sys_time(time_t *x) { return time(x); }
597 #endif
598
599 #if TZ_TIME_T
600
601 typedef time_tz tz_time_t;
602
603 # undef asctime
604 # define asctime tz_asctime
605 # undef asctime_r
606 # define asctime_r tz_asctime_r
607 # undef ctime
608 # define ctime tz_ctime
609 # undef ctime_r
610 # define ctime_r tz_ctime_r
611 # undef difftime
612 # define difftime tz_difftime
613 # undef gmtime
614 # define gmtime tz_gmtime
615 # undef gmtime_r
616 # define gmtime_r tz_gmtime_r
617 # undef localtime
618 # define localtime tz_localtime
619 # undef localtime_r
620 # define localtime_r tz_localtime_r
621 # undef localtime_rz
622 # define localtime_rz tz_localtime_rz
623 # undef mktime
624 # define mktime tz_mktime
625 # undef mktime_z
626 # define mktime_z tz_mktime_z
627 # undef offtime
628 # define offtime tz_offtime
629 # undef posix2time
630 # define posix2time tz_posix2time
631 # undef posix2time_z
632 # define posix2time_z tz_posix2time_z
633 # undef strftime
634 # define strftime tz_strftime
635 # undef time
636 # define time tz_time
637 # undef time2posix
638 # define time2posix tz_time2posix
639 # undef time2posix_z
640 # define time2posix_z tz_time2posix_z
641 # undef time_t
642 # define time_t tz_time_t
643 # undef timegm
644 # define timegm tz_timegm
645 # undef timelocal
646 # define timelocal tz_timelocal
647 # undef timeoff
648 # define timeoff tz_timeoff
649 # undef tzalloc
650 # define tzalloc tz_tzalloc
651 # undef tzfree
652 # define tzfree tz_tzfree
653 # undef tzset
654 # define tzset tz_tzset
655 # if HAVE_STRFTIME_L
656 # undef strftime_l
657 # define strftime_l tz_strftime_l
658 # endif
659 # if HAVE_TZNAME
660 # undef tzname
661 # define tzname tz_tzname
662 # endif
663 # if USG_COMPAT
664 # undef daylight
665 # define daylight tz_daylight
666 # undef timezone
667 # define timezone tz_timezone
668 # endif
669 # if ALTZONE
670 # undef altzone
671 # define altzone tz_altzone
672 # endif
673
674 # if __STDC_VERSION__ < 202311
675 # define DEPRECATED_IN_C23 /* empty */
676 # else
677 # define DEPRECATED_IN_C23 ATTRIBUTE_DEPRECATED
678 # endif
679 DEPRECATED_IN_C23 char *asctime(struct tm const *);
680 char *asctime_r(struct tm const *restrict, char *restrict);
681 DEPRECATED_IN_C23 char *ctime(time_t const *);
682 char *ctime_r(time_t const *, char *);
683 ATTRIBUTE_UNSEQUENCED double difftime(time_t, time_t);
684 size_t strftime(char *restrict, size_t, char const *restrict,
685 struct tm const *restrict);
686 # if HAVE_STRFTIME_L
687 size_t strftime_l(char *restrict, size_t, char const *restrict,
688 struct tm const *restrict, locale_t);
689 # endif
690 struct tm *gmtime(time_t const *);
691 struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
692 struct tm *localtime(time_t const *);
693 struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
694 time_t mktime(struct tm *);
695 time_t time(time_t *);
696 time_t timegm(struct tm *);
697 void tzset(void);
698 #endif
699
700 #ifndef HAVE_DECL_TIMEGM
701 # if (202311 <= __STDC_VERSION__ \
702 || defined __GLIBC__ || defined __tm_zone /* musl */ \
703 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
704 || (defined __APPLE__ && defined __MACH__))
705 # define HAVE_DECL_TIMEGM true
706 # else
707 # define HAVE_DECL_TIMEGM false
708 # endif
709 #endif
710 #if !HAVE_DECL_TIMEGM && !defined timegm
711 time_t timegm(struct tm *);
712 #endif
713
714 #if !HAVE_DECL_ASCTIME_R && !defined asctime_r
715 extern char *asctime_r(struct tm const *restrict, char *restrict);
716 #endif
717
718 #ifndef HAVE_DECL_ENVIRON
719 # if defined environ || defined __USE_GNU
720 # define HAVE_DECL_ENVIRON 1
721 # else
722 # define HAVE_DECL_ENVIRON 0
723 # endif
724 #endif
725
726 #if !HAVE_DECL_ENVIRON
727 extern char **environ;
728 #endif
729
730 #if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
731 extern char *tzname[];
732 #endif
733 #if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
734 extern long timezone;
735 extern int daylight;
736 #endif
737 #if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
738 extern long altzone;
739 #endif
740
741 /*
742 ** The STD_INSPIRED functions are similar, but most also need
743 ** declarations if time_tz is defined.
744 */
745
746 #ifndef STD_INSPIRED
747 # define STD_INSPIRED 0
748 #endif
749 #if STD_INSPIRED
750 # if TZ_TIME_T || !defined offtime
751 struct tm *offtime(time_t const *, long);
752 # endif
753 # if TZ_TIME_T || !defined timelocal
754 time_t timelocal(struct tm *);
755 # endif
756 # if TZ_TIME_T || !defined timeoff
757 time_t timeoff(struct tm *, long);
758 # endif
759 # if TZ_TIME_T || !defined time2posix
760 time_t time2posix(time_t);
761 # endif
762 # if TZ_TIME_T || !defined posix2time
763 time_t posix2time(time_t);
764 # endif
765 #endif
766
767 /* Infer TM_ZONE on systems where this information is known, but suppress
768 guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
769 #if (defined __GLIBC__ \
770 || defined __tm_zone /* musl */ \
771 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
772 || (defined __APPLE__ && defined __MACH__))
773 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
774 # define TM_GMTOFF tm_gmtoff
775 # endif
776 # if !defined TM_ZONE && !defined NO_TM_ZONE
777 # define TM_ZONE tm_zone
778 # endif
779 #endif
780
781 /*
782 ** Define functions that are ABI compatible with NetBSD but have
783 ** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
784 ** and labors under the misconception that 'const timezone_t' is a
785 ** pointer to a constant. This use of 'const' is ineffective, so it
786 ** is not done here. What we call 'struct state' NetBSD calls
787 ** 'struct __state', but this is a private name so it doesn't matter.
788 */
789 #if NETBSD_INSPIRED
790 typedef struct state *timezone_t;
791 struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
792 struct tm *restrict);
793 time_t mktime_z(timezone_t restrict, struct tm *restrict);
794 timezone_t tzalloc(char const *);
795 void tzfree(timezone_t);
796 # if STD_INSPIRED
797 # if TZ_TIME_T || !defined posix2time_z
798 ATTRIBUTE_REPRODUCIBLE time_t posix2time_z(timezone_t, time_t);
799 # endif
800 # if TZ_TIME_T || !defined time2posix_z
801 ATTRIBUTE_REPRODUCIBLE time_t time2posix_z(timezone_t, time_t);
802 # endif
803 # endif
804 #endif
805
806 /*
807 ** Finally, some convenience items.
808 */
809
810 #define TYPE_BIT(type) (CHAR_BIT * (ptrdiff_t) sizeof(type))
811 #define TYPE_SIGNED(type) (((type) -1) < 0)
812 #define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
813
814 /* Minimum and maximum of two values. Use lower case to avoid
815 naming clashes with standard include files. */
816 #define max(a, b) ((a) > (b) ? (a) : (b))
817 #define min(a, b) ((a) < (b) ? (a) : (b))
818
819 /* Max and min values of the integer type T, of which only the bottom
820 B bits are used, and where the highest-order used bit is considered
821 to be a sign bit if T is signed. */
822 #define MAXVAL(t, b) \
823 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
824 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
825 #define MINVAL(t, b) \
826 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
827
828 /* The extreme time values, assuming no padding. */
829 #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
830 #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
831
832 /* The extreme time values. These are macros, not constants, so that
833 any portability problems occur only when compiling .c files that use
834 the macros, which is safer for applications that need only zdump and zic.
835 This implementation assumes no padding if time_t is signed and
836 either the compiler lacks support for _Generic or time_t is not one
837 of the standard signed integer types. */
838 #if HAVE__GENERIC
839 # define TIME_T_MIN \
840 _Generic((time_t) 0, \
841 signed char: SCHAR_MIN, short: SHRT_MIN, \
842 int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
843 default: TIME_T_MIN_NO_PADDING)
844 # define TIME_T_MAX \
845 (TYPE_SIGNED(time_t) \
846 ? _Generic((time_t) 0, \
847 signed char: SCHAR_MAX, short: SHRT_MAX, \
848 int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
849 default: TIME_T_MAX_NO_PADDING) \
850 : (time_t) -1)
851 enum { SIGNED_PADDING_CHECK_NEEDED
852 = _Generic((time_t) 0,
853 signed char: false, short: false,
854 int: false, long: false, long long: false,
855 default: true) };
856 #else
857 # define TIME_T_MIN TIME_T_MIN_NO_PADDING
858 # define TIME_T_MAX TIME_T_MAX_NO_PADDING
859 enum { SIGNED_PADDING_CHECK_NEEDED = true };
860 #endif
861 /* Try to check the padding assumptions. Although TIME_T_MAX and the
862 following check can both have undefined behavior on oddball
863 platforms due to shifts exceeding widths of signed integers, these
864 platforms' compilers are likely to diagnose these issues in integer
865 constant expressions, so it shouldn't hurt to check statically. */
866 static_assert(! TYPE_SIGNED(time_t) || ! SIGNED_PADDING_CHECK_NEEDED
867 || TIME_T_MAX >> (TYPE_BIT(time_t) - 2) == 1);
868
869 /*
870 ** 302 / 1000 is log10(2.0) rounded up.
871 ** Subtract one for the sign bit if the type is signed;
872 ** add one for integer division truncation;
873 ** add one more for a minus sign if the type is signed.
874 */
875 #define INT_STRLEN_MAXIMUM(type) \
876 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
877 1 + TYPE_SIGNED(type))
878
879 /*
880 ** INITIALIZE(x)
881 */
882
883 #ifdef GCC_LINT
884 # define INITIALIZE(x) ((x) = 0)
885 #else
886 # define INITIALIZE(x)
887 #endif
888
889 /* Whether memory access must strictly follow the C standard.
890 If 0, it's OK to read uninitialized storage so long as the value is
891 not relied upon. Defining it to 0 lets mktime access parts of
892 struct tm that might be uninitialized, as a heuristic when the
893 standard doesn't say what to return and when tm_gmtoff can help
894 mktime likely infer a better value. */
895 #ifndef UNINIT_TRAP
896 # define UNINIT_TRAP 0
897 #endif
898
899 #ifdef DEBUG
900 # undef unreachable
901 # define unreachable() abort()
902 #elif !defined unreachable
903 # ifdef __has_builtin
904 # if __has_builtin(__builtin_unreachable)
905 # define unreachable() __builtin_unreachable()
906 # endif
907 # elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
908 # define unreachable() __builtin_unreachable()
909 # endif
910 # ifndef unreachable
911 # define unreachable() ((void) 0)
912 # endif
913 #endif
914
915 /*
916 ** For the benefit of GNU folk...
917 ** '_(MSGID)' uses the current locale's message library string for MSGID.
918 ** The default is to use gettext if available, and use MSGID otherwise.
919 */
920
921 #if HAVE_GETTEXT
922 #define _(msgid) gettext(msgid)
923 #else /* !HAVE_GETTEXT */
924 #define _(msgid) msgid
925 #endif /* !HAVE_GETTEXT */
926
927 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
928 # define TZ_DOMAIN "tz"
929 #endif
930
931 #if HAVE_INCOMPATIBLE_CTIME_R
932 #undef asctime_r
933 #undef ctime_r
934 char *asctime_r(struct tm const *restrict, char *restrict);
935 char *ctime_r(time_t const *, char *);
936 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
937
938 /* Handy macros that are independent of tzfile implementation. */
939
940 enum {
941 SECSPERMIN = 60,
942 MINSPERHOUR = 60,
943 SECSPERHOUR = SECSPERMIN * MINSPERHOUR,
944 HOURSPERDAY = 24,
945 DAYSPERWEEK = 7,
946 DAYSPERNYEAR = 365,
947 DAYSPERLYEAR = DAYSPERNYEAR + 1,
948 MONSPERYEAR = 12,
949 YEARSPERREPEAT = 400 /* years before a Gregorian repeat */
950 };
951
952 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
953
954 #define DAYSPERREPEAT ((int_fast32_t) 400 * 365 + 100 - 4 + 1)
955 #define SECSPERREPEAT ((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
956 #define AVGSECSPERYEAR (SECSPERREPEAT / YEARSPERREPEAT)
957
958 enum {
959 TM_SUNDAY,
960 TM_MONDAY,
961 TM_TUESDAY,
962 TM_WEDNESDAY,
963 TM_THURSDAY,
964 TM_FRIDAY,
965 TM_SATURDAY
966 };
967
968 enum {
969 TM_JANUARY,
970 TM_FEBRUARY,
971 TM_MARCH,
972 TM_APRIL,
973 TM_MAY,
974 TM_JUNE,
975 TM_JULY,
976 TM_AUGUST,
977 TM_SEPTEMBER,
978 TM_OCTOBER,
979 TM_NOVEMBER,
980 TM_DECEMBER
981 };
982
983 enum {
984 TM_YEAR_BASE = 1900,
985 TM_WDAY_BASE = TM_MONDAY,
986 EPOCH_YEAR = 1970,
987 EPOCH_WDAY = TM_THURSDAY
988 };
989
990 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
991
992 /*
993 ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
994 ** isleap(y) == isleap(y % 400)
995 ** and so
996 ** isleap(a + b) == isleap((a + b) % 400)
997 ** or
998 ** isleap(a + b) == isleap(a % 400 + b % 400)
999 ** This is true even if % means modulo rather than Fortran remainder
1000 ** (which is allowed by C89 but not by C99 or later).
1001 ** We use this to avoid addition overflow problems.
1002 */
1003
1004 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
1005
1006 #endif /* !defined PRIVATE_H */
1007