1 /* $NetBSD: zdump.c,v 1.40 2014/10/23 21:19:53 christos Exp $ */ 2 /* 3 ** This file is in the public domain, so clarified as of 4 ** 2009-05-17 by Arthur David Olson. 5 */ 6 7 #include <sys/cdefs.h> 8 #ifndef lint 9 __RCSID("$NetBSD: zdump.c,v 1.40 2014/10/23 21:19:53 christos Exp $"); 10 #endif /* !defined lint */ 11 12 /* 13 ** This code has been made independent of the rest of the time 14 ** conversion package to increase confidence in the verification it provides. 15 ** You can use this code to help in verifying other implementations. 16 ** 17 ** To do this, compile with -DUSE_LTZ=0 and link without the tz library. 18 */ 19 20 #ifndef NETBSD_INSPIRED 21 # define NETBSD_INSPIRED 1 22 #endif 23 #ifndef USE_LTZ 24 # define USE_LTZ 1 25 #endif 26 27 #if USE_LTZ 28 #include "private.h" 29 #endif 30 31 /* Enable tm_gmtoff and tm_zone on GNUish systems. */ 32 #define _GNU_SOURCE 1 33 /* Enable strtoimax on Solaris 10. */ 34 #define __EXTENSIONS__ 1 35 36 #include "stdio.h" /* for stdout, stderr */ 37 #include "string.h" /* for strcpy */ 38 #include "sys/types.h" /* for time_t */ 39 #include "time.h" /* for struct tm */ 40 #include "stdlib.h" /* for exit, malloc, atoi */ 41 #include "limits.h" /* for CHAR_BIT, LLONG_MAX */ 42 #include <errno.h> 43 #include <err.h> 44 45 /* 46 ** Substitutes for pre-C99 compilers. 47 ** Much of this section of code is stolen from private.h. 48 */ 49 50 #ifndef HAVE_STDINT_H 51 # define HAVE_STDINT_H \ 52 (199901 <= __STDC_VERSION__ \ 53 || 2 < __GLIBC__ + (1 <= __GLIBC_MINOR__) \ 54 || __CYGWIN__) 55 #endif 56 #if HAVE_STDINT_H 57 # include "stdint.h" 58 #endif 59 #ifndef HAVE_INTTYPES_H 60 # define HAVE_INTTYPES_H HAVE_STDINT_H 61 #endif 62 #if HAVE_INTTYPES_H 63 # include <inttypes.h> 64 #endif 65 66 #ifndef INT_FAST32_MAX 67 # if INT_MAX >> 31 == 0 68 typedef long int_fast32_t; 69 # else 70 typedef int int_fast32_t; 71 # endif 72 #endif 73 74 /* Pre-C99 GCC compilers define __LONG_LONG_MAX__ instead of LLONG_MAX. */ 75 #if !defined LLONG_MAX && defined __LONG_LONG_MAX__ 76 # define LLONG_MAX __LONG_LONG_MAX__ 77 #endif 78 79 #ifndef INTMAX_MAX 80 # ifdef LLONG_MAX 81 typedef long long intmax_t; 82 # define strtoimax strtoll 83 # define INTMAX_MAX LLONG_MAX 84 # else 85 typedef long intmax_t; 86 # define strtoimax strtol 87 # define INTMAX_MAX LONG_MAX 88 # endif 89 #endif 90 91 #ifndef PRIdMAX 92 # if INTMAX_MAX == LLONG_MAX 93 # define PRIdMAX "lld" 94 # else 95 # define PRIdMAX "ld" 96 # endif 97 #endif 98 99 /* Infer TM_ZONE on systems where this information is known, but suppress 100 guessing if NO_TM_ZONE is defined. Similarly for TM_GMTOFF. */ 101 #if (defined __GLIBC__ \ 102 || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \ 103 || (defined __APPLE__ && defined __MACH__)) 104 # if !defined TM_GMTOFF && !defined NO_TM_GMTOFF 105 # define TM_GMTOFF tm_gmtoff 106 # endif 107 # if !defined TM_ZONE && !defined NO_TM_ZONE 108 # define TM_ZONE tm_zone 109 # endif 110 #endif 111 112 #ifndef HAVE_LOCALTIME_R 113 # define HAVE_LOCALTIME_R 1 114 #endif 115 116 #ifndef HAVE_LOCALTIME_RZ 117 # ifdef TM_ZONE 118 # define HAVE_LOCALTIME_RZ (NETBSD_INSPIRED && USE_LTZ) 119 # else 120 # define HAVE_LOCALTIME_RZ 0 121 # endif 122 #endif 123 124 #ifndef HAVE_TZSET 125 # define HAVE_TZSET 1 126 #endif 127 128 #ifndef ZDUMP_LO_YEAR 129 #define ZDUMP_LO_YEAR (-500) 130 #endif /* !defined ZDUMP_LO_YEAR */ 131 132 #ifndef ZDUMP_HI_YEAR 133 #define ZDUMP_HI_YEAR 2500 134 #endif /* !defined ZDUMP_HI_YEAR */ 135 136 #ifndef MAX_STRING_LENGTH 137 #define MAX_STRING_LENGTH 1024 138 #endif /* !defined MAX_STRING_LENGTH */ 139 140 #if __STDC_VERSION__ < 199901 141 # define true 1 142 # define false 0 143 # define bool int 144 #else 145 # include <stdbool.h> 146 #endif 147 148 #ifndef EXIT_SUCCESS 149 #define EXIT_SUCCESS 0 150 #endif /* !defined EXIT_SUCCESS */ 151 152 #ifndef EXIT_FAILURE 153 #define EXIT_FAILURE 1 154 #endif /* !defined EXIT_FAILURE */ 155 156 #ifndef SECSPERMIN 157 #define SECSPERMIN 60 158 #endif /* !defined SECSPERMIN */ 159 160 #ifndef MINSPERHOUR 161 #define MINSPERHOUR 60 162 #endif /* !defined MINSPERHOUR */ 163 164 #ifndef SECSPERHOUR 165 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR) 166 #endif /* !defined SECSPERHOUR */ 167 168 #ifndef HOURSPERDAY 169 #define HOURSPERDAY 24 170 #endif /* !defined HOURSPERDAY */ 171 172 #ifndef EPOCH_YEAR 173 #define EPOCH_YEAR 1970 174 #endif /* !defined EPOCH_YEAR */ 175 176 #ifndef TM_YEAR_BASE 177 #define TM_YEAR_BASE 1900 178 #endif /* !defined TM_YEAR_BASE */ 179 180 #ifndef DAYSPERNYEAR 181 #define DAYSPERNYEAR 365 182 #endif /* !defined DAYSPERNYEAR */ 183 184 #ifndef isleap 185 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0)) 186 #endif /* !defined isleap */ 187 188 #ifndef isleap_sum 189 /* 190 ** See tzfile.h for details on isleap_sum. 191 */ 192 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400) 193 #endif /* !defined isleap_sum */ 194 195 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY) 196 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR) 197 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY) 198 #define SECSPER400YEARS (SECSPERNYEAR * (intmax_t) (300 + 3) \ 199 + SECSPERLYEAR * (intmax_t) (100 - 3)) 200 201 /* 202 ** True if SECSPER400YEARS is known to be representable as an 203 ** intmax_t. It's OK that SECSPER400YEARS_FITS can in theory be false 204 ** even if SECSPER400YEARS is representable, because when that happens 205 ** the code merely runs a bit more slowly, and this slowness doesn't 206 ** occur on any practical platform. 207 */ 208 enum { SECSPER400YEARS_FITS = SECSPERLYEAR <= INTMAX_MAX / 400 }; 209 210 #ifndef HAVE_GETTEXT 211 #define HAVE_GETTEXT 0 212 #endif 213 #if HAVE_GETTEXT 214 #include "locale.h" /* for setlocale */ 215 #include "libintl.h" 216 #endif /* HAVE_GETTEXT */ 217 218 #ifndef ATTRIBUTE_PURE 219 #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__) 220 # define ATTRIBUTE_PURE __attribute__ ((ATTRIBUTE_PURE__)) 221 #else 222 # define ATTRIBUTE_PURE /* empty */ 223 #endif 224 #endif 225 226 #ifndef INITIALIZE 227 #if defined(__GNUC__) || defined(__lint__) 228 #define INITIALIZE(x) ((x) = 0) 229 #else /* !defined GNUC || lint */ 230 #define INITIALIZE(x) 231 #endif /* !defined GNUC || lint */ 232 #endif /* !defined INITIALIZE */ 233 234 /* 235 ** For the benefit of GNU folk... 236 ** '_(MSGID)' uses the current locale's message library string for MSGID. 237 ** The default is to use gettext if available, and use MSGID otherwise. 238 */ 239 240 #ifndef _ 241 #if HAVE_GETTEXT 242 #define _(msgid) gettext(msgid) 243 #else /* !HAVE_GETTEXT */ 244 #define _(msgid) msgid 245 #endif /* !HAVE_GETTEXT */ 246 #endif /* !defined _ */ 247 248 #if !defined TZ_DOMAIN && defined HAVE_GETTEXT 249 # define TZ_DOMAIN "tz" 250 #endif 251 252 #if ! HAVE_LOCALTIME_RZ 253 # undef timezone_t 254 # define timezone_t char ** 255 #endif 256 257 extern char ** environ; 258 extern int getopt(int argc, char * const argv[], 259 const char * options); 260 extern char * optarg; 261 extern int optind; 262 263 /* The minimum and maximum finite time values. */ 264 static time_t absolute_min_time = 265 ((time_t) -1 < 0 266 ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1) 267 : 0); 268 static time_t absolute_max_time = 269 ((time_t) -1 < 0 270 ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)) 271 : -1); 272 static size_t longest; 273 static char * progname; 274 static bool warned; 275 static bool errout; 276 277 static char const *abbr(struct tm const *); 278 static intmax_t delta(struct tm *, struct tm *) ATTRIBUTE_PURE; 279 static void dumptime(struct tm const *); 280 static time_t hunt(timezone_t, char *, time_t, time_t); 281 static void show(timezone_t, char *, time_t, bool); 282 static const char *tformat(void); 283 static time_t yeartot(intmax_t) ATTRIBUTE_PURE; 284 285 /* Is A an alphabetic character in the C locale? */ 286 static bool 287 is_alpha(char a) 288 { 289 switch (a) { 290 default: 291 return false; 292 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 293 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': 294 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': 295 case 'V': case 'W': case 'X': case 'Y': case 'Z': 296 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': 297 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': 298 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': 299 case 'v': case 'w': case 'x': case 'y': case 'z': 300 return true; 301 } 302 } 303 304 /* Return A + B, exiting if the result would overflow. */ 305 static size_t 306 sumsize(size_t a, size_t b) 307 { 308 size_t sum = a + b; 309 if (sum < a) 310 errx(EXIT_FAILURE, "size overflow"); 311 return sum; 312 } 313 314 #if ! HAVE_TZSET 315 # undef tzset 316 # define tzset zdump_tzset 317 static void tzset(void) { } 318 #endif 319 320 /* Assume gmtime_r works if localtime_r does. 321 A replacement localtime_r is defined below if needed. */ 322 #if ! HAVE_LOCALTIME_R 323 324 # undef gmtime_r 325 # define gmtime_r zdump_gmtime_r 326 327 static struct tm * 328 gmtime_r(time_t *tp, struct tm *tmp) 329 { 330 struct tm *r = gmtime(tp); 331 if (r) { 332 *tmp = *r; 333 r = tmp; 334 } 335 return r; 336 } 337 338 #endif 339 340 /* Platforms with TM_ZONE don't need tzname, so they can use the 341 faster localtime_rz or localtime_r if available. */ 342 343 #if defined TM_ZONE && HAVE_LOCALTIME_RZ 344 # define USE_LOCALTIME_RZ true 345 #else 346 # define USE_LOCALTIME_RZ false 347 #endif 348 349 #if ! USE_LOCALTIME_RZ 350 351 # if !defined TM_ZONE || ! HAVE_LOCALTIME_R || ! HAVE_TZSET 352 # undef localtime_r 353 # define localtime_r zdump_localtime_r 354 static struct tm * 355 localtime_r(time_t *tp, struct tm *tmp) 356 { 357 struct tm *r = localtime(tp); 358 if (r) { 359 *tmp = *r; 360 r = tmp; 361 } 362 return r; 363 } 364 # endif 365 366 # undef localtime_rz 367 # define localtime_rz zdump_localtime_rz 368 static struct tm * 369 localtime_rz(timezone_t rz, time_t *tp, struct tm *tmp) 370 { 371 return localtime_r(tp, tmp); 372 } 373 374 # ifdef TYPECHECK 375 # undef mktime_z 376 # define mktime_z zdump_mktime_z 377 static time_t 378 mktime_z(timezone_t tz, struct tm *tmp) 379 { 380 return mktime(tmp); 381 } 382 # endif 383 384 # undef tzalloc 385 # undef tzfree 386 # define tzalloc zdump_tzalloc 387 # define tzfree zdump_tzfree 388 389 static timezone_t 390 tzalloc(char const *val) 391 { 392 static char **fakeenv; 393 char **env = fakeenv; 394 char *env0; 395 if (! env) { 396 char **e = environ; 397 int to; 398 399 while (*e++) 400 continue; 401 env = malloc(sumsize(sizeof *environ, 402 (e - environ) * sizeof *environ)); 403 if (! env) { 404 err(EXIT_FAILURE, "malloc"); 405 } 406 to = 1; 407 for (e = environ; (env[to] = *e); e++) 408 to += strncmp(*e, "TZ=", 3) != 0; 409 } 410 env0 = malloc(sumsize(sizeof "TZ=", strlen(val))); 411 if (! env0) { 412 err(EXIT_FAILURE, "malloc"); 413 } 414 env[0] = strcat(strcpy(env0, "TZ="), val); 415 environ = fakeenv = env; 416 tzset(); 417 return env; 418 } 419 420 static void 421 tzfree(timezone_t env) 422 { 423 environ = env + 1; 424 free(env[0]); 425 } 426 #endif /* ! USE_LOCALTIME_RZ */ 427 428 /* A UTC time zone, and its initializer. */ 429 static timezone_t gmtz; 430 static void 431 gmtzinit(void) 432 { 433 if (USE_LOCALTIME_RZ) { 434 static char const utc[] = "UTC0"; 435 gmtz = tzalloc(utc); 436 if (!gmtz) { 437 err(EXIT_FAILURE, "Cannot create %s", utc); 438 } 439 } 440 } 441 442 /* Convert *TP to UTC, storing the broken-down time into *TMP. 443 Return TMP if successful, NULL otherwise. This is like gmtime_r(TP, TMP), 444 except typically faster if USE_LOCALTIME_RZ. */ 445 static struct tm * 446 my_gmtime_r(time_t *tp, struct tm *tmp) 447 { 448 return USE_LOCALTIME_RZ ? 449 localtime_rz(gmtz, tp, tmp) : gmtime_r(tp, tmp); 450 } 451 452 #ifndef TYPECHECK 453 #define my_localtime_rz localtime_rz 454 #else /* !defined TYPECHECK */ 455 static struct tm * 456 my_localtime_rz(timezone_t tz, const time_t *tp, struct tm *tmp) 457 { 458 tmp = localtime_rz(tz, tp, tmp); 459 if (tmp) { 460 struct tm tm; 461 time_t t; 462 463 tm = *tmp; 464 t = mktime_z(tz, &tm); 465 if (t != *tp) { 466 (void) fflush(stdout); 467 (void) fprintf(stderr, "\n%s: ", progname); 468 (void) fprintf(stderr, tformat(), *tp); 469 (void) fprintf(stderr, " ->"); 470 (void) fprintf(stderr, " year=%d", tmp->tm_year); 471 (void) fprintf(stderr, " mon=%d", tmp->tm_mon); 472 (void) fprintf(stderr, " mday=%d", tmp->tm_mday); 473 (void) fprintf(stderr, " hour=%d", tmp->tm_hour); 474 (void) fprintf(stderr, " min=%d", tmp->tm_min); 475 (void) fprintf(stderr, " sec=%d", tmp->tm_sec); 476 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst); 477 (void) fprintf(stderr, " -> "); 478 (void) fprintf(stderr, tformat(), t); 479 (void) fprintf(stderr, "\n"); 480 errout = true; 481 } 482 } 483 return tmp; 484 } 485 #endif /* !defined TYPECHECK */ 486 487 static void 488 abbrok(const char *const abbrp, const char *const zone) 489 { 490 const char *cp; 491 const char *wp; 492 493 if (warned) 494 return; 495 cp = abbrp; 496 wp = NULL; 497 while (is_alpha(*cp)) 498 ++cp; 499 if (cp - abbrp == 0) 500 wp = _("lacks alphabetic at start"); 501 else if (cp - abbrp < 3) 502 wp = _("has fewer than 3 alphabetics"); 503 else if (cp - abbrp > 6) 504 wp = _("has more than 6 alphabetics"); 505 if (wp == NULL && (*cp == '+' || *cp == '-')) { 506 ++cp; 507 if ('0' <= *cp && *cp <= '9') 508 if (*cp++ == '1' && '0' <= *cp && *cp <= '4') 509 cp++; 510 if (*cp != '\0') 511 wp = _("differs from POSIX standard"); 512 } 513 if (wp == NULL) 514 return; 515 (void) fflush(stdout); 516 (void) fprintf(stderr, 517 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"), 518 progname, zone, abbrp, wp); 519 warned = errout = true; 520 } 521 522 /* Return a time zone abbreviation. If the abbreviation needs to be 523 saved, use *BUF (of size *BUFALLOC) to save it, and return the 524 abbreviation in the possibly-reallocated *BUF. Otherwise, just 525 return the abbreviation. Get the abbreviation from TMP. 526 Exit on memory allocation failure. */ 527 static char const * 528 saveabbr(char **buf, size_t *bufalloc, struct tm const *tmp) 529 { 530 char const *ab = abbr(tmp); 531 if (HAVE_LOCALTIME_RZ) 532 return ab; 533 else { 534 size_t ablen = strlen(ab); 535 if (*bufalloc <= ablen) { 536 free(*buf); 537 538 /* Make the new buffer at least twice as long as the 539 old, to avoid O(N**2) behavior on repeated calls. */ 540 *bufalloc = sumsize(*bufalloc, ablen + 1); 541 *buf = malloc(*bufalloc); 542 if (! *buf) { 543 err(EXIT_FAILURE, "malloc"); 544 } 545 } 546 return strcpy(*buf, ab); 547 } 548 } 549 550 static void 551 close_file(FILE *stream) 552 { 553 char const *e = (ferror(stream) ? _("I/O error") 554 : fclose(stream) != 0 ? strerror(errno) : NULL); 555 if (e) { 556 errx(EXIT_FAILURE, "%s", e); 557 } 558 } 559 560 __dead static void 561 usage(FILE *const stream, const int status) 562 { 563 (void) fprintf(stream, 564 _("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n" 565 "\n" 566 "Report bugs to %s.\n"), 567 progname, progname, REPORT_BUGS_TO); 568 if (status == EXIT_SUCCESS) 569 close_file(stream); 570 exit(status); 571 } 572 573 int 574 main(int argc, char *argv[]) 575 { 576 /* These are static so that they're initially zero. */ 577 static char * abbrev; 578 static size_t abbrevsize; 579 static struct tm newtm; 580 581 int i; 582 bool vflag; 583 bool Vflag; 584 char * cutarg; 585 char * cuttimes; 586 time_t cutlotime; 587 time_t cuthitime; 588 time_t now; 589 time_t t; 590 time_t newt; 591 struct tm tm; 592 struct tm * tmp; 593 struct tm * newtmp; 594 595 cutlotime = absolute_min_time; 596 cuthitime = absolute_max_time; 597 #if HAVE_GETTEXT 598 (void) setlocale(LC_ALL, ""); 599 #ifdef TZ_DOMAINDIR 600 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR); 601 #endif /* defined TEXTDOMAINDIR */ 602 (void) textdomain(TZ_DOMAIN); 603 #endif /* HAVE_GETTEXT */ 604 progname = argv[0]; 605 for (i = 1; i < argc; ++i) 606 if (strcmp(argv[i], "--version") == 0) { 607 (void) printf("zdump %s%s\n", PKGVERSION, TZVERSION); 608 return EXIT_SUCCESS; 609 } else if (strcmp(argv[i], "--help") == 0) { 610 usage(stdout, EXIT_SUCCESS); 611 } 612 vflag = Vflag = false; 613 cutarg = cuttimes = NULL; 614 for (;;) 615 switch (getopt(argc, argv, "c:t:vV")) { 616 case 'c': cutarg = optarg; break; 617 case 't': cuttimes = optarg; break; 618 case 'v': vflag = true; break; 619 case 'V': Vflag = true; break; 620 case -1: 621 if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) 622 goto arg_processing_done; 623 /* Fall through. */ 624 default: 625 usage(stderr, EXIT_FAILURE); 626 } 627 arg_processing_done:; 628 629 if (vflag | Vflag) { 630 intmax_t lo; 631 intmax_t hi; 632 char *loend, *hiend; 633 intmax_t cutloyear = ZDUMP_LO_YEAR; 634 intmax_t cuthiyear = ZDUMP_HI_YEAR; 635 if (cutarg != NULL) { 636 lo = strtoimax(cutarg, &loend, 10); 637 if (cutarg != loend && !*loend) { 638 hi = lo; 639 cuthiyear = hi; 640 } else if (cutarg != loend && *loend == ',' 641 && (hi = strtoimax(loend + 1, &hiend, 10), 642 loend + 1 != hiend && !*hiend)) { 643 cutloyear = lo; 644 cuthiyear = hi; 645 } else { 646 fprintf(stderr, _("%s: wild -c argument %s\n"), 647 progname, cutarg); 648 return EXIT_FAILURE; 649 } 650 } 651 if (cutarg != NULL || cuttimes == NULL) { 652 cutlotime = yeartot(cutloyear); 653 cuthitime = yeartot(cuthiyear); 654 } 655 if (cuttimes != NULL) { 656 lo = strtoimax(cuttimes, &loend, 10); 657 if (cuttimes != loend && !*loend) { 658 hi = lo; 659 if (hi < cuthitime) { 660 if (hi < absolute_min_time) 661 hi = absolute_min_time; 662 cuthitime = hi; 663 } 664 } else if (cuttimes != loend && *loend == ',' 665 && (hi = strtoimax(loend + 1, &hiend, 10), 666 loend + 1 != hiend && !*hiend)) { 667 if (cutlotime < lo) { 668 if (absolute_max_time < lo) 669 lo = absolute_max_time; 670 cutlotime = lo; 671 } 672 if (hi < cuthitime) { 673 if (hi < absolute_min_time) 674 hi = absolute_min_time; 675 cuthitime = hi; 676 } 677 } else { 678 (void) fprintf(stderr, 679 _("%s: wild -t argument %s\n"), 680 progname, cuttimes); 681 return EXIT_FAILURE; 682 } 683 } 684 } 685 gmtzinit(); 686 now = time(NULL); 687 longest = 0; 688 for (i = optind; i < argc; i++) { 689 size_t arglen = strlen(argv[i]); 690 if (longest < arglen) 691 longest = arglen < INT_MAX ? arglen : INT_MAX; 692 } 693 694 for (i = optind; i < argc; ++i) { 695 timezone_t tz = tzalloc(argv[i]); 696 char const *ab; 697 if (!tz) { 698 errx(EXIT_FAILURE, "%s", argv[i]); 699 } 700 if (! (vflag | Vflag)) { 701 show(tz, argv[i], now, false); 702 tzfree(tz); 703 continue; 704 } 705 warned = false; 706 t = absolute_min_time; 707 if (!Vflag) { 708 show(tz, argv[i], t, true); 709 t += SECSPERDAY; 710 show(tz, argv[i], t, true); 711 } 712 if (t < cutlotime) 713 t = cutlotime; 714 tmp = my_localtime_rz(tz, &t, &tm); 715 if (tmp) 716 ab = saveabbr(&abbrev, &abbrevsize, &tm); 717 else 718 ab = NULL; 719 while (t < cuthitime) { 720 newt = ((t < absolute_max_time - SECSPERDAY / 2 721 && t + SECSPERDAY / 2 < cuthitime) 722 ? t + SECSPERDAY / 2 723 : cuthitime); 724 newtmp = localtime_rz(tz, &newt, &newtm); 725 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) : 726 (delta(&newtm, &tm) != (newt - t) || 727 newtm.tm_isdst != tm.tm_isdst || 728 strcmp(abbr(&newtm), ab) != 0)) { 729 newt = hunt(tz, argv[i], t, newt); 730 newtmp = localtime_rz(tz, &newt, &newtm); 731 if (newtmp) 732 ab = saveabbr(&abbrev, &abbrevsize, 733 &newtm); 734 } 735 t = newt; 736 tm = newtm; 737 tmp = newtmp; 738 } 739 if (!Vflag) { 740 t = absolute_max_time; 741 t -= SECSPERDAY; 742 show(tz, argv[i], t, true); 743 t += SECSPERDAY; 744 show(tz, argv[i], t, true); 745 } 746 tzfree(tz); 747 } 748 close_file(stdout); 749 if (errout && (ferror(stderr) || fclose(stderr) != 0)) 750 return EXIT_FAILURE; 751 return EXIT_SUCCESS; 752 } 753 754 static time_t 755 yeartot(const intmax_t y) 756 { 757 intmax_t myy, seconds, years; 758 time_t t; 759 760 myy = EPOCH_YEAR; 761 t = 0; 762 while (myy < y) { 763 if (SECSPER400YEARS_FITS && 400 <= y - myy) { 764 intmax_t diff400 = (y - myy) / 400; 765 if (INTMAX_MAX / SECSPER400YEARS < diff400) 766 return absolute_max_time; 767 seconds = diff400 * SECSPER400YEARS; 768 years = diff400 * 400; 769 } else { 770 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR; 771 years = 1; 772 } 773 myy += years; 774 if (t > absolute_max_time - seconds) 775 return absolute_max_time; 776 t += seconds; 777 } 778 while (y < myy) { 779 if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) { 780 intmax_t diff400 = (myy - y) / 400; 781 if (INTMAX_MAX / SECSPER400YEARS < diff400) 782 return absolute_min_time; 783 seconds = diff400 * SECSPER400YEARS; 784 years = diff400 * 400; 785 } else { 786 seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR; 787 years = 1; 788 } 789 myy -= years; 790 if (t < absolute_min_time + seconds) 791 return absolute_min_time; 792 t -= seconds; 793 } 794 return t; 795 } 796 797 static time_t 798 hunt(timezone_t tz, char *name, time_t lot, time_t hit) 799 { 800 static char * loab; 801 static size_t loabsize; 802 char const * ab; 803 time_t t; 804 struct tm lotm; 805 struct tm * lotmp; 806 struct tm tm; 807 struct tm * tmp; 808 809 lotmp = my_localtime_rz(tz, &lot, &lotm); 810 if (lotmp) 811 ab = saveabbr(&loab, &loabsize, &lotm); 812 else 813 ab = NULL; 814 for ( ; ; ) { 815 time_t diff = hit - lot; 816 if (diff < 2) 817 break; 818 t = lot; 819 t += diff / 2; 820 if (t <= lot) 821 ++t; 822 else if (t >= hit) 823 --t; 824 tmp = my_localtime_rz(tz, &t, &tm); 825 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) : 826 (delta(&tm, &lotm) == (t - lot) && 827 tm.tm_isdst == lotm.tm_isdst && 828 strcmp(abbr(&tm), ab) == 0)) { 829 lot = t; 830 lotm = tm; 831 lotmp = tmp; 832 } else hit = t; 833 } 834 show(tz, name, lot, true); 835 show(tz, name, hit, true); 836 return hit; 837 } 838 839 /* 840 ** Thanks to Paul Eggert for logic used in delta. 841 */ 842 843 static intmax_t 844 delta(struct tm *newp, struct tm *oldp) 845 { 846 intmax_t result; 847 int tmy; 848 849 if (newp->tm_year < oldp->tm_year) 850 return -delta(oldp, newp); 851 result = 0; 852 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy) 853 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE); 854 result += newp->tm_yday - oldp->tm_yday; 855 result *= HOURSPERDAY; 856 result += newp->tm_hour - oldp->tm_hour; 857 result *= MINSPERHOUR; 858 result += newp->tm_min - oldp->tm_min; 859 result *= SECSPERMIN; 860 result += newp->tm_sec - oldp->tm_sec; 861 return result; 862 } 863 864 #ifndef TM_GMTOFF 865 /* Return A->tm_yday, adjusted to compare it fairly to B->tm_yday. 866 Assume A and B differ by at most one year. */ 867 static int 868 adjusted_yday(struct tm const *a, struct tm const *b) 869 { 870 int yday = a->tm_yday; 871 if (b->tm_year < a->tm_year) 872 yday += 365 + isleap_sum(b->tm_year, TM_YEAR_BASE); 873 return yday; 874 } 875 #endif 876 877 /* If A is the broken-down local time and B the broken-down UTC for 878 the same instant, return A's UTC offset in seconds, where positive 879 offsets are east of Greenwich. On failure, return LONG_MIN. */ 880 static long 881 gmtoff(struct tm const *a, struct tm const *b) 882 { 883 #ifdef TM_GMTOFF 884 return a->TM_GMTOFF; 885 #else 886 if (! b) 887 return LONG_MIN; 888 else { 889 int ayday = adjusted_yday(a, b); 890 int byday = adjusted_yday(b, a); 891 int days = ayday - byday; 892 long hours = a->tm_hour - b->tm_hour + 24 * days; 893 long minutes = a->tm_min - b->tm_min + 60 * hours; 894 long seconds = a->tm_sec - b->tm_sec + 60 * minutes; 895 return seconds; 896 } 897 #endif 898 } 899 900 static void 901 show(timezone_t tz, char *zone, time_t t, bool v) 902 { 903 struct tm * tmp; 904 struct tm * gmtmp; 905 struct tm tm, gmtm; 906 907 (void) printf("%-*s ", (int) longest, zone); 908 if (v) { 909 gmtmp = my_gmtime_r(&t, &gmtm); 910 if (gmtmp == NULL) { 911 printf(tformat(), t); 912 } else { 913 dumptime(gmtmp); 914 (void) printf(" UT"); 915 } 916 (void) printf(" = "); 917 } 918 tmp = my_localtime_rz(tz, &t, &tm); 919 dumptime(tmp); 920 if (tmp != NULL) { 921 if (*abbr(tmp) != '\0') 922 (void) printf(" %s", abbr(tmp)); 923 if (v) { 924 long off = gmtoff(tmp, gmtmp); 925 (void) printf(" isdst=%d", tmp->tm_isdst); 926 if (off != LONG_MIN) 927 (void) printf(" gmtoff=%ld", off); 928 } 929 } 930 (void) printf("\n"); 931 if (tmp != NULL && *abbr(tmp) != '\0') 932 abbrok(abbr(tmp), zone); 933 } 934 935 static const char * 936 abbr(struct tm const *tmp) 937 { 938 #ifdef TM_ZONE 939 return tmp->TM_ZONE; 940 #else 941 return (0 <= tmp->tm_isdst && tzname[0 < tmp->tm_isdst] 942 ? tzname[0 < tmp->tm_isdst] 943 : ""); 944 #endif 945 } 946 947 /* 948 ** The code below can fail on certain theoretical systems; 949 ** it works on all known real-world systems as of 2004-12-30. 950 */ 951 952 static const char * 953 tformat(void) 954 { 955 if (0 > (time_t) -1) { /* signed */ 956 if (sizeof (time_t) == sizeof (intmax_t)) 957 return "%"PRIdMAX; 958 if (sizeof (time_t) > sizeof (long)) 959 return "%lld"; 960 if (sizeof (time_t) > sizeof (int)) 961 return "%ld"; 962 return "%d"; 963 } 964 #ifdef PRIuMAX 965 if (sizeof (time_t) == sizeof (uintmax_t)) 966 return "%"PRIuMAX; 967 #endif 968 if (sizeof (time_t) > sizeof (unsigned long)) 969 return "%llu"; 970 if (sizeof (time_t) > sizeof (unsigned int)) 971 return "%lu"; 972 return "%u"; 973 } 974 975 static void 976 dumptime(const struct tm *timeptr) 977 { 978 static const char wday_name[][3] = { 979 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 980 }; 981 static const char mon_name[][3] = { 982 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 983 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 984 }; 985 const char * wn; 986 const char * mn; 987 int lead; 988 int trail; 989 990 if (timeptr == NULL) { 991 printf("NULL"); 992 return; 993 } 994 /* 995 ** The packaged localtime_rz and gmtime_r never put out-of-range 996 ** values in tm_wday or tm_mon, but since this code might be compiled 997 ** with other (perhaps experimental) versions, paranoia is in order. 998 */ 999 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= 1000 (int) (sizeof wday_name / sizeof wday_name[0])) 1001 wn = "???"; 1002 else wn = wday_name[timeptr->tm_wday]; 1003 if (timeptr->tm_mon < 0 || timeptr->tm_mon >= 1004 (int) (sizeof mon_name / sizeof mon_name[0])) 1005 mn = "???"; 1006 else mn = mon_name[timeptr->tm_mon]; 1007 printf("%.3s %.3s%3d %.2d:%.2d:%.2d ", 1008 wn, mn, 1009 timeptr->tm_mday, timeptr->tm_hour, 1010 timeptr->tm_min, timeptr->tm_sec); 1011 #define DIVISOR 10 1012 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR; 1013 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR + 1014 trail / DIVISOR; 1015 trail %= DIVISOR; 1016 if (trail < 0 && lead > 0) { 1017 trail += DIVISOR; 1018 --lead; 1019 } else if (lead < 0 && trail > 0) { 1020 trail -= DIVISOR; 1021 ++lead; 1022 } 1023 if (lead == 0) 1024 printf("%d", trail); 1025 else printf("%d%d", lead, ((trail < 0) ? -trail : trail)); 1026 } 1027