1 /* $NetBSD: zdump.c,v 1.30 2013/07/17 23:09:26 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.30 2013/07/17 23:09:26 christos Exp $"); 10 #endif /* !defined lint */ 11 12 #include "version.h" 13 /* 14 ** This code has been made independent of the rest of the time 15 ** conversion package to increase confidence in the verification it provides. 16 ** You can use this code to help in verifying other implementations. 17 ** 18 ** However, include private.h when debugging, so that it overrides 19 ** time_t consistently with the rest of the package. 20 */ 21 22 #include "private.h" 23 24 #include "stdio.h" /* for stdout, stderr */ 25 #include "string.h" /* for strcpy */ 26 #include "sys/types.h" /* for time_t */ 27 #include "time.h" /* for struct tm */ 28 #include "stdlib.h" /* for exit, malloc, atoi */ 29 #include <err.h> 30 #include "float.h" /* for FLT_MAX and DBL_MAX */ 31 #include "ctype.h" /* for isalpha et al. */ 32 #ifndef isascii 33 #define isascii(x) 1 34 #endif /* !defined isascii */ 35 36 /* 37 ** Substitutes for pre-C99 compilers. 38 ** Much of this section of code is stolen from private.h. 39 */ 40 41 #ifndef HAVE_STDINT_H 42 # define HAVE_STDINT_H \ 43 (199901 <= __STDC_VERSION__ || 2 < (__GLIBC__ + (0 < __GLIBC_MINOR__))) 44 #endif 45 #if HAVE_STDINT_H 46 # include "stdint.h" 47 #endif 48 #ifndef HAVE_INTTYPES_H 49 # define HAVE_INTTYPES_H HAVE_STDINT_H 50 #endif 51 #if HAVE_INTTYPES_H 52 # include <inttypes.h> 53 #endif 54 55 #ifndef INT_FAST32_MAX 56 # if INT_MAX >> 31 == 0 57 typedef long int_fast32_t; 58 # else 59 typedef int int_fast32_t; 60 # endif 61 #endif 62 63 #ifndef INTMAX_MAX 64 # if defined LLONG_MAX || defined __LONG_LONG_MAX__ 65 typedef long long intmax_t; 66 # define PRIdMAX "lld" 67 # else 68 typedef long intmax_t; 69 # define PRIdMAX "ld" 70 # endif 71 #endif 72 #ifndef SCNdMAX 73 # define SCNdMAX PRIdMAX 74 #endif 75 76 77 #ifndef ZDUMP_LO_YEAR 78 #define ZDUMP_LO_YEAR (-500) 79 #endif /* !defined ZDUMP_LO_YEAR */ 80 81 #ifndef ZDUMP_HI_YEAR 82 #define ZDUMP_HI_YEAR 2500 83 #endif /* !defined ZDUMP_HI_YEAR */ 84 85 #ifndef MAX_STRING_LENGTH 86 #define MAX_STRING_LENGTH 1024 87 #endif /* !defined MAX_STRING_LENGTH */ 88 89 #ifndef TRUE 90 #define TRUE 1 91 #endif /* !defined TRUE */ 92 93 #ifndef FALSE 94 #define FALSE 0 95 #endif /* !defined FALSE */ 96 97 #ifndef EXIT_SUCCESS 98 #define EXIT_SUCCESS 0 99 #endif /* !defined EXIT_SUCCESS */ 100 101 #ifndef EXIT_FAILURE 102 #define EXIT_FAILURE 1 103 #endif /* !defined EXIT_FAILURE */ 104 105 #ifndef SECSPERMIN 106 #define SECSPERMIN 60 107 #endif /* !defined SECSPERMIN */ 108 109 #ifndef MINSPERHOUR 110 #define MINSPERHOUR 60 111 #endif /* !defined MINSPERHOUR */ 112 113 #ifndef SECSPERHOUR 114 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR) 115 #endif /* !defined SECSPERHOUR */ 116 117 #ifndef HOURSPERDAY 118 #define HOURSPERDAY 24 119 #endif /* !defined HOURSPERDAY */ 120 121 #ifndef EPOCH_YEAR 122 #define EPOCH_YEAR 1970 123 #endif /* !defined EPOCH_YEAR */ 124 125 #ifndef TM_YEAR_BASE 126 #define TM_YEAR_BASE 1900 127 #endif /* !defined TM_YEAR_BASE */ 128 129 #ifndef DAYSPERNYEAR 130 #define DAYSPERNYEAR 365 131 #endif /* !defined DAYSPERNYEAR */ 132 133 #ifndef isleap 134 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0)) 135 #endif /* !defined isleap */ 136 137 #ifndef isleap_sum 138 /* 139 ** See tzfile.h for details on isleap_sum. 140 */ 141 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400) 142 #endif /* !defined isleap_sum */ 143 144 #define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY) 145 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR) 146 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY) 147 148 #ifndef HAVE_GETTEXT 149 #define HAVE_GETTEXT 0 150 #endif 151 #if HAVE_GETTEXT 152 #include "locale.h" /* for setlocale */ 153 #include "libintl.h" 154 #endif /* HAVE_GETTEXT */ 155 156 #ifndef GNUC_or_lint 157 #ifdef lint 158 #define GNUC_or_lint 159 #else /* !defined lint */ 160 #ifdef __GNUC__ 161 #define GNUC_or_lint 162 #endif /* defined __GNUC__ */ 163 #endif /* !defined lint */ 164 #endif /* !defined GNUC_or_lint */ 165 166 #ifndef ATTRIBUTE_PURE 167 #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__) 168 # define ATTRIBUTE_PURE __attribute__ ((ATTRIBUTE_PURE__)) 169 #else 170 # define ATTRIBUTE_PURE /* empty */ 171 #endif 172 #endif 173 174 #ifndef INITIALIZE 175 #ifdef GNUC_or_lint 176 #define INITIALIZE(x) ((x) = 0) 177 #else /* !defined GNUC_or_lint */ 178 #define INITIALIZE(x) 179 #endif /* !defined GNUC_or_lint */ 180 #endif /* !defined INITIALIZE */ 181 182 /* 183 ** For the benefit of GNU folk... 184 ** `_(MSGID)' uses the current locale's message library string for MSGID. 185 ** The default is to use gettext if available, and use MSGID otherwise. 186 */ 187 188 #ifndef _ 189 #if HAVE_GETTEXT 190 #define _(msgid) gettext(msgid) 191 #else /* !HAVE_GETTEXT */ 192 #define _(msgid) msgid 193 #endif /* !HAVE_GETTEXT */ 194 #endif /* !defined _ */ 195 196 #ifndef TZ_DOMAIN 197 #define TZ_DOMAIN "tz" 198 #endif /* !defined TZ_DOMAIN */ 199 200 extern char ** environ; 201 extern int getopt(int argc, char * const argv[], 202 const char * options); 203 extern char * optarg; 204 extern int optind; 205 206 /* The minimum and maximum finite time values. */ 207 static time_t absolute_min_time = 208 ((time_t) 0.5 == 0.5 209 ? (sizeof (time_t) == sizeof (float) ? (time_t) -FLT_MAX 210 : sizeof (time_t) == sizeof (double) ? (time_t) -DBL_MAX 211 : sizeof (time_t) == sizeof (long double) ? (time_t) -LDBL_MAX 212 : 0) 213 #ifndef TIME_T_FLOATING 214 : (time_t) -1 < 0 215 ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1) 216 #endif 217 : 0); 218 static time_t absolute_max_time = 219 ((time_t) 0.5 == 0.5 220 ? (sizeof (time_t) == sizeof (float) ? (time_t) FLT_MAX 221 : sizeof (time_t) == sizeof (double) ? (time_t) DBL_MAX 222 : sizeof (time_t) == sizeof (long double) ? (time_t) LDBL_MAX 223 : -1) 224 #ifndef TIME_T_FLOATING 225 : (time_t) -1 < 0 226 ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)) 227 #endif 228 : -1); 229 static size_t longest; 230 static char * progname; 231 static int warned; 232 233 static const char * abbr(struct tm * tmp); 234 static void abbrok(const char * abbrp, const char * zone); 235 static intmax_t delta(struct tm * newp, struct tm * oldp) ATTRIBUTE_PURE; 236 static void dumptime(const struct tm * tmp); 237 static time_t hunt(char * name, time_t lot, time_t hit); 238 static void checkabsolutes(void); 239 static void show(char * zone, time_t t, int v); 240 static const char * tformat(void); 241 static time_t yeartot(long y) ATTRIBUTE_PURE; 242 243 #ifndef TYPECHECK 244 #define my_localtime localtime 245 #else /* !defined TYPECHECK */ 246 static struct tm * 247 my_localtime(time_t *tp) 248 { 249 struct tm *tmp; 250 251 tmp = localtime(tp); 252 if (tp != NULL && tmp != NULL) { 253 struct tm tm; 254 time_t t; 255 256 tm = *tmp; 257 t = mktime(&tm); 258 if (t - *tp >= 1 || *tp - t >= 1) { 259 (void) fflush(stdout); 260 (void) fprintf(stderr, "\n%s: ", progname); 261 (void) fprintf(stderr, tformat(), *tp); 262 (void) fprintf(stderr, " ->"); 263 (void) fprintf(stderr, " year=%d", tmp->tm_year); 264 (void) fprintf(stderr, " mon=%d", tmp->tm_mon); 265 (void) fprintf(stderr, " mday=%d", tmp->tm_mday); 266 (void) fprintf(stderr, " hour=%d", tmp->tm_hour); 267 (void) fprintf(stderr, " min=%d", tmp->tm_min); 268 (void) fprintf(stderr, " sec=%d", tmp->tm_sec); 269 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst); 270 (void) fprintf(stderr, " -> "); 271 (void) fprintf(stderr, tformat(), t); 272 (void) fprintf(stderr, "\n"); 273 } 274 } 275 return tmp; 276 } 277 #endif /* !defined TYPECHECK */ 278 279 static void 280 abbrok(const char *const abbrp, const char *const zone) 281 { 282 const char *cp; 283 const char *wp; 284 285 if (warned) 286 return; 287 cp = abbrp; 288 wp = NULL; 289 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp)) 290 ++cp; 291 if (cp - abbrp == 0) 292 wp = _("lacks alphabetic at start"); 293 else if (cp - abbrp < 3) 294 wp = _("has fewer than 3 alphabetics"); 295 else if (cp - abbrp > 6) 296 wp = _("has more than 6 alphabetics"); 297 if (wp == NULL && (*cp == '+' || *cp == '-')) { 298 ++cp; 299 if (isascii((unsigned char) *cp) && 300 isdigit((unsigned char) *cp)) 301 if (*cp++ == '1' && *cp >= '0' && *cp <= '4') 302 ++cp; 303 if (*cp != '\0') 304 wp = _("differs from POSIX standard"); 305 } 306 if (wp == NULL) 307 return; 308 (void) fflush(stdout); 309 (void) fprintf(stderr, 310 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"), 311 progname, zone, abbrp, wp); 312 warned = TRUE; 313 } 314 315 __dead static void 316 usage(FILE *const stream, const int status) 317 { 318 (void) fprintf(stream, 319 _("%s: usage: %s [--version] [--help] [-{vV}] [-{ct} [lo,]hi] zonename ...\n" 320 "\n" 321 "Report bugs to %s.\n"), 322 progname, progname, REPORT_BUGS_TO); 323 exit(status); 324 } 325 326 int 327 main(int argc, char *argv[]) 328 { 329 int i; 330 int vflag; 331 int Vflag; 332 char * cutarg; 333 char * cuttimes; 334 time_t cutlotime; 335 time_t cuthitime; 336 char ** fakeenv; 337 time_t now; 338 time_t t; 339 time_t newt; 340 struct tm tm; 341 struct tm newtm; 342 struct tm * tmp; 343 struct tm * newtmp; 344 345 cutlotime = absolute_min_time; 346 cuthitime = absolute_max_time; 347 #if HAVE_GETTEXT 348 (void) setlocale(LC_ALL, ""); 349 #ifdef TZ_DOMAINDIR 350 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR); 351 #endif /* defined TEXTDOMAINDIR */ 352 (void) textdomain(TZ_DOMAIN); 353 #endif /* HAVE_GETTEXT */ 354 progname = argv[0]; 355 for (i = 1; i < argc; ++i) 356 if (strcmp(argv[i], "--version") == 0) { 357 (void) printf("zdump %s%s\n", PKGVERSION, TZVERSION); 358 exit(EXIT_SUCCESS); 359 } else if (strcmp(argv[i], "--help") == 0) { 360 usage(stdout, EXIT_SUCCESS); 361 } 362 vflag = Vflag = 0; 363 cutarg = cuttimes = NULL; 364 for (;;) 365 switch (getopt(argc, argv, "c:t:vV")) { 366 case 'c': cutarg = optarg; break; 367 case 't': cuttimes = optarg; break; 368 case 'v': vflag = 1; break; 369 case 'V': Vflag = 1; break; 370 case -1: 371 if (! (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) 372 goto arg_processing_done; 373 /* Fall through. */ 374 default: 375 usage(stderr, EXIT_FAILURE); 376 } 377 arg_processing_done:; 378 379 if (vflag | Vflag) { 380 intmax_t lo; 381 intmax_t hi; 382 char dummy; 383 intmax_t cutloyear = ZDUMP_LO_YEAR; 384 intmax_t cuthiyear = ZDUMP_HI_YEAR; 385 if (cutarg != NULL) { 386 if (sscanf(cutarg, "%"SCNdMAX"%c", &hi, &dummy) == 1) { 387 cuthiyear = hi; 388 } else if (sscanf(cutarg, "%"SCNdMAX",%"SCNdMAX"%c", 389 &lo, &hi, &dummy) == 2) { 390 cutloyear = lo; 391 cuthiyear = hi; 392 } else { 393 (void) fprintf(stderr, _("%s: wild -c argument %s\n"), 394 progname, cutarg); 395 exit(EXIT_FAILURE); 396 } 397 } 398 checkabsolutes(); 399 if (cutarg != NULL || cuttimes == NULL) { 400 cutlotime = yeartot(cutloyear); 401 cuthitime = yeartot(cuthiyear); 402 } 403 if (cuttimes != NULL) { 404 if (sscanf(cuttimes, "%"SCNdMAX"%c", &hi, &dummy) == 1) { 405 if (hi < cuthitime) { 406 if (hi < absolute_min_time) 407 hi = absolute_min_time; 408 cuthitime = hi; 409 } 410 } else if (sscanf(cuttimes, "%"SCNdMAX",%"SCNdMAX"%c", 411 &lo, &hi, &dummy) == 2) { 412 if (cutlotime < lo) { 413 if (absolute_max_time < lo) 414 lo = absolute_max_time; 415 cutlotime = lo; 416 } 417 if (hi < cuthitime) { 418 if (hi < absolute_min_time) 419 hi = absolute_min_time; 420 cuthitime = hi; 421 } 422 } else { 423 (void) fprintf(stderr, 424 _("%s: wild -t argument %s\n"), 425 progname, cuttimes); 426 exit(EXIT_FAILURE); 427 } 428 } 429 } 430 (void) time(&now); 431 longest = 0; 432 for (i = optind; i < argc; ++i) 433 if (strlen(argv[i]) > longest) 434 longest = strlen(argv[i]); 435 { 436 int from; 437 int to; 438 439 for (i = 0; environ[i] != NULL; ++i) 440 continue; 441 fakeenv = malloc((i + 2) * sizeof *fakeenv); 442 if (fakeenv == NULL || 443 (fakeenv[0] = malloc(longest + 4)) == NULL) { 444 err(EXIT_FAILURE, "Can't allocated %zu bytes", 445 longest + 4); 446 } 447 to = 0; 448 (void)strcpy(fakeenv[to++], "TZ="); /* XXX strcpy is safe */ 449 for (from = 0; environ[from] != NULL; ++from) 450 if (strncmp(environ[from], "TZ=", 3) != 0) 451 fakeenv[to++] = environ[from]; 452 fakeenv[to] = NULL; 453 environ = fakeenv; 454 } 455 for (i = optind; i < argc; ++i) { 456 static char buf[MAX_STRING_LENGTH]; 457 458 (void) strcpy(&fakeenv[0][3], argv[i]); /* XXX strcpy is safe */ 459 if (! (vflag | Vflag)) { 460 show(argv[i], now, FALSE); 461 continue; 462 } 463 warned = FALSE; 464 t = absolute_min_time; 465 if (!Vflag) { 466 show(argv[i], t, TRUE); 467 t += SECSPERHOUR * HOURSPERDAY; 468 show(argv[i], t, TRUE); 469 } 470 if (t < cutlotime) 471 t = cutlotime; 472 tmp = my_localtime(&t); 473 if (tmp != NULL) { 474 tm = *tmp; 475 (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1); 476 } 477 for ( ; ; ) { 478 if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12) 479 break; 480 newt = t + SECSPERHOUR * 12; 481 newtmp = localtime(&newt); 482 if (newtmp != NULL) 483 newtm = *newtmp; 484 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) : 485 (delta(&newtm, &tm) != (newt - t) || 486 newtm.tm_isdst != tm.tm_isdst || 487 strcmp(abbr(&newtm), buf) != 0)) { 488 newt = hunt(argv[i], t, newt); 489 newtmp = localtime(&newt); 490 if (newtmp != NULL) { 491 newtm = *newtmp; 492 (void) strncpy(buf, 493 abbr(&newtm), 494 (sizeof buf) - 1); 495 } 496 } 497 t = newt; 498 tm = newtm; 499 tmp = newtmp; 500 } 501 if (!Vflag) { 502 t = absolute_max_time; 503 t -= SECSPERHOUR * HOURSPERDAY; 504 show(argv[i], t, TRUE); 505 t += SECSPERHOUR * HOURSPERDAY; 506 show(argv[i], t, TRUE); 507 } 508 } 509 if (fflush(stdout) || ferror(stdout)) { 510 err(EXIT_FAILURE, _("Error writing standard output")); 511 } 512 exit(EXIT_SUCCESS); 513 /* If exit fails to exit... */ 514 return EXIT_FAILURE; 515 } 516 517 static void 518 checkabsolutes(void) 519 { 520 if (absolute_max_time < absolute_min_time) { 521 (void) fprintf(stderr, 522 _("%s: use of -v on system with floating time_t other than float or double\n"), 523 progname); 524 exit(EXIT_FAILURE); 525 } 526 } 527 528 static time_t 529 yeartot(const long y) 530 { 531 intmax_t myy; 532 int_fast32_t seconds; 533 time_t t; 534 535 myy = EPOCH_YEAR; 536 t = 0; 537 while (myy != y) { 538 if (myy < y) { 539 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR; 540 ++myy; 541 if (t > absolute_max_time - seconds) { 542 t = absolute_max_time; 543 break; 544 } 545 t += seconds; 546 } else { 547 --myy; 548 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR; 549 if (t < absolute_min_time + seconds) { 550 t = absolute_min_time; 551 break; 552 } 553 t -= seconds; 554 } 555 } 556 return t; 557 } 558 559 static time_t 560 hunt(char *name, time_t lot, time_t hit) 561 { 562 time_t t; 563 struct tm lotm; 564 struct tm * lotmp; 565 struct tm tm; 566 struct tm * tmp; 567 char loab[MAX_STRING_LENGTH]; 568 569 lotmp = my_localtime(&lot); 570 if (lotmp != NULL) { 571 lotm = *lotmp; 572 (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1); 573 } 574 for ( ; ; ) { 575 time_t diff = hit - lot; 576 if (diff < 2) 577 break; 578 t = lot; 579 t += diff / 2; 580 if (t <= lot) 581 ++t; 582 else if (t >= hit) 583 --t; 584 tmp = my_localtime(&t); 585 if (tmp != NULL) 586 tm = *tmp; 587 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) : 588 (delta(&tm, &lotm) == (t - lot) && 589 tm.tm_isdst == lotm.tm_isdst && 590 strcmp(abbr(&tm), loab) == 0)) { 591 lot = t; 592 lotm = tm; 593 lotmp = tmp; 594 } else hit = t; 595 } 596 show(name, lot, TRUE); 597 show(name, hit, TRUE); 598 return hit; 599 } 600 601 /* 602 ** Thanks to Paul Eggert for logic used in delta. 603 */ 604 605 static intmax_t 606 delta(struct tm *newp, struct tm *oldp) 607 { 608 intmax_t result; 609 int tmy; 610 611 if (newp->tm_year < oldp->tm_year) 612 return -delta(oldp, newp); 613 result = 0; 614 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy) 615 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE); 616 result += newp->tm_yday - oldp->tm_yday; 617 result *= HOURSPERDAY; 618 result += newp->tm_hour - oldp->tm_hour; 619 result *= MINSPERHOUR; 620 result += newp->tm_min - oldp->tm_min; 621 result *= SECSPERMIN; 622 result += newp->tm_sec - oldp->tm_sec; 623 return result; 624 } 625 626 static void 627 show(char *zone, time_t t, int v) 628 { 629 struct tm * tmp; 630 631 (void) printf("%-*s ", (int) longest, zone); 632 if (v) { 633 tmp = gmtime(&t); 634 if (tmp == NULL) { 635 (void) printf(tformat(), t); 636 } else { 637 dumptime(tmp); 638 (void) printf(" UTC"); 639 } 640 (void) printf(" = "); 641 } 642 tmp = my_localtime(&t); 643 dumptime(tmp); 644 if (tmp != NULL) { 645 if (*abbr(tmp) != '\0') 646 (void) printf(" %s", abbr(tmp)); 647 if (v) { 648 (void) printf(" isdst=%d", tmp->tm_isdst); 649 #ifdef TM_GMTOFF 650 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF); 651 #endif /* defined TM_GMTOFF */ 652 } 653 } 654 (void) printf("\n"); 655 if (tmp != NULL && *abbr(tmp) != '\0') 656 abbrok(abbr(tmp), zone); 657 } 658 659 static const char * 660 abbr(struct tm *tmp) 661 { 662 const char * result; 663 static const char nada; 664 665 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1) 666 return &nada; 667 result = tzname[tmp->tm_isdst]; 668 return (result == NULL) ? &nada : result; 669 } 670 671 /* 672 ** The code below can fail on certain theoretical systems; 673 ** it works on all known real-world systems as of 2004-12-30. 674 */ 675 676 static const char * 677 tformat(void) 678 { 679 if (0.5 == (time_t) 0.5) { /* floating */ 680 if (sizeof (time_t) > sizeof (double)) 681 return "%Lg"; 682 return "%g"; 683 } 684 if (0 > (time_t) -1) { /* signed */ 685 if (sizeof (time_t) == sizeof (intmax_t)) 686 return "%"PRIdMAX; 687 if (sizeof (time_t) > sizeof (long)) 688 return "%lld"; 689 if (sizeof (time_t) > sizeof (int)) 690 return "%ld"; 691 return "%d"; 692 } 693 #ifdef PRIuMAX 694 if (sizeof (time_t) == sizeof (uintmax_t)) 695 return "%"PRIuMAX; 696 #endif 697 if (sizeof (time_t) > sizeof (unsigned long)) 698 return "%llu"; 699 if (sizeof (time_t) > sizeof (unsigned int)) 700 return "%lu"; 701 return "%u"; 702 } 703 704 static void 705 dumptime(const struct tm *timeptr) 706 { 707 static const char wday_name[][3] = { 708 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 709 }; 710 static const char mon_name[][3] = { 711 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 712 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 713 }; 714 const char * wn; 715 const char * mn; 716 int lead; 717 int trail; 718 719 if (timeptr == NULL) { 720 (void) printf("NULL"); 721 return; 722 } 723 /* 724 ** The packaged versions of localtime and gmtime never put out-of-range 725 ** values in tm_wday or tm_mon, but since this code might be compiled 726 ** with other (perhaps experimental) versions, paranoia is in order. 727 */ 728 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= 729 (int) (sizeof wday_name / sizeof wday_name[0])) 730 wn = "???"; 731 else wn = wday_name[timeptr->tm_wday]; 732 if (timeptr->tm_mon < 0 || timeptr->tm_mon >= 733 (int) (sizeof mon_name / sizeof mon_name[0])) 734 mn = "???"; 735 else mn = mon_name[timeptr->tm_mon]; 736 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ", 737 wn, mn, 738 timeptr->tm_mday, timeptr->tm_hour, 739 timeptr->tm_min, timeptr->tm_sec); 740 #define DIVISOR 10 741 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR; 742 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR + 743 trail / DIVISOR; 744 trail %= DIVISOR; 745 if (trail < 0 && lead > 0) { 746 trail += DIVISOR; 747 --lead; 748 } else if (lead < 0 && trail > 0) { 749 trail -= DIVISOR; 750 ++lead; 751 } 752 if (lead == 0) 753 (void) printf("%d", trail); 754 else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail)); 755 } 756