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 /* For pre-C23 compilers, a substitute for static_assert.
86 Some of these compilers may warn if it is used outside the top level. */
87 #if __STDC_VERSION__ < 202311 && !defined static_assert
88 # define static_assert(cond) extern int static_assert_check[(cond) ? 1 : -1]
89 #endif
90
91 /*
92 ** zdump has been made independent of the rest of the time
93 ** conversion package to increase confidence in the verification it provides.
94 ** You can use zdump to help in verifying other implementations.
95 ** To do this, compile with -DUSE_LTZ=0 and link without the tz library.
96 */
97 #ifndef USE_LTZ
98 # define USE_LTZ 1
99 #endif
100
101 /* This string was in the Factory zone through version 2016f. */
102 #ifndef GRANDPARENTED
103 # define GRANDPARENTED "Local time zone must be set--see zic manual page"
104 #endif
105
106 /*
107 ** Defaults for preprocessor symbols.
108 ** You can override these in your C compiler options, e.g. '-DHAVE_GETTEXT=1'.
109 */
110
111 #if !defined HAVE__GENERIC && defined __has_extension
112 # if !__has_extension(c_generic_selections)
113 # define HAVE__GENERIC 0
114 # endif
115 #endif
116 /* _Generic is buggy in pre-4.9 GCC. */
117 #if !defined HAVE__GENERIC && defined __GNUC__ && !defined __STRICT_ANSI__
118 # define HAVE__GENERIC (4 < __GNUC__ + (9 <= __GNUC_MINOR__))
119 #endif
120 #ifndef HAVE__GENERIC
121 # define HAVE__GENERIC (201112 <= __STDC_VERSION__)
122 #endif
123
124 #ifndef HAVE_GETEUID
125 # define HAVE_GETEUID 1
126 #endif
127
128 #ifndef HAVE_GETRESUID
129 # define HAVE_GETRESUID 1
130 #endif
131
132 #if !defined HAVE_GETTEXT && defined __has_include
133 # if __has_include(<libintl.h>)
134 # define HAVE_GETTEXT 1
135 # endif
136 #endif
137 #ifndef HAVE_GETTEXT
138 # define HAVE_GETTEXT 0
139 #endif
140
141 #ifndef HAVE_INCOMPATIBLE_CTIME_R
142 # define HAVE_INCOMPATIBLE_CTIME_R 0
143 #endif
144
145 #ifndef HAVE_LINK
146 # define HAVE_LINK 1
147 #endif /* !defined HAVE_LINK */
148
149 #ifndef HAVE_MALLOC_ERRNO
150 # define HAVE_MALLOC_ERRNO 1
151 #endif
152
153 #ifndef HAVE_POSIX_DECLS
154 # define HAVE_POSIX_DECLS 1
155 #endif
156
157 #ifndef HAVE_SETENV
158 # define HAVE_SETENV 1
159 #endif
160
161 #ifndef HAVE_STRDUP
162 # define HAVE_STRDUP 1
163 #endif
164
165 #ifndef HAVE_SYMLINK
166 # define HAVE_SYMLINK 1
167 #endif /* !defined HAVE_SYMLINK */
168
169 #if !defined HAVE_SYS_STAT_H && defined __has_include
170 # if !__has_include(<sys/stat.h>)
171 # define HAVE_SYS_STAT_H 0
172 # endif
173 #endif
174 #ifndef HAVE_SYS_STAT_H
175 # define HAVE_SYS_STAT_H 1
176 #endif
177
178 #if !defined HAVE_UNISTD_H && defined __has_include
179 # if !__has_include(<unistd.h>)
180 # define HAVE_UNISTD_H 0
181 # endif
182 #endif
183 #ifndef HAVE_UNISTD_H
184 # define HAVE_UNISTD_H 1
185 #endif
186
187 #ifndef NETBSD_INSPIRED
188 # define NETBSD_INSPIRED 1
189 #endif
190
191 #if HAVE_INCOMPATIBLE_CTIME_R
192 # define asctime_r _incompatible_asctime_r
193 # define ctime_r _incompatible_ctime_r
194 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
195
196 #ifndef TZ_RUNTIME_LEAPS
197 # define TZ_RUNTIME_LEAPS 1
198 #endif
199
200 /*
201 ** Nested includes
202 */
203
204 #include <stddef.h>
205
206 /* If defining the 'timezone' variable a la POSIX, avoid clashing with the old
207 'timezone' function of FreeBSD <= 14, by renaming the latter's declaration.
208 This hack can be removed after 2028-11-30, FreeBSD 14's expected EOL. */
209 #if (defined __FreeBSD__ && __FreeBSD__ < 15 && defined __BSD_VISIBLE \
210 && defined USG_COMPAT && USG_COMPAT == 2)
211 # define timezone sys_timezone
212 # define timezone_defined
213 #endif
214
215 #include <time.h>
216
217 #ifdef timezone_defined
218 # undef timezone
219 # undef timezone_defined
220 #endif
221
222 #include <string.h>
223 #if defined HAVE_STRNLEN && !HAVE_STRNLEN
224 static size_t
strnlen(char const * s,size_t maxlen)225 strnlen (char const *s, size_t maxlen)
226 {
227 size_t i;
228 for (i = 0; i < maxlen && s[i]; i++)
229 continue;
230 return i;
231 }
232 #endif
233
234 #if !PORT_TO_C89
235 # include <inttypes.h>
236 #endif
237 #include <limits.h> /* for CHAR_BIT et al. */
238 #include <stdlib.h>
239
240 #include <errno.h>
241
242 #ifndef EINVAL
243 # define EINVAL ERANGE
244 #endif
245
246 #ifndef ELOOP
247 # define ELOOP EINVAL
248 #endif
249 #ifndef ENAMETOOLONG
250 # define ENAMETOOLONG EINVAL
251 #endif
252 #ifndef ENOMEM
253 # define ENOMEM EINVAL
254 #endif
255 #ifndef ENOTCAPABLE
256 # define ENOTCAPABLE EINVAL
257 #endif
258 #ifndef ENOTSUP
259 # define ENOTSUP EINVAL
260 #endif
261 #ifndef EOVERFLOW
262 # define EOVERFLOW EINVAL
263 #endif
264
265 #if HAVE_GETTEXT
266 # include <libintl.h>
267 #endif /* HAVE_GETTEXT */
268
269 #if HAVE_UNISTD_H
270 # include <unistd.h>
271 #else
272 /* Assume getopt.o or equivalent is linked via Makefile configuration. */
273 int getopt(int, char *const[], char const *);
274 extern char *optarg;
275 extern int optind;
276 #endif /* HAVE_UNISTD_H */
277
278 /* SUPPORT_POSIX2008 means the tzcode library should support
279 POSIX.1-2017-and-earlier callers in addition to the usual support for
280 POSIX.1-2024-and-later callers; however, this can be
281 incompatible with POSIX.1-2024-and-later callers.
282 This macro is obsolescent, and the plan is to remove it
283 along with any code needed only when it is nonzero.
284 A good time to do that might be in the year 2034.
285 This macro's name is SUPPORT_POSIX2008 because _POSIX_VERSION == 200809
286 in POSIX.1-2017, a minor revision of POSIX.1-2008. */
287 #ifndef SUPPORT_POSIX2008
288 # if defined _POSIX_VERSION && _POSIX_VERSION <= 200809
289 # define SUPPORT_POSIX2008 1
290 # else
291 # define SUPPORT_POSIX2008 0
292 # endif
293 #endif
294
295 #ifndef HAVE_DECL_ASCTIME_R
296 # if SUPPORT_POSIX2008
297 # define HAVE_DECL_ASCTIME_R 1
298 # else
299 # define HAVE_DECL_ASCTIME_R 0
300 # endif
301 #endif
302
303 #ifndef HAVE_ISSETUGID
304 # if (defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
305 || (defined __linux__ && !defined __GLIBC__) /* Android, musl, etc. */ \
306 || (defined __APPLE__ && defined __MACH__) || defined __sun)
307 # define HAVE_ISSETUGID 1
308 # else
309 # define HAVE_ISSETUGID 0
310 # endif
311 #endif
312
313 #ifndef HAVE_SNPRINTF
314 # define HAVE_SNPRINTF (!PORT_TO_C89 || 199901 <= __STDC_VERSION__)
315 #endif
316
317 #ifndef HAVE_STRFTIME_L
318 # if _POSIX_VERSION < 200809
319 # define HAVE_STRFTIME_L 0
320 # else
321 # define HAVE_STRFTIME_L 1
322 # endif
323 #endif
324
325 #ifndef USG_COMPAT
326 # ifndef _XOPEN_VERSION
327 # define USG_COMPAT 0
328 # else
329 # define USG_COMPAT 1
330 # endif
331 #endif
332
333 #ifndef HAVE_TZNAME
334 # if _POSIX_VERSION < 198808 && !USG_COMPAT
335 # define HAVE_TZNAME 0
336 # else
337 # define HAVE_TZNAME 1
338 # endif
339 #endif
340
341 #ifndef ALTZONE
342 # if defined __sun || defined _M_XENIX
343 # define ALTZONE 1
344 # else
345 # define ALTZONE 0
346 # endif
347 #endif
348
349 #if PORT_TO_C89
350
351 /*
352 ** Define HAVE_STDINT_H's default value here, rather than at the
353 ** start, since __GLIBC__ and INTMAX_MAX's values depend on
354 ** previously included files. glibc 2.1 and Solaris 10 and later have
355 ** stdint.h, even with pre-C99 compilers.
356 */
357 # if !defined HAVE_STDINT_H && defined __has_include
358 # define HAVE_STDINT_H 1 /* C23 __has_include implies C99 stdint.h. */
359 # endif
360 # ifndef HAVE_STDINT_H
361 # define HAVE_STDINT_H \
362 (199901 <= __STDC_VERSION__ \
363 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \
364 || __CYGWIN__ || INTMAX_MAX)
365 # endif /* !defined HAVE_STDINT_H */
366
367 # if HAVE_STDINT_H
368 # include <stdint.h>
369 # endif /* !HAVE_STDINT_H */
370
371 # ifndef HAVE_INTTYPES_H
372 # define HAVE_INTTYPES_H HAVE_STDINT_H
373 # endif
374 # if HAVE_INTTYPES_H
375 # include <inttypes.h>
376 # endif
377
378 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */
379 # if defined __LONG_LONG_MAX__ && !defined __STRICT_ANSI__
380 # ifndef LLONG_MAX
381 # define LLONG_MAX __LONG_LONG_MAX__
382 # endif
383 # ifndef LLONG_MIN
384 # define LLONG_MIN (-1 - LLONG_MAX)
385 # endif
386 # ifndef ULLONG_MAX
387 # define ULLONG_MAX (LLONG_MAX * 2ull + 1)
388 # endif
389 # endif
390
391 # ifndef INT_FAST64_MAX
392 # if 1 <= LONG_MAX >> 31 >> 31
393 typedef long int_fast64_t;
394 # define INT_FAST64_MIN LONG_MIN
395 # define INT_FAST64_MAX LONG_MAX
396 # else
397 /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
398 typedef long long int_fast64_t;
399 # define INT_FAST64_MIN LLONG_MIN
400 # define INT_FAST64_MAX LLONG_MAX
401 # endif
402 # endif
403
404 # ifndef PRIdFAST64
405 # if INT_FAST64_MAX == LONG_MAX
406 # define PRIdFAST64 "ld"
407 # else
408 # define PRIdFAST64 "lld"
409 # endif
410 # endif
411
412 # ifndef SCNdFAST64
413 # define SCNdFAST64 PRIdFAST64
414 # endif
415
416 # ifndef INT_FAST32_MAX
417 typedef long int_fast32_t;
418 # define INT_FAST32_MAX LONG_MAX
419 # define INT_FAST32_MIN LONG_MIN
420 # endif
421
422 # ifndef INT_LEAST32_MAX
423 typedef int_fast32_t int_least32_t;
424 # endif
425
426 # ifndef INTMAX_MAX
427 # ifdef LLONG_MAX
428 typedef long long intmax_t;
429 # ifndef HAVE_STRTOLL
430 # define HAVE_STRTOLL 1
431 # endif
432 # if HAVE_STRTOLL
433 # define strtoimax strtoll
434 # endif
435 # define INTMAX_MAX LLONG_MAX
436 # define INTMAX_MIN LLONG_MIN
437 # else
438 typedef long intmax_t;
439 # define INTMAX_MAX LONG_MAX
440 # define INTMAX_MIN LONG_MIN
441 # endif
442 # ifndef strtoimax
443 # define strtoimax strtol
444 # endif
445 # endif
446
447 # ifndef PRIdMAX
448 # if INTMAX_MAX == LLONG_MAX
449 # define PRIdMAX "lld"
450 # else
451 # define PRIdMAX "ld"
452 # endif
453 # endif
454
455 # ifndef PTRDIFF_MAX
456 # define PTRDIFF_MAX MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t))
457 # endif
458
459 # ifndef UINT_FAST32_MAX
460 typedef unsigned long uint_fast32_t;
461 # endif
462
463 # ifndef UINT_FAST64_MAX
464 # if 3 <= ULONG_MAX >> 31 >> 31
465 typedef unsigned long uint_fast64_t;
466 # define UINT_FAST64_MAX ULONG_MAX
467 # else
468 /* If this fails, compile with -DHAVE_STDINT_H or with a better compiler. */
469 typedef unsigned long long uint_fast64_t;
470 # define UINT_FAST64_MAX ULLONG_MAX
471 # endif
472 # endif
473
474 # ifndef UINTMAX_MAX
475 # ifdef ULLONG_MAX
476 typedef unsigned long long uintmax_t;
477 # define UINTMAX_MAX ULLONG_MAX
478 # else
479 typedef unsigned long uintmax_t;
480 # define UINTMAX_MAX ULONG_MAX
481 # endif
482 # endif
483
484 # ifndef PRIuMAX
485 # ifdef ULLONG_MAX
486 # define PRIuMAX "llu"
487 # else
488 # define PRIuMAX "lu"
489 # endif
490 # endif
491
492 # ifndef SIZE_MAX
493 # define SIZE_MAX ((size_t) -1)
494 # endif
495
496 #endif /* PORT_TO_C89 */
497
498 /* The maximum size of any created object, as a signed integer.
499 Although the C standard does not outright prohibit larger objects,
500 behavior is undefined if the result of pointer subtraction does not
501 fit into ptrdiff_t, and the code assumes in several places that
502 pointer subtraction works. As a practical matter it's OK to not
503 support objects larger than this. */
504 #define INDEX_MAX ((ptrdiff_t) min(PTRDIFF_MAX, SIZE_MAX))
505
506 /* Support ckd_add, ckd_sub, ckd_mul on C23 or recent-enough GCC-like
507 hosts, unless compiled with -DHAVE_STDCKDINT_H=0 or with pre-C23 EDG. */
508 #if !defined HAVE_STDCKDINT_H && defined __has_include
509 # if __has_include(<stdckdint.h>)
510 # define HAVE_STDCKDINT_H 1
511 # endif
512 #endif
513 #ifdef HAVE_STDCKDINT_H
514 # if HAVE_STDCKDINT_H
515 # include <stdckdint.h>
516 # endif
517 #elif defined __EDG__
518 /* Do nothing, to work around EDG bug <https://bugs.gnu.org/53256>. */
519 #elif defined __has_builtin
520 # if __has_builtin(__builtin_add_overflow)
521 # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
522 # endif
523 # if __has_builtin(__builtin_sub_overflow)
524 # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
525 # endif
526 # if __has_builtin(__builtin_mul_overflow)
527 # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
528 # endif
529 #elif 7 <= __GNUC__
530 # define ckd_add(r, a, b) __builtin_add_overflow(a, b, r)
531 # define ckd_sub(r, a, b) __builtin_sub_overflow(a, b, r)
532 # define ckd_mul(r, a, b) __builtin_mul_overflow(a, b, r)
533 #endif
534
535 #if (defined __has_c_attribute \
536 && (202311 <= __STDC_VERSION__ || !defined __STRICT_ANSI__))
537 # define HAVE___HAS_C_ATTRIBUTE true
538 #else
539 # define HAVE___HAS_C_ATTRIBUTE false
540 #endif
541
542 #ifdef __has_attribute
543 # if __has_attribute (nonstring)
544 # define ATTRIBUTE_NONSTRING __attribute__((__nonstring__))
545 # endif
546 #endif
547 #ifndef ATTRIBUTE_NONSTRING
548 # define ATTRIBUTE_NONSTRING
549 #endif
550
551 #if HAVE___HAS_C_ATTRIBUTE
552 # if __has_c_attribute(deprecated)
553 # define ATTRIBUTE_DEPRECATED [[deprecated]]
554 # endif
555 #endif
556 #ifndef ATTRIBUTE_DEPRECATED
557 # if 3 < __GNUC__ + (2 <= __GNUC_MINOR__)
558 # define ATTRIBUTE_DEPRECATED __attribute__((deprecated))
559 # else
560 # define ATTRIBUTE_DEPRECATED /* empty */
561 # endif
562 #endif
563
564 #if HAVE___HAS_C_ATTRIBUTE
565 # if __has_c_attribute(fallthrough)
566 # define ATTRIBUTE_FALLTHROUGH [[fallthrough]]
567 # endif
568 #endif
569 #ifndef ATTRIBUTE_FALLTHROUGH
570 # if 7 <= __GNUC__
571 # define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
572 # else
573 # define ATTRIBUTE_FALLTHROUGH ((void) 0)
574 # endif
575 #endif
576
577 #if HAVE___HAS_C_ATTRIBUTE
578 # if __has_c_attribute(maybe_unused)
579 # define ATTRIBUTE_MAYBE_UNUSED [[maybe_unused]]
580 # endif
581 #endif
582 #ifndef ATTRIBUTE_MAYBE_UNUSED
583 # if 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
584 # define ATTRIBUTE_MAYBE_UNUSED __attribute__((unused))
585 # else
586 # define ATTRIBUTE_MAYBE_UNUSED /* empty */
587 # endif
588 #endif
589
590 #if HAVE___HAS_C_ATTRIBUTE
591 # if __has_c_attribute(noreturn)
592 # define ATTRIBUTE_NORETURN [[noreturn]]
593 # endif
594 #endif
595 #ifndef ATTRIBUTE_NORETURN
596 # if 201112 <= __STDC_VERSION__
597 # define ATTRIBUTE_NORETURN _Noreturn
598 # elif 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
599 # define ATTRIBUTE_NORETURN __attribute__((noreturn))
600 # else
601 # define ATTRIBUTE_NORETURN /* empty */
602 # endif
603 #endif
604
605 #if HAVE___HAS_C_ATTRIBUTE
606 # if __has_c_attribute(reproducible)
607 # define ATTRIBUTE_REPRODUCIBLE [[reproducible]]
608 # endif
609 #endif
610 #ifndef ATTRIBUTE_REPRODUCIBLE
611 # define ATTRIBUTE_REPRODUCIBLE /* empty */
612 #endif
613
614 #if HAVE___HAS_C_ATTRIBUTE
615 # if __has_c_attribute(unsequenced)
616 # define ATTRIBUTE_UNSEQUENCED [[unsequenced]]
617 # endif
618 #endif
619 #ifndef ATTRIBUTE_UNSEQUENCED
620 # define ATTRIBUTE_UNSEQUENCED /* empty */
621 #endif
622
623 /* GNU C attributes that are useful in tzcode.
624 Although neither __attribute__((const)) nor __attribute__((pure)) are
625 stricter than their C23 counterparts [[unsequenced]] and [[reproducible]],
626 the C23 attributes happen to work in each tzcode use of ATTRIBUTE_CONST
627 and ATTRIBUTE_PURE. (This might not work outside of tzcode!) */
628 #if __GNUC__ < 3
629 # define ATTRIBUTE_CONST ATTRIBUTE_UNSEQUENCED
630 # define ATTRIBUTE_FORMAT(spec) /* empty */
631 # define ATTRIBUTE_PURE ATTRIBUTE_REPRODUCIBLE
632 #else
633 # define ATTRIBUTE_CONST __attribute__((const))
634 # define ATTRIBUTE_FORMAT(spec) __attribute__((format spec))
635 # define ATTRIBUTE_PURE __attribute__((pure))
636 #endif
637
638 /* Avoid GCC bug 114833 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114833>.
639 Remove this macro and its uses when the bug is fixed in a GCC release,
640 because only the latest GCC matters for $(GCC_DEBUG_FLAGS). */
641 #ifdef GCC_LINT
642 # define ATTRIBUTE_PURE_114833 ATTRIBUTE_PURE
643 #else
644 # define ATTRIBUTE_PURE_114833 /* empty */
645 #endif
646 /* GCC_LINT hack to pacify GCC bug 114833 even though the attribute is
647 not strictly correct, as the function might not return whereas pure
648 functions are supposed to return exactly once. This hack is not
649 known to generate wrong code for tzcode on any platform.
650 Remove this macro and its uses when the bug is fixed in a GCC release. */
651 #define ATTRIBUTE_PURE_114833_HACK ATTRIBUTE_PURE_114833
652
653 #if (__STDC_VERSION__ < 199901 && !defined restrict \
654 && (PORT_TO_C89 || defined _MSC_VER))
655 # define restrict /* empty */
656 #endif
657
658 /*
659 ** Workarounds for compilers/systems.
660 */
661
662 #ifndef EPOCH_LOCAL
663 # define EPOCH_LOCAL 0
664 #endif
665 #ifndef EPOCH_OFFSET
666 # define EPOCH_OFFSET 0
667 #endif
668 #ifndef RESERVE_STD_EXT_IDS
669 # define RESERVE_STD_EXT_IDS 0
670 #endif
671
672 #ifdef time_tz
673 # define defined_time_tz true
674 #else
675 # define defined_time_tz false
676 #endif
677
678 /* If standard C identifiers with external linkage (e.g., localtime)
679 are reserved and are not already being renamed anyway, rename them
680 as if compiling with '-Dtime_tz=time_t'. */
681 #if !defined time_tz && RESERVE_STD_EXT_IDS && USE_LTZ
682 # define time_tz time_t
683 #endif
684
685 /*
686 ** Compile with -Dtime_tz=T to build the tz package with a private
687 ** time_t type equivalent to T rather than the system-supplied time_t.
688 ** This debugging feature can test unusual design decisions
689 ** (e.g., time_t wider than 'long', or unsigned time_t) even on
690 ** typical platforms.
691 */
692 #if defined time_tz || EPOCH_LOCAL || EPOCH_OFFSET != 0
693 # define TZ_TIME_T true
694 #else
695 # define TZ_TIME_T false
696 #endif
697
698 #if defined LOCALTIME_IMPLEMENTATION && TZ_TIME_T
sys_time(time_t * x)699 static time_t sys_time(time_t *x) { return time(x); }
700 #endif
701
702 #if TZ_TIME_T
703
704 typedef time_tz tz_time_t;
705
706 # undef asctime
707 # define asctime tz_asctime
708 # undef ctime
709 # define ctime tz_ctime
710 # undef difftime
711 # define difftime tz_difftime
712 # undef gmtime
713 # define gmtime tz_gmtime
714 # undef gmtime_r
715 # define gmtime_r tz_gmtime_r
716 # undef localtime
717 # define localtime tz_localtime
718 # undef localtime_r
719 # define localtime_r tz_localtime_r
720 # undef localtime_rz
721 # define localtime_rz tz_localtime_rz
722 # undef mktime
723 # define mktime tz_mktime
724 # undef mktime_z
725 # define mktime_z tz_mktime_z
726 # undef offtime
727 # define offtime tz_offtime
728 # undef offtime_r
729 # define offtime_r tz_offtime_r
730 # undef posix2time
731 # define posix2time tz_posix2time
732 # undef posix2time_z
733 # define posix2time_z tz_posix2time_z
734 # undef strftime
735 # define strftime tz_strftime
736 # undef time
737 # define time tz_time
738 # undef time2posix
739 # define time2posix tz_time2posix
740 # undef time2posix_z
741 # define time2posix_z tz_time2posix_z
742 # undef time_t
743 # define time_t tz_time_t
744 # undef timegm
745 # define timegm tz_timegm
746 # undef timelocal
747 # define timelocal tz_timelocal
748 # undef timeoff
749 # define timeoff tz_timeoff
750 # undef tzalloc
751 # define tzalloc tz_tzalloc
752 # undef tzfree
753 # define tzfree tz_tzfree
754 # undef tzset
755 # define tzset tz_tzset
756 # if SUPPORT_POSIX2008
757 # undef asctime_r
758 # define asctime_r tz_asctime_r
759 # undef ctime_r
760 # define ctime_r tz_ctime_r
761 # endif
762 # if HAVE_STRFTIME_L
763 # undef strftime_l
764 # define strftime_l tz_strftime_l
765 # endif
766 # if HAVE_TZNAME
767 # undef tzname
768 # define tzname tz_tzname
769 # endif
770 # if USG_COMPAT
771 # undef daylight
772 # define daylight tz_daylight
773 # undef timezone
774 # define timezone tz_timezone
775 # endif
776 # if ALTZONE
777 # undef altzone
778 # define altzone tz_altzone
779 # endif
780
781 # if __STDC_VERSION__ < 202311
782 # define DEPRECATED_IN_C23 /* empty */
783 # else
784 # define DEPRECATED_IN_C23 ATTRIBUTE_DEPRECATED
785 # endif
786 DEPRECATED_IN_C23 char *asctime(struct tm const *);
787 DEPRECATED_IN_C23 char *ctime(time_t const *);
788 # if SUPPORT_POSIX2008
789 char *asctime_r(struct tm const *restrict, char *restrict);
790 char *ctime_r(time_t const *, char *);
791 # endif
792 ATTRIBUTE_CONST double difftime(time_t, time_t);
793 size_t strftime(char *restrict, size_t, char const *restrict,
794 struct tm const *restrict);
795 # if HAVE_STRFTIME_L
796 size_t strftime_l(char *restrict, size_t, char const *restrict,
797 struct tm const *restrict, locale_t);
798 # endif
799 struct tm *gmtime(time_t const *);
800 struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
801 struct tm *localtime(time_t const *);
802 struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
803 time_t mktime(struct tm *);
804 time_t time(time_t *);
805 time_t timegm(struct tm *);
806 void tzset(void);
807 #endif
808
809 #ifndef HAVE_DECL_TIMEGM
810 # if (202311 <= __STDC_VERSION__ \
811 || defined __GLIBC__ || defined __tm_zone /* musl */ \
812 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
813 || (defined __APPLE__ && defined __MACH__))
814 # define HAVE_DECL_TIMEGM 1
815 # else
816 # define HAVE_DECL_TIMEGM 0
817 # endif
818 #endif
819 #if !HAVE_DECL_TIMEGM && !defined timegm
820 time_t timegm(struct tm *);
821 #endif
822
823 #if !HAVE_DECL_ASCTIME_R && !defined asctime_r && SUPPORT_POSIX2008
824 extern char *asctime_r(struct tm const *restrict, char *restrict);
825 #endif
826
827 #ifndef HAVE_DECL_ENVIRON
828 # if defined environ || defined __USE_GNU
829 # define HAVE_DECL_ENVIRON 1
830 # else
831 # define HAVE_DECL_ENVIRON 0
832 # endif
833 #endif
834
835 #if !HAVE_DECL_ENVIRON
836 extern char **environ;
837 #endif
838
839 #ifndef HAVE_MEMPCPY
840 # if (defined mempcpy \
841 || defined __FreeBSD__ || defined __NetBSD__ || defined __linux__)
842 # define HAVE_MEMPCPY 1
843 # else
844 # define HAVE_MEMPCPY 0
845 # endif
846 #endif
847 #if !HAVE_MEMPCPY
848 static void *
mempcpy(void * restrict s1,void const * restrict s2,size_t n)849 mempcpy(void *restrict s1, void const *restrict s2, size_t n)
850 {
851 char *p = memcpy(s1, s2, n);
852 return p + n;
853 }
854 #endif
855
856 #if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
857 extern char *tzname[];
858 #endif
859 #if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
860 extern long timezone;
861 extern int daylight;
862 #endif
863 #if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
864 extern long altzone;
865 #endif
866
867 /*
868 ** The STD_INSPIRED functions are similar, but most also need
869 ** declarations if time_tz is defined.
870 */
871
872 #ifndef STD_INSPIRED
873 # ifdef __NetBSD__
874 # define STD_INSPIRED 1
875 # else
876 # define STD_INSPIRED 0
877 # endif
878 #endif
879 #if STD_INSPIRED
880 # if TZ_TIME_T || !defined offtime
881 struct tm *offtime(time_t const *, long);
882 # endif
883 # if TZ_TIME_T || !defined offtime_r
884 struct tm *offtime_r(time_t const *restrict, long, struct tm *restrict);
885 # endif
886 # if TZ_TIME_T || !defined timelocal
887 time_t timelocal(struct tm *);
888 # endif
889 # if TZ_TIME_T || !defined timeoff
890 # define EXTERN_TIMEOFF
891 # endif
892 # if TZ_TIME_T || !defined time2posix
893 time_t time2posix(time_t);
894 # endif
895 # if TZ_TIME_T || !defined posix2time
896 time_t posix2time(time_t);
897 # endif
898 #endif
899
900 /* Infer TM_ZONE on systems where this information is known, but suppress
901 guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
902 #if (200809 < _POSIX_VERSION \
903 || defined __GLIBC__ \
904 || defined __tm_zone /* musl */ \
905 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
906 || (defined __APPLE__ && defined __MACH__))
907 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
908 # define TM_GMTOFF tm_gmtoff
909 # endif
910 # if !defined TM_ZONE && !defined NO_TM_ZONE
911 # define TM_ZONE tm_zone
912 # endif
913 #endif
914
915 /*
916 ** Define functions that are ABI compatible with NetBSD.
917 ** What we call 'struct state' NetBSD calls
918 ** 'struct __state', but this is a private name so it doesn't matter.
919 */
920 #if NETBSD_INSPIRED
921 # ifdef _NETBSD_SOURCE
922 # define state __state
923 # else
924 typedef struct state *timezone_t;
925 # endif
926 struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
927 struct tm *restrict);
928 time_t mktime_z(timezone_t restrict, struct tm *restrict);
929 timezone_t tzalloc(char const *);
930 void tzfree(timezone_t);
931 # if STD_INSPIRED
932 # if TZ_RUNTIME_LEAPS
933 # define ATTRIBUTE_POSIX2TIME ATTRIBUTE_PURE
934 # else
935 # define ATTRIBUTE_POSIX2TIME ATTRIBUTE_CONST
936 # endif
937 # if TZ_TIME_T || !defined posix2time_z
938 ATTRIBUTE_POSIX2TIME time_t posix2time_z(timezone_t, time_t);
939 # endif
940 # if TZ_TIME_T || !defined time2posix_z
941 ATTRIBUTE_POSIX2TIME time_t time2posix_z(timezone_t, time_t);
942 # endif
943 # endif
944 #endif
945
946 /*
947 ** Finally, some convenience items.
948 */
949
950 #define TYPE_BIT(type) (CHAR_BIT * (ptrdiff_t) sizeof(type))
951 #define TYPE_SIGNED(type) (((type) -1) < 0)
952 #define TWOS_COMPLEMENT(type) (TYPE_SIGNED (type) && (! ~ (type) -1))
953
954 /* Minimum and maximum of two values. Use lower case to avoid
955 naming clashes with standard include files. */
956 #define max(a, b) ((a) > (b) ? (a) : (b))
957 #define min(a, b) ((a) < (b) ? (a) : (b))
958
959 /* Max and min values of the integer type T, of which only the bottom
960 B bits are used, and where the highest-order used bit is considered
961 to be a sign bit if T is signed. */
962 #define MAXVAL(t, b) \
963 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
964 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
965 #define MINVAL(t, b) \
966 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
967
968 /* The extreme time values, assuming no padding. */
969 #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
970 #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
971
972 /* The extreme time values. These are macros, not constants, so that
973 any portability problems occur only when compiling .c files that use
974 the macros, which is safer for applications that need only zdump and zic.
975 This implementation assumes no padding if time_t is signed and
976 either the compiler lacks support for _Generic or time_t is not one
977 of the standard signed integer types. */
978 #if HAVE__GENERIC
979 # define TIME_T_MIN \
980 _Generic((time_t) 0, \
981 signed char: SCHAR_MIN, short: SHRT_MIN, \
982 int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
983 default: TIME_T_MIN_NO_PADDING)
984 # define TIME_T_MAX \
985 (TYPE_SIGNED(time_t) \
986 ? _Generic((time_t) 0, \
987 signed char: SCHAR_MAX, short: SHRT_MAX, \
988 int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
989 default: TIME_T_MAX_NO_PADDING) \
990 : (time_t) -1)
991 enum { SIGNED_PADDING_CHECK_NEEDED
992 = _Generic((time_t) 0,
993 signed char: false, short: false,
994 int: false, long: false, long long: false,
995 default: true) };
996 #else
997 # define TIME_T_MIN TIME_T_MIN_NO_PADDING
998 # define TIME_T_MAX TIME_T_MAX_NO_PADDING
999 enum { SIGNED_PADDING_CHECK_NEEDED = true };
1000 #endif
1001 /* Try to check the padding assumptions. Although TIME_T_MAX and the
1002 following check can both have undefined behavior on oddball
1003 platforms due to shifts exceeding widths of signed integers, these
1004 platforms' compilers are likely to diagnose these issues in integer
1005 constant expressions, so it shouldn't hurt to check statically. */
1006 static_assert(! TYPE_SIGNED(time_t) || ! SIGNED_PADDING_CHECK_NEEDED
1007 || TIME_T_MAX >> (TYPE_BIT(time_t) - 2) == 1);
1008
1009 /*
1010 ** 302 / 1000 is log10(2.0) rounded up.
1011 ** Subtract one for the sign bit if the type is signed;
1012 ** add one for integer division truncation;
1013 ** add one more for a minus sign if the type is signed.
1014 */
1015 #define INT_STRLEN_MAXIMUM(type) \
1016 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
1017 1 + TYPE_SIGNED(type))
1018
1019 /*
1020 ** INITIALIZE(x)
1021 */
1022
1023 #ifdef GCC_LINT
1024 # define INITIALIZE(x) ((x) = 0)
1025 #else
1026 # define INITIALIZE(x)
1027 #endif
1028
1029 /* Whether memory access must strictly follow the C standard.
1030 If 0, it's OK to read uninitialized storage so long as the value is
1031 not relied upon. Defining it to 0 lets mktime access parts of
1032 struct tm that might be uninitialized, as a heuristic when the
1033 standard doesn't say what to return and when tm_gmtoff can help
1034 mktime likely infer a better value. */
1035 #ifndef UNINIT_TRAP
1036 # define UNINIT_TRAP 0
1037 #endif
1038
1039 /* strftime.c sometimes needs access to timeoff if it is not already public.
1040 tz_private_timeoff should be used only by localtime.c and strftime.c. */
1041 #if (!defined EXTERN_TIMEOFF \
1042 && defined TM_GMTOFF && (200809 < _POSIX_VERSION || ! UNINIT_TRAP))
1043 # ifndef timeoff
1044 # define timeoff tz_private_timeoff
1045 # endif
1046 # define EXTERN_TIMEOFF
1047 #endif
1048 #ifdef EXTERN_TIMEOFF
1049 time_t timeoff(struct tm *, long);
1050 #endif
1051
1052 #ifdef DEBUG
1053 # undef unreachable
1054 # define unreachable() abort()
1055 #elif !defined unreachable
1056 # ifdef __has_builtin
1057 # if __has_builtin(__builtin_unreachable)
1058 # define unreachable() __builtin_unreachable()
1059 # endif
1060 # elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
1061 # define unreachable() __builtin_unreachable()
1062 # endif
1063 # ifndef unreachable
1064 # define unreachable() ((void) 0)
1065 # endif
1066 #endif
1067
1068 /*
1069 ** For the benefit of GNU folk...
1070 ** '_(MSGID)' uses the current locale's message library string for MSGID.
1071 ** The default is to use gettext if available, and use MSGID otherwise.
1072 */
1073
1074 #if HAVE_GETTEXT
1075 # define _(msgid) gettext(msgid)
1076 #else /* !HAVE_GETTEXT */
1077 # define _(msgid) (msgid)
1078 #endif /* !HAVE_GETTEXT */
1079 #define N_(msgid) (msgid)
1080
1081 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
1082 # define TZ_DOMAIN "tz"
1083 #endif
1084
1085 #if HAVE_INCOMPATIBLE_CTIME_R
1086 # undef asctime_r
1087 # undef ctime_r
1088 char *asctime_r(struct tm const *restrict, char *restrict);
1089 char *ctime_r(time_t const *, char *);
1090 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
1091
1092 /* Handy constants that are independent of tzfile implementation. */
1093
1094 /* 2**31 - 1 as a signed integer, and usable in #if. */
1095 #define TWO_31_MINUS_1 2147483647
1096
1097 enum {
1098 SECSPERMIN = 60,
1099 MINSPERHOUR = 60,
1100 SECSPERHOUR = SECSPERMIN * MINSPERHOUR,
1101 HOURSPERDAY = 24,
1102 DAYSPERWEEK = 7,
1103 DAYSPERNYEAR = 365,
1104 DAYSPERLYEAR = DAYSPERNYEAR + 1,
1105 MONSPERYEAR = 12,
1106 YEARSPERREPEAT = 400 /* years before a Gregorian repeat */
1107 };
1108
1109 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
1110
1111 #define DAYSPERREPEAT ((int_fast32_t) 400 * 365 + 100 - 4 + 1)
1112 #define SECSPERREPEAT ((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
1113 #define AVGSECSPERYEAR (SECSPERREPEAT / YEARSPERREPEAT)
1114
1115 /* How many years to generate (in zic.c) or search through (in localtime.c).
1116 This is two years larger than the obvious 400, to avoid edge cases.
1117 E.g., suppose a rule applies from 2012 on with transitions
1118 in March and September, plus one-off transitions in November 2013,
1119 and suppose the rule cannot be expressed as a proleptic TZ string.
1120 If zic looked only at the last 400 years, it would set max_year=2413,
1121 with the intent that the 400 years 2014 through 2413 will be repeated.
1122 The last transition listed in the tzfile would be in 2413-09,
1123 less than 400 years after the last one-off transition in 2013-11.
1124 Two years is not overkill for localtime.c, as a one-year bump
1125 would mishandle 2023d's America/Ciudad_Juarez for November 2422. */
1126 enum { years_of_observations = YEARSPERREPEAT + 2 };
1127
1128 enum {
1129 TM_SUNDAY,
1130 TM_MONDAY,
1131 TM_TUESDAY,
1132 TM_WEDNESDAY,
1133 TM_THURSDAY,
1134 TM_FRIDAY,
1135 TM_SATURDAY
1136 };
1137
1138 enum {
1139 TM_JANUARY,
1140 TM_FEBRUARY,
1141 TM_MARCH,
1142 TM_APRIL,
1143 TM_MAY,
1144 TM_JUNE,
1145 TM_JULY,
1146 TM_AUGUST,
1147 TM_SEPTEMBER,
1148 TM_OCTOBER,
1149 TM_NOVEMBER,
1150 TM_DECEMBER
1151 };
1152
1153 enum {
1154 TM_YEAR_BASE = 1900,
1155 TM_WDAY_BASE = TM_MONDAY,
1156 EPOCH_YEAR = 1970,
1157 EPOCH_WDAY = TM_THURSDAY
1158 };
1159
1160 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
1161
1162 /*
1163 ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
1164 ** isleap(y) == isleap(y % 400)
1165 ** and so
1166 ** isleap(a + b) == isleap((a + b) % 400)
1167 ** or
1168 ** isleap(a + b) == isleap(a % 400 + b % 400)
1169 ** This is true even if % means modulo rather than Fortran remainder
1170 ** (which is allowed by C89 but not by C99 or later).
1171 ** We use this to avoid addition overflow problems.
1172 */
1173
1174 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
1175
1176 #endif /* !defined PRIVATE_H */
1177