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