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