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--use tzsetup"
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 offtime_r
688 # define offtime_r tz_offtime_r
689 # undef posix2time
690 # define posix2time tz_posix2time
691 # undef posix2time_z
692 # define posix2time_z tz_posix2time_z
693 # undef strftime
694 # define strftime tz_strftime
695 # undef time
696 # define time tz_time
697 # undef time2posix
698 # define time2posix tz_time2posix
699 # undef time2posix_z
700 # define time2posix_z tz_time2posix_z
701 # undef time_t
702 # define time_t tz_time_t
703 # undef timegm
704 # define timegm tz_timegm
705 # undef timelocal
706 # define timelocal tz_timelocal
707 # undef timeoff
708 # define timeoff tz_timeoff
709 # undef tzalloc
710 # define tzalloc tz_tzalloc
711 # undef tzfree
712 # define tzfree tz_tzfree
713 # undef tzset
714 # define tzset tz_tzset
715 # if SUPPORT_POSIX2008
716 # undef asctime_r
717 # define asctime_r tz_asctime_r
718 # undef ctime_r
719 # define ctime_r tz_ctime_r
720 # endif
721 # if HAVE_STRFTIME_L
722 # undef strftime_l
723 # define strftime_l tz_strftime_l
724 # endif
725 # if HAVE_TZNAME
726 # undef tzname
727 # define tzname tz_tzname
728 # endif
729 # if USG_COMPAT
730 # undef daylight
731 # define daylight tz_daylight
732 # undef timezone
733 # define timezone tz_timezone
734 # endif
735 # if ALTZONE
736 # undef altzone
737 # define altzone tz_altzone
738 # endif
739
740 # if __STDC_VERSION__ < 202311
741 # define DEPRECATED_IN_C23 /* empty */
742 # else
743 # define DEPRECATED_IN_C23 ATTRIBUTE_DEPRECATED
744 # endif
745 DEPRECATED_IN_C23 char *asctime(struct tm const *);
746 DEPRECATED_IN_C23 char *ctime(time_t const *);
747 #if SUPPORT_POSIX2008
748 char *asctime_r(struct tm const *restrict, char *restrict);
749 char *ctime_r(time_t const *, char *);
750 #endif
751 ATTRIBUTE_CONST double difftime(time_t, time_t);
752 size_t strftime(char *restrict, size_t, char const *restrict,
753 struct tm const *restrict);
754 # if HAVE_STRFTIME_L
755 size_t strftime_l(char *restrict, size_t, char const *restrict,
756 struct tm const *restrict, locale_t);
757 # endif
758 struct tm *gmtime(time_t const *);
759 struct tm *gmtime_r(time_t const *restrict, struct tm *restrict);
760 struct tm *localtime(time_t const *);
761 struct tm *localtime_r(time_t const *restrict, struct tm *restrict);
762 time_t mktime(struct tm *);
763 time_t time(time_t *);
764 time_t timegm(struct tm *);
765 void tzset(void);
766 #endif
767
768 #ifndef HAVE_DECL_TIMEGM
769 # if (202311 <= __STDC_VERSION__ \
770 || defined __GLIBC__ || defined __tm_zone /* musl */ \
771 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
772 || (defined __APPLE__ && defined __MACH__))
773 # define HAVE_DECL_TIMEGM 1
774 # else
775 # define HAVE_DECL_TIMEGM 0
776 # endif
777 #endif
778 #if !HAVE_DECL_TIMEGM && !defined timegm
779 time_t timegm(struct tm *);
780 #endif
781
782 #if !HAVE_DECL_ASCTIME_R && !defined asctime_r && SUPPORT_POSIX2008
783 extern char *asctime_r(struct tm const *restrict, char *restrict);
784 #endif
785
786 #ifndef HAVE_DECL_ENVIRON
787 # if defined environ || defined __USE_GNU
788 # define HAVE_DECL_ENVIRON 1
789 # else
790 # define HAVE_DECL_ENVIRON 0
791 # endif
792 #endif
793
794 #if !HAVE_DECL_ENVIRON
795 extern char **environ;
796 #endif
797
798 #if 2 <= HAVE_TZNAME + (TZ_TIME_T || !HAVE_POSIX_DECLS)
799 extern char *tzname[];
800 #endif
801 #if 2 <= USG_COMPAT + (TZ_TIME_T || !HAVE_POSIX_DECLS)
802 extern long timezone;
803 extern int daylight;
804 #endif
805 #if 2 <= ALTZONE + (TZ_TIME_T || !HAVE_POSIX_DECLS)
806 extern long altzone;
807 #endif
808
809 /*
810 ** The STD_INSPIRED functions are similar, but most also need
811 ** declarations if time_tz is defined.
812 */
813
814 #ifndef STD_INSPIRED
815 # ifdef __NetBSD__
816 # define STD_INSPIRED 1
817 # else
818 # define STD_INSPIRED 0
819 # endif
820 #endif
821 #if STD_INSPIRED
822 # if TZ_TIME_T || !defined offtime
823 struct tm *offtime(time_t const *, long);
824 # endif
825 # if TZ_TIME_T || !defined offtime_r
826 struct tm *offtime_r(time_t const *, long, struct tm *);
827 # endif
828 # if TZ_TIME_T || !defined timelocal
829 time_t timelocal(struct tm *);
830 # endif
831 # if TZ_TIME_T || !defined timeoff
832 # define EXTERN_TIMEOFF
833 # endif
834 # if TZ_TIME_T || !defined time2posix
835 time_t time2posix(time_t);
836 # endif
837 # if TZ_TIME_T || !defined posix2time
838 time_t posix2time(time_t);
839 # endif
840 #endif
841
842 /* Infer TM_ZONE on systems where this information is known, but suppress
843 guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */
844 #if (200809 < _POSIX_VERSION \
845 || defined __GLIBC__ \
846 || defined __tm_zone /* musl */ \
847 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
848 || (defined __APPLE__ && defined __MACH__))
849 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF
850 # define TM_GMTOFF tm_gmtoff
851 # endif
852 # if !defined TM_ZONE && !defined NO_TM_ZONE
853 # define TM_ZONE tm_zone
854 # endif
855 #endif
856
857 /*
858 ** Define functions that are ABI compatible with NetBSD but have
859 ** better prototypes. NetBSD 6.1.4 defines a pointer type timezone_t
860 ** and labors under the misconception that 'const timezone_t' is a
861 ** pointer to a constant. This use of 'const' is ineffective, so it
862 ** is not done here. What we call 'struct state' NetBSD calls
863 ** 'struct __state', but this is a private name so it doesn't matter.
864 */
865 #if NETBSD_INSPIRED
866 typedef struct state *timezone_t;
867 struct tm *localtime_rz(timezone_t restrict, time_t const *restrict,
868 struct tm *restrict);
869 time_t mktime_z(timezone_t restrict, struct tm *restrict);
870 timezone_t tzalloc(char const *);
871 void tzfree(timezone_t);
872 # if STD_INSPIRED
873 # if TZ_TIME_T || !defined posix2time_z
874 ATTRIBUTE_PURE time_t posix2time_z(timezone_t, time_t);
875 # endif
876 # if TZ_TIME_T || !defined time2posix_z
877 ATTRIBUTE_PURE time_t time2posix_z(timezone_t, time_t);
878 # endif
879 # endif
880 #endif
881
882 /*
883 ** Finally, some convenience items.
884 */
885
886 #define TYPE_BIT(type) (CHAR_BIT * (ptrdiff_t) sizeof(type))
887 #define TYPE_SIGNED(type) (((type) -1) < 0)
888 #define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
889
890 /* Minimum and maximum of two values. Use lower case to avoid
891 naming clashes with standard include files. */
892 #define max(a, b) ((a) > (b) ? (a) : (b))
893 #define min(a, b) ((a) < (b) ? (a) : (b))
894
895 /* Max and min values of the integer type T, of which only the bottom
896 B bits are used, and where the highest-order used bit is considered
897 to be a sign bit if T is signed. */
898 #define MAXVAL(t, b) \
899 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
900 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
901 #define MINVAL(t, b) \
902 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
903
904 /* The extreme time values, assuming no padding. */
905 #define TIME_T_MIN_NO_PADDING MINVAL(time_t, TYPE_BIT(time_t))
906 #define TIME_T_MAX_NO_PADDING MAXVAL(time_t, TYPE_BIT(time_t))
907
908 /* The extreme time values. These are macros, not constants, so that
909 any portability problems occur only when compiling .c files that use
910 the macros, which is safer for applications that need only zdump and zic.
911 This implementation assumes no padding if time_t is signed and
912 either the compiler lacks support for _Generic or time_t is not one
913 of the standard signed integer types. */
914 #if HAVE__GENERIC
915 # define TIME_T_MIN \
916 _Generic((time_t) 0, \
917 signed char: SCHAR_MIN, short: SHRT_MIN, \
918 int: INT_MIN, long: LONG_MIN, long long: LLONG_MIN, \
919 default: TIME_T_MIN_NO_PADDING)
920 # define TIME_T_MAX \
921 (TYPE_SIGNED(time_t) \
922 ? _Generic((time_t) 0, \
923 signed char: SCHAR_MAX, short: SHRT_MAX, \
924 int: INT_MAX, long: LONG_MAX, long long: LLONG_MAX, \
925 default: TIME_T_MAX_NO_PADDING) \
926 : (time_t) -1)
927 enum { SIGNED_PADDING_CHECK_NEEDED
928 = _Generic((time_t) 0,
929 signed char: false, short: false,
930 int: false, long: false, long long: false,
931 default: true) };
932 #else
933 # define TIME_T_MIN TIME_T_MIN_NO_PADDING
934 # define TIME_T_MAX TIME_T_MAX_NO_PADDING
935 enum { SIGNED_PADDING_CHECK_NEEDED = true };
936 #endif
937 /* Try to check the padding assumptions. Although TIME_T_MAX and the
938 following check can both have undefined behavior on oddball
939 platforms due to shifts exceeding widths of signed integers, these
940 platforms' compilers are likely to diagnose these issues in integer
941 constant expressions, so it shouldn't hurt to check statically. */
942 static_assert(! TYPE_SIGNED(time_t) || ! SIGNED_PADDING_CHECK_NEEDED
943 || TIME_T_MAX >> (TYPE_BIT(time_t) - 2) == 1);
944
945 /*
946 ** 302 / 1000 is log10(2.0) rounded up.
947 ** Subtract one for the sign bit if the type is signed;
948 ** add one for integer division truncation;
949 ** add one more for a minus sign if the type is signed.
950 */
951 #define INT_STRLEN_MAXIMUM(type) \
952 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
953 1 + TYPE_SIGNED(type))
954
955 /*
956 ** INITIALIZE(x)
957 */
958
959 #ifdef GCC_LINT
960 # define INITIALIZE(x) ((x) = 0)
961 #else
962 # define INITIALIZE(x)
963 #endif
964
965 /* Whether memory access must strictly follow the C standard.
966 If 0, it's OK to read uninitialized storage so long as the value is
967 not relied upon. Defining it to 0 lets mktime access parts of
968 struct tm that might be uninitialized, as a heuristic when the
969 standard doesn't say what to return and when tm_gmtoff can help
970 mktime likely infer a better value. */
971 #ifndef UNINIT_TRAP
972 # define UNINIT_TRAP 0
973 #endif
974
975 /* strftime.c sometimes needs access to timeoff if it is not already public.
976 tz_private_timeoff should be used only by localtime.c and strftime.c. */
977 #if (!defined EXTERN_TIMEOFF \
978 && defined TM_GMTOFF && (200809 < _POSIX_VERSION || ! UNINIT_TRAP))
979 # ifndef timeoff
980 # define timeoff tz_private_timeoff
981 # endif
982 # define EXTERN_TIMEOFF
983 #endif
984 #ifdef EXTERN_TIMEOFF
985 time_t timeoff(struct tm *, long);
986 #endif
987
988 #ifdef DEBUG
989 # undef unreachable
990 # define unreachable() abort()
991 #elif !defined unreachable
992 # ifdef __has_builtin
993 # if __has_builtin(__builtin_unreachable)
994 # define unreachable() __builtin_unreachable()
995 # endif
996 # elif 4 < __GNUC__ + (5 <= __GNUC_MINOR__)
997 # define unreachable() __builtin_unreachable()
998 # endif
999 # ifndef unreachable
1000 # define unreachable() ((void) 0)
1001 # endif
1002 #endif
1003
1004 /*
1005 ** For the benefit of GNU folk...
1006 ** '_(MSGID)' uses the current locale's message library string for MSGID.
1007 ** The default is to use gettext if available, and use MSGID otherwise.
1008 */
1009
1010 #if HAVE_GETTEXT
1011 #define _(msgid) gettext(msgid)
1012 #else /* !HAVE_GETTEXT */
1013 #define _(msgid) msgid
1014 #endif /* !HAVE_GETTEXT */
1015
1016 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT
1017 # define TZ_DOMAIN "tz"
1018 #endif
1019
1020 #if HAVE_INCOMPATIBLE_CTIME_R
1021 #undef asctime_r
1022 #undef ctime_r
1023 char *asctime_r(struct tm const *restrict, char *restrict);
1024 char *ctime_r(time_t const *, char *);
1025 #endif /* HAVE_INCOMPATIBLE_CTIME_R */
1026
1027 /* Handy macros that are independent of tzfile implementation. */
1028
1029 enum {
1030 SECSPERMIN = 60,
1031 MINSPERHOUR = 60,
1032 SECSPERHOUR = SECSPERMIN * MINSPERHOUR,
1033 HOURSPERDAY = 24,
1034 DAYSPERWEEK = 7,
1035 DAYSPERNYEAR = 365,
1036 DAYSPERLYEAR = DAYSPERNYEAR + 1,
1037 MONSPERYEAR = 12,
1038 YEARSPERREPEAT = 400 /* years before a Gregorian repeat */
1039 };
1040
1041 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
1042
1043 #define DAYSPERREPEAT ((int_fast32_t) 400 * 365 + 100 - 4 + 1)
1044 #define SECSPERREPEAT ((int_fast64_t) DAYSPERREPEAT * SECSPERDAY)
1045 #define AVGSECSPERYEAR (SECSPERREPEAT / YEARSPERREPEAT)
1046
1047 /* How many years to generate (in zic.c) or search through (in localtime.c).
1048 This is two years larger than the obvious 400, to avoid edge cases.
1049 E.g., suppose a rule applies from 2012 on with transitions
1050 in March and September, plus one-off transitions in November 2013,
1051 and suppose the rule cannot be expressed as a proleptic TZ string.
1052 If zic looked only at the last 400 years, it would set max_year=2413,
1053 with the intent that the 400 years 2014 through 2413 will be repeated.
1054 The last transition listed in the tzfile would be in 2413-09,
1055 less than 400 years after the last one-off transition in 2013-11.
1056 Two years is not overkill for localtime.c, as a one-year bump
1057 would mishandle 2023d's America/Ciudad_Juarez for November 2422. */
1058 enum { years_of_observations = YEARSPERREPEAT + 2 };
1059
1060 enum {
1061 TM_SUNDAY,
1062 TM_MONDAY,
1063 TM_TUESDAY,
1064 TM_WEDNESDAY,
1065 TM_THURSDAY,
1066 TM_FRIDAY,
1067 TM_SATURDAY
1068 };
1069
1070 enum {
1071 TM_JANUARY,
1072 TM_FEBRUARY,
1073 TM_MARCH,
1074 TM_APRIL,
1075 TM_MAY,
1076 TM_JUNE,
1077 TM_JULY,
1078 TM_AUGUST,
1079 TM_SEPTEMBER,
1080 TM_OCTOBER,
1081 TM_NOVEMBER,
1082 TM_DECEMBER
1083 };
1084
1085 enum {
1086 TM_YEAR_BASE = 1900,
1087 TM_WDAY_BASE = TM_MONDAY,
1088 EPOCH_YEAR = 1970,
1089 EPOCH_WDAY = TM_THURSDAY
1090 };
1091
1092 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
1093
1094 /*
1095 ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
1096 ** isleap(y) == isleap(y % 400)
1097 ** and so
1098 ** isleap(a + b) == isleap((a + b) % 400)
1099 ** or
1100 ** isleap(a + b) == isleap(a % 400 + b % 400)
1101 ** This is true even if % means modulo rather than Fortran remainder
1102 ** (which is allowed by C89 but not by C99 or later).
1103 ** We use this to avoid addition overflow problems.
1104 */
1105
1106 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
1107
1108 #endif /* !defined PRIVATE_H */
1109