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