1 /* $NetBSD: zdump.c,v 1.41 2015/06/21 16:06:51 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.41 2015/06/21 16:06:51 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 enum { atime_shift = CHAR_BIT * sizeof (time_t) - 2 }; 265 static time_t absolute_min_time = 266 ((time_t) -1 < 0 267 ? (- ((time_t) ~ (time_t) 0 < 0) 268 - (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift))) 269 : 0); 270 static time_t absolute_max_time = 271 ((time_t) -1 < 0 272 ? (((time_t) 1 << atime_shift) - 1 + ((time_t) 1 << atime_shift)) 273 : -1); 274 static size_t longest; 275 static char * progname; 276 static bool warned; 277 static bool errout; 278 279 static char const *abbr(struct tm const *); 280 static intmax_t delta(struct tm *, struct tm *) ATTRIBUTE_PURE; 281 static void dumptime(struct tm const *); 282 static time_t hunt(timezone_t, char *, time_t, time_t); 283 static void show(timezone_t, char *, time_t, bool); 284 static const char *tformat(void); 285 static time_t yeartot(intmax_t) ATTRIBUTE_PURE; 286 287 /* Is A an alphabetic character in the C locale? */ 288 static bool 289 is_alpha(char a) 290 { 291 switch (a) { 292 default: 293 return false; 294 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 295 case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': 296 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': 297 case 'V': case 'W': case 'X': case 'Y': case 'Z': 298 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': 299 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': 300 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': 301 case 'v': case 'w': case 'x': case 'y': case 'z': 302 return true; 303 } 304 } 305 306 /* Return A + B, exiting if the result would overflow. */ 307 static size_t 308 sumsize(size_t a, size_t b) 309 { 310 size_t sum = a + b; 311 if (sum < a) 312 errx(EXIT_FAILURE, "size overflow"); 313 return sum; 314 } 315 316 #if ! HAVE_TZSET 317 # undef tzset 318 # define tzset zdump_tzset 319 static void tzset(void) { } 320 #endif 321 322 /* Assume gmtime_r works if localtime_r does. 323 A replacement localtime_r is defined below if needed. */ 324 #if ! HAVE_LOCALTIME_R 325 326 # undef gmtime_r 327 # define gmtime_r zdump_gmtime_r 328 329 static struct tm * 330 gmtime_r(time_t *tp, struct tm *tmp) 331 { 332 struct tm *r = gmtime(tp); 333 if (r) { 334 *tmp = *r; 335 r = tmp; 336 } 337 return r; 338 } 339 340 #endif 341 342 /* Platforms with TM_ZONE don't need tzname, so they can use the 343 faster localtime_rz or localtime_r if available. */ 344 345 #if defined TM_ZONE && HAVE_LOCALTIME_RZ 346 # define USE_LOCALTIME_RZ true 347 #else 348 # define USE_LOCALTIME_RZ false 349 #endif 350 351 #if ! USE_LOCALTIME_RZ 352 353 # if !defined TM_ZONE || ! HAVE_LOCALTIME_R || ! HAVE_TZSET 354 # undef localtime_r 355 # define localtime_r zdump_localtime_r 356 static struct tm * 357 localtime_r(time_t *tp, struct tm *tmp) 358 { 359 struct tm *r = localtime(tp); 360 if (r) { 361 *tmp = *r; 362 r = tmp; 363 } 364 return r; 365 } 366 # endif 367 368 # undef localtime_rz 369 # define localtime_rz zdump_localtime_rz 370 static struct tm * 371 localtime_rz(timezone_t rz, time_t *tp, struct tm *tmp) 372 { 373 return localtime_r(tp, tmp); 374 } 375 376 # ifdef TYPECHECK 377 # undef mktime_z 378 # define mktime_z zdump_mktime_z 379 static time_t 380 mktime_z(timezone_t tz, struct tm *tmp) 381 { 382 return mktime(tmp); 383 } 384 # endif 385 386 # undef tzalloc 387 # undef tzfree 388 # define tzalloc zdump_tzalloc 389 # define tzfree zdump_tzfree 390 391 static timezone_t 392 tzalloc(char const *val) 393 { 394 static char **fakeenv; 395 char **env = fakeenv; 396 char *env0; 397 if (! env) { 398 char **e = environ; 399 int to; 400 401 while (*e++) 402 continue; 403 env = malloc(sumsize(sizeof *environ, 404 (e - environ) * sizeof *environ)); 405 if (! env) { 406 err(EXIT_FAILURE, "malloc"); 407 } 408 to = 1; 409 for (e = environ; (env[to] = *e); e++) 410 to += strncmp(*e, "TZ=", 3) != 0; 411 } 412 env0 = malloc(sumsize(sizeof "TZ=", strlen(val))); 413 if (! env0) { 414 err(EXIT_FAILURE, "malloc"); 415 } 416 env[0] = strcat(strcpy(env0, "TZ="), val); 417 environ = fakeenv = env; 418 tzset(); 419 return env; 420 } 421 422 static void 423 tzfree(timezone_t env) 424 { 425 environ = env + 1; 426 free(env[0]); 427 } 428 #endif /* ! USE_LOCALTIME_RZ */ 429 430 /* A UTC time zone, and its initializer. */ 431 static timezone_t gmtz; 432 static void 433 gmtzinit(void) 434 { 435 if (USE_LOCALTIME_RZ) { 436 static char const utc[] = "UTC0"; 437 gmtz = tzalloc(utc); 438 if (!gmtz) { 439 err(EXIT_FAILURE, "Cannot create %s", utc); 440 } 441 } 442 } 443 444 /* Convert *TP to UTC, storing the broken-down time into *TMP. 445 Return TMP if successful, NULL otherwise. This is like gmtime_r(TP, TMP), 446 except typically faster if USE_LOCALTIME_RZ. */ 447 static struct tm * 448 my_gmtime_r(time_t *tp, struct tm *tmp) 449 { 450 return USE_LOCALTIME_RZ ? 451 localtime_rz(gmtz, tp, tmp) : gmtime_r(tp, tmp); 452 } 453 454 #ifndef TYPECHECK 455 #define my_localtime_rz localtime_rz 456 #else /* !defined TYPECHECK */ 457 static struct tm * 458 my_localtime_rz(timezone_t tz, const time_t *tp, struct tm *tmp) 459 { 460 tmp = localtime_rz(tz, tp, tmp); 461 if (tmp) { 462 struct tm tm; 463 time_t t; 464 465 tm = *tmp; 466 t = mktime_z(tz, &tm); 467 if (t != *tp) { 468 (void) fflush(stdout); 469 (void) fprintf(stderr, "\n%s: ", progname); 470 (void) fprintf(stderr, tformat(), *tp); 471 (void) fprintf(stderr, " ->"); 472 (void) fprintf(stderr, " year=%d", tmp->tm_year); 473 (void) fprintf(stderr, " mon=%d", tmp->tm_mon); 474 (void) fprintf(stderr, " mday=%d", tmp->tm_mday); 475 (void) fprintf(stderr, " hour=%d", tmp->tm_hour); 476 (void) fprintf(stderr, " min=%d", tmp->tm_min); 477 (void) fprintf(stderr, " sec=%d", tmp->tm_sec); 478 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst); 479 (void) fprintf(stderr, " -> "); 480 (void) fprintf(stderr, tformat(), t); 481 (void) fprintf(stderr, "\n"); 482 errout = true; 483 } 484 } 485 return tmp; 486 } 487 #endif /* !defined TYPECHECK */ 488 489 static void 490 abbrok(const char *const abbrp, const char *const zone) 491 { 492 const char *cp; 493 const char *wp; 494 495 if (warned) 496 return; 497 cp = abbrp; 498 wp = NULL; 499 while (is_alpha(*cp)) 500 ++cp; 501 if (cp - abbrp == 0) 502 wp = _("lacks alphabetic at start"); 503 else if (cp - abbrp < 3) 504 wp = _("has fewer than 3 alphabetics"); 505 else if (cp - abbrp > 6) 506 wp = _("has more than 6 alphabetics"); 507 if (wp == NULL && (*cp == '+' || *cp == '-')) { 508 ++cp; 509 if ('0' <= *cp && *cp <= '9') 510 if (*cp++ == '1' && '0' <= *cp && *cp <= '4') 511 cp++; 512 if (*cp != '\0') 513 wp = _("differs from POSIX standard"); 514 } 515 if (wp == NULL) 516 return; 517 (void) fflush(stdout); 518 (void) fprintf(stderr, 519 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"), 520 progname, zone, abbrp, wp); 521 warned = errout = true; 522 } 523 524 /* Return a time zone abbreviation. If the abbreviation needs to be 525 saved, use *BUF (of size *BUFALLOC) to save it, and return the 526 abbreviation in the possibly-reallocated *BUF. Otherwise, just 527 return the abbreviation. Get the abbreviation from TMP. 528 Exit on memory allocation failure. */ 529 static char const * 530 saveabbr(char **buf, size_t *bufalloc, struct tm const *tmp) 531 { 532 char const *ab = abbr(tmp); 533 if (HAVE_LOCALTIME_RZ) 534 return ab; 535 else { 536 size_t ablen = strlen(ab); 537 if (*bufalloc <= ablen) { 538 free(*buf); 539 540 /* Make the new buffer at least twice as long as the 541 old, to avoid O(N**2) behavior on repeated calls. */ 542 *bufalloc = sumsize(*bufalloc, ablen + 1); 543 *buf = malloc(*bufalloc); 544 if (! *buf) { 545 err(EXIT_FAILURE, "malloc"); 546 } 547 } 548 return strcpy(*buf, ab); 549 } 550 } 551 552 static void 553 close_file(FILE *stream) 554 { 555 char const *e = (ferror(stream) ? _("I/O error") 556 : fclose(stream) != 0 ? strerror(errno) : NULL); 557 if (e) { 558 errx(EXIT_FAILURE, "%s", e); 559 } 560 } 561 562 __dead static void 563 usage(FILE *const stream, const int status) 564 { 565 (void) fprintf(stream, 566 _("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n" 567 "\n" 568 "Report bugs to %s.\n"), 569 progname, progname, REPORT_BUGS_TO); 570 if (status == EXIT_SUCCESS) 571 close_file(stream); 572 exit(status); 573 } 574 575 int 576 main(int argc, char *argv[]) 577 { 578 /* These are static so that they're initially zero. */ 579 static char * abbrev; 580 static size_t abbrevsize; 581 static struct tm newtm; 582 583 int i; 584 bool vflag; 585 bool Vflag; 586 char * cutarg; 587 char * cuttimes; 588 time_t cutlotime; 589 time_t cuthitime; 590 time_t now; 591 time_t t; 592 time_t newt; 593 struct tm tm; 594 struct tm * tmp; 595 struct tm * newtmp; 596 597 cutlotime = absolute_min_time; 598 cuthitime = absolute_max_time; 599 #if HAVE_GETTEXT 600 (void) setlocale(LC_ALL, ""); 601 #ifdef TZ_DOMAINDIR 602 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR); 603 #endif /* defined TEXTDOMAINDIR */ 604 (void) textdomain(TZ_DOMAIN); 605 #endif /* HAVE_GETTEXT */ 606 progname = argv[0]; 607 for (i = 1; i < argc; ++i) 608 if (strcmp(argv[i], "--version") == 0) { 609 (void) printf("zdump %s%s\n", PKGVERSION, TZVERSION); 610 return EXIT_SUCCESS; 611 } else if (strcmp(argv[i], "--help") == 0) { 612 usage(stdout, EXIT_SUCCESS); 613 } 614 vflag = Vflag = false; 615 cutarg = cuttimes = NULL; 616 for (;;) 617 switch (getopt(argc, argv, "c:t:vV")) { 618 case 'c': cutarg = optarg; break; 619 case 't': cuttimes = optarg; break; 620 case 'v': vflag = true; break; 621 case 'V': Vflag = true; break; 622 case -1: 623 if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) 624 goto arg_processing_done; 625 /* Fall through. */ 626 default: 627 usage(stderr, EXIT_FAILURE); 628 } 629 arg_processing_done:; 630 631 if (vflag | Vflag) { 632 intmax_t lo; 633 intmax_t hi; 634 char *loend, *hiend; 635 intmax_t cutloyear = ZDUMP_LO_YEAR; 636 intmax_t cuthiyear = ZDUMP_HI_YEAR; 637 if (cutarg != NULL) { 638 lo = strtoimax(cutarg, &loend, 10); 639 if (cutarg != loend && !*loend) { 640 hi = lo; 641 cuthiyear = hi; 642 } else if (cutarg != loend && *loend == ',' 643 && (hi = strtoimax(loend + 1, &hiend, 10), 644 loend + 1 != hiend && !*hiend)) { 645 cutloyear = lo; 646 cuthiyear = hi; 647 } else { 648 fprintf(stderr, _("%s: wild -c argument %s\n"), 649 progname, cutarg); 650 return EXIT_FAILURE; 651 } 652 } 653 if (cutarg != NULL || cuttimes == NULL) { 654 cutlotime = yeartot(cutloyear); 655 cuthitime = yeartot(cuthiyear); 656 } 657 if (cuttimes != NULL) { 658 lo = strtoimax(cuttimes, &loend, 10); 659 if (cuttimes != loend && !*loend) { 660 hi = lo; 661 if (hi < cuthitime) { 662 if (hi < absolute_min_time) 663 hi = absolute_min_time; 664 cuthitime = hi; 665 } 666 } else if (cuttimes != loend && *loend == ',' 667 && (hi = strtoimax(loend + 1, &hiend, 10), 668 loend + 1 != hiend && !*hiend)) { 669 if (cutlotime < lo) { 670 if (absolute_max_time < lo) 671 lo = absolute_max_time; 672 cutlotime = lo; 673 } 674 if (hi < cuthitime) { 675 if (hi < absolute_min_time) 676 hi = absolute_min_time; 677 cuthitime = hi; 678 } 679 } else { 680 (void) fprintf(stderr, 681 _("%s: wild -t argument %s\n"), 682 progname, cuttimes); 683 return EXIT_FAILURE; 684 } 685 } 686 } 687 gmtzinit(); 688 now = time(NULL); 689 longest = 0; 690 for (i = optind; i < argc; i++) { 691 size_t arglen = strlen(argv[i]); 692 if (longest < arglen) 693 longest = arglen < INT_MAX ? arglen : INT_MAX; 694 } 695 696 for (i = optind; i < argc; ++i) { 697 timezone_t tz = tzalloc(argv[i]); 698 char const *ab; 699 if (!tz) { 700 errx(EXIT_FAILURE, "%s", argv[i]); 701 } 702 if (! (vflag | Vflag)) { 703 show(tz, argv[i], now, false); 704 tzfree(tz); 705 continue; 706 } 707 warned = false; 708 t = absolute_min_time; 709 if (!Vflag) { 710 show(tz, argv[i], t, true); 711 t += SECSPERDAY; 712 show(tz, argv[i], t, true); 713 } 714 if (t < cutlotime) 715 t = cutlotime; 716 tmp = my_localtime_rz(tz, &t, &tm); 717 if (tmp) 718 ab = saveabbr(&abbrev, &abbrevsize, &tm); 719 else 720 ab = NULL; 721 while (t < cuthitime) { 722 newt = ((t < absolute_max_time - SECSPERDAY / 2 723 && t + SECSPERDAY / 2 < cuthitime) 724 ? t + SECSPERDAY / 2 725 : cuthitime); 726 newtmp = localtime_rz(tz, &newt, &newtm); 727 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) : 728 (delta(&newtm, &tm) != (newt - t) || 729 newtm.tm_isdst != tm.tm_isdst || 730 strcmp(abbr(&newtm), ab) != 0)) { 731 newt = hunt(tz, argv[i], t, newt); 732 newtmp = localtime_rz(tz, &newt, &newtm); 733 if (newtmp) 734 ab = saveabbr(&abbrev, &abbrevsize, 735 &newtm); 736 } 737 t = newt; 738 tm = newtm; 739 tmp = newtmp; 740 } 741 if (!Vflag) { 742 t = absolute_max_time; 743 t -= SECSPERDAY; 744 show(tz, argv[i], t, true); 745 t += SECSPERDAY; 746 show(tz, argv[i], t, true); 747 } 748 tzfree(tz); 749 } 750 close_file(stdout); 751 if (errout && (ferror(stderr) || fclose(stderr) != 0)) 752 return EXIT_FAILURE; 753 return EXIT_SUCCESS; 754 } 755 756 static time_t 757 yeartot(const intmax_t y) 758 { 759 intmax_t myy, seconds, years; 760 time_t t; 761 762 myy = EPOCH_YEAR; 763 t = 0; 764 while (myy < y) { 765 if (SECSPER400YEARS_FITS && 400 <= y - myy) { 766 intmax_t diff400 = (y - myy) / 400; 767 if (INTMAX_MAX / SECSPER400YEARS < diff400) 768 return absolute_max_time; 769 seconds = diff400 * SECSPER400YEARS; 770 years = diff400 * 400; 771 } else { 772 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR; 773 years = 1; 774 } 775 myy += years; 776 if (t > absolute_max_time - seconds) 777 return absolute_max_time; 778 t += seconds; 779 } 780 while (y < myy) { 781 if (SECSPER400YEARS_FITS && y + 400 <= myy && myy < 0) { 782 intmax_t diff400 = (myy - y) / 400; 783 if (INTMAX_MAX / SECSPER400YEARS < diff400) 784 return absolute_min_time; 785 seconds = diff400 * SECSPER400YEARS; 786 years = diff400 * 400; 787 } else { 788 seconds = isleap(myy - 1) ? SECSPERLYEAR : SECSPERNYEAR; 789 years = 1; 790 } 791 myy -= years; 792 if (t < absolute_min_time + seconds) 793 return absolute_min_time; 794 t -= seconds; 795 } 796 return t; 797 } 798 799 static time_t 800 hunt(timezone_t tz, char *name, time_t lot, time_t hit) 801 { 802 static char * loab; 803 static size_t loabsize; 804 char const * ab; 805 time_t t; 806 struct tm lotm; 807 struct tm * lotmp; 808 struct tm tm; 809 struct tm * tmp; 810 811 lotmp = my_localtime_rz(tz, &lot, &lotm); 812 if (lotmp) 813 ab = saveabbr(&loab, &loabsize, &lotm); 814 else 815 ab = NULL; 816 for ( ; ; ) { 817 time_t diff = hit - lot; 818 if (diff < 2) 819 break; 820 t = lot; 821 t += diff / 2; 822 if (t <= lot) 823 ++t; 824 else if (t >= hit) 825 --t; 826 tmp = my_localtime_rz(tz, &t, &tm); 827 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) : 828 (delta(&tm, &lotm) == (t - lot) && 829 tm.tm_isdst == lotm.tm_isdst && 830 strcmp(abbr(&tm), ab) == 0)) { 831 lot = t; 832 lotm = tm; 833 lotmp = tmp; 834 } else hit = t; 835 } 836 show(tz, name, lot, true); 837 show(tz, name, hit, true); 838 return hit; 839 } 840 841 /* 842 ** Thanks to Paul Eggert for logic used in delta. 843 */ 844 845 static intmax_t 846 delta(struct tm *newp, struct tm *oldp) 847 { 848 intmax_t result; 849 int tmy; 850 851 if (newp->tm_year < oldp->tm_year) 852 return -delta(oldp, newp); 853 result = 0; 854 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy) 855 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE); 856 result += newp->tm_yday - oldp->tm_yday; 857 result *= HOURSPERDAY; 858 result += newp->tm_hour - oldp->tm_hour; 859 result *= MINSPERHOUR; 860 result += newp->tm_min - oldp->tm_min; 861 result *= SECSPERMIN; 862 result += newp->tm_sec - oldp->tm_sec; 863 return result; 864 } 865 866 #ifndef TM_GMTOFF 867 /* Return A->tm_yday, adjusted to compare it fairly to B->tm_yday. 868 Assume A and B differ by at most one year. */ 869 static int 870 adjusted_yday(struct tm const *a, struct tm const *b) 871 { 872 int yday = a->tm_yday; 873 if (b->tm_year < a->tm_year) 874 yday += 365 + isleap_sum(b->tm_year, TM_YEAR_BASE); 875 return yday; 876 } 877 #endif 878 879 /* If A is the broken-down local time and B the broken-down UTC for 880 the same instant, return A's UTC offset in seconds, where positive 881 offsets are east of Greenwich. On failure, return LONG_MIN. */ 882 static long 883 gmtoff(struct tm const *a, struct tm const *b) 884 { 885 #ifdef TM_GMTOFF 886 return a->TM_GMTOFF; 887 #else 888 if (! b) 889 return LONG_MIN; 890 else { 891 int ayday = adjusted_yday(a, b); 892 int byday = adjusted_yday(b, a); 893 int days = ayday - byday; 894 long hours = a->tm_hour - b->tm_hour + 24 * days; 895 long minutes = a->tm_min - b->tm_min + 60 * hours; 896 long seconds = a->tm_sec - b->tm_sec + 60 * minutes; 897 return seconds; 898 } 899 #endif 900 } 901 902 static void 903 show(timezone_t tz, char *zone, time_t t, bool v) 904 { 905 struct tm * tmp; 906 struct tm * gmtmp; 907 struct tm tm, gmtm; 908 909 (void) printf("%-*s ", (int) longest, zone); 910 if (v) { 911 gmtmp = my_gmtime_r(&t, &gmtm); 912 if (gmtmp == NULL) { 913 printf(tformat(), t); 914 } else { 915 dumptime(gmtmp); 916 (void) printf(" UT"); 917 } 918 (void) printf(" = "); 919 } 920 tmp = my_localtime_rz(tz, &t, &tm); 921 dumptime(tmp); 922 if (tmp != NULL) { 923 if (*abbr(tmp) != '\0') 924 (void) printf(" %s", abbr(tmp)); 925 if (v) { 926 long off = gmtoff(tmp, gmtmp); 927 (void) printf(" isdst=%d", tmp->tm_isdst); 928 if (off != LONG_MIN) 929 (void) printf(" gmtoff=%ld", off); 930 } 931 } 932 (void) printf("\n"); 933 if (tmp != NULL && *abbr(tmp) != '\0') 934 abbrok(abbr(tmp), zone); 935 } 936 937 static const char * 938 abbr(struct tm const *tmp) 939 { 940 #ifdef TM_ZONE 941 return tmp->TM_ZONE; 942 #else 943 return (0 <= tmp->tm_isdst && tzname[0 < tmp->tm_isdst] 944 ? tzname[0 < tmp->tm_isdst] 945 : ""); 946 #endif 947 } 948 949 /* 950 ** The code below can fail on certain theoretical systems; 951 ** it works on all known real-world systems as of 2004-12-30. 952 */ 953 954 static const char * 955 tformat(void) 956 { 957 if (0 > (time_t) -1) { /* signed */ 958 if (sizeof (time_t) == sizeof (intmax_t)) 959 return "%"PRIdMAX; 960 if (sizeof (time_t) > sizeof (long)) 961 return "%lld"; 962 if (sizeof (time_t) > sizeof (int)) 963 return "%ld"; 964 return "%d"; 965 } 966 #ifdef PRIuMAX 967 if (sizeof (time_t) == sizeof (uintmax_t)) 968 return "%"PRIuMAX; 969 #endif 970 if (sizeof (time_t) > sizeof (unsigned long)) 971 return "%llu"; 972 if (sizeof (time_t) > sizeof (unsigned int)) 973 return "%lu"; 974 return "%u"; 975 } 976 977 static void 978 dumptime(const struct tm *timeptr) 979 { 980 static const char wday_name[][3] = { 981 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 982 }; 983 static const char mon_name[][3] = { 984 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 985 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 986 }; 987 const char * wn; 988 const char * mn; 989 int lead; 990 int trail; 991 992 if (timeptr == NULL) { 993 printf("NULL"); 994 return; 995 } 996 /* 997 ** The packaged localtime_rz and gmtime_r never put out-of-range 998 ** values in tm_wday or tm_mon, but since this code might be compiled 999 ** with other (perhaps experimental) versions, paranoia is in order. 1000 */ 1001 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= 1002 (int) (sizeof wday_name / sizeof wday_name[0])) 1003 wn = "???"; 1004 else wn = wday_name[timeptr->tm_wday]; 1005 if (timeptr->tm_mon < 0 || timeptr->tm_mon >= 1006 (int) (sizeof mon_name / sizeof mon_name[0])) 1007 mn = "???"; 1008 else mn = mon_name[timeptr->tm_mon]; 1009 printf("%.3s %.3s%3d %.2d:%.2d:%.2d ", 1010 wn, mn, 1011 timeptr->tm_mday, timeptr->tm_hour, 1012 timeptr->tm_min, timeptr->tm_sec); 1013 #define DIVISOR 10 1014 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR; 1015 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR + 1016 trail / DIVISOR; 1017 trail %= DIVISOR; 1018 if (trail < 0 && lead > 0) { 1019 trail += DIVISOR; 1020 --lead; 1021 } else if (lead < 0 && trail > 0) { 1022 trail -= DIVISOR; 1023 ++lead; 1024 } 1025 if (lead == 0) 1026 printf("%d", trail); 1027 else printf("%d%d", lead, ((trail < 0) ? -trail : trail)); 1028 } 1029