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