1 /* 2 * @(#)zdump.c 8.8 3 * $FreeBSD: src/usr.sbin/zic/zdump.c,v 1.7 1999/08/28 01:21:19 peter Exp $ 4 * $DragonFly: src/usr.sbin/zic/zdump.c,v 1.5 2008/10/19 20:15:58 swildner Exp $ 5 */ 6 /* 7 ** This code has been made independent of the rest of the time 8 ** conversion package to increase confidence in the verification it provides. 9 ** You can use this code to help in verifying other implementations. 10 */ 11 12 #include <ctype.h> /* for isalpha et al. */ 13 #include <err.h> 14 #include <float.h> /* for FLT_MAX and DBL_MAX */ 15 #include <stdio.h> /* for stdout, stderr */ 16 #include <stdlib.h> /* for exit, malloc, atoi */ 17 #include <string.h> /* for strcpy */ 18 #include <sys/types.h> /* for time_t */ 19 #include <time.h> /* for struct tm */ 20 #include <unistd.h> 21 22 #ifndef ZDUMP_LO_YEAR 23 #define ZDUMP_LO_YEAR (-500) 24 #endif /* !defined ZDUMP_LO_YEAR */ 25 26 #ifndef ZDUMP_HI_YEAR 27 #define ZDUMP_HI_YEAR 2500 28 #endif /* !defined ZDUMP_HI_YEAR */ 29 30 #ifndef MAX_STRING_LENGTH 31 #define MAX_STRING_LENGTH 1024 32 #endif /* !defined MAX_STRING_LENGTH */ 33 34 #ifndef TRUE 35 #define TRUE 1 36 #endif /* !defined TRUE */ 37 38 #ifndef FALSE 39 #define FALSE 0 40 #endif /* !defined FALSE */ 41 42 #ifndef EXIT_SUCCESS 43 #define EXIT_SUCCESS 0 44 #endif /* !defined EXIT_SUCCESS */ 45 46 #ifndef EXIT_FAILURE 47 #define EXIT_FAILURE 1 48 #endif /* !defined EXIT_FAILURE */ 49 50 #ifndef SECSPERMIN 51 #define SECSPERMIN 60 52 #endif /* !defined SECSPERMIN */ 53 54 #ifndef MINSPERHOUR 55 #define MINSPERHOUR 60 56 #endif /* !defined MINSPERHOUR */ 57 58 #ifndef SECSPERHOUR 59 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR) 60 #endif /* !defined SECSPERHOUR */ 61 62 #ifndef HOURSPERDAY 63 #define HOURSPERDAY 24 64 #endif /* !defined HOURSPERDAY */ 65 66 #ifndef EPOCH_YEAR 67 #define EPOCH_YEAR 1970 68 #endif /* !defined EPOCH_YEAR */ 69 70 #ifndef TM_YEAR_BASE 71 #define TM_YEAR_BASE 1900 72 #endif /* !defined TM_YEAR_BASE */ 73 74 #ifndef DAYSPERNYEAR 75 #define DAYSPERNYEAR 365 76 #endif /* !defined DAYSPERNYEAR */ 77 78 #ifndef isleap 79 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0)) 80 #endif /* !defined isleap */ 81 82 #ifndef isleap_sum 83 /* 84 ** See tzfile.h for details on isleap_sum. 85 */ 86 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400) 87 #endif /* !defined isleap_sum */ 88 89 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY) 90 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR) 91 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY) 92 93 #ifndef GNUC_or_lint 94 #ifdef lint 95 #define GNUC_or_lint 96 #else /* !defined lint */ 97 #ifdef __GNUC__ 98 #define GNUC_or_lint 99 #endif /* defined __GNUC__ */ 100 #endif /* !defined lint */ 101 #endif /* !defined GNUC_or_lint */ 102 103 #ifndef INITIALIZE 104 #ifdef GNUC_or_lint 105 #define INITIALIZE(x) ((x) = 0) 106 #else /* !defined GNUC_or_lint */ 107 #define INITIALIZE(x) 108 #endif /* !defined GNUC_or_lint */ 109 #endif /* !defined INITIALIZE */ 110 111 /* 112 ** For the benefit of GNU folk... 113 ** `_(MSGID)' uses the current locale's message library string for MSGID. 114 ** The default is to use gettext if available, and use MSGID otherwise. 115 */ 116 117 #ifndef _ 118 #define _(msgid) msgid 119 #endif /* !defined _ */ 120 121 #ifndef TZ_DOMAIN 122 #define TZ_DOMAIN "tz" 123 #endif /* !defined TZ_DOMAIN */ 124 125 extern char ** environ; 126 extern char * tzname[2]; 127 128 static time_t absolute_min_time; 129 static time_t absolute_max_time; 130 static size_t longest; 131 static int warned; 132 133 static char *abbr(struct tm *tmp); 134 static void abbrok(const char *abbrp, const char *zone); 135 static long delta(struct tm *newp, struct tm *oldp); 136 static void dumptime(const struct tm *tmp); 137 static time_t hunt(char *name, time_t lot, time_t hit); 138 static void setabsolutes(void); 139 static void show(char *zone, time_t t, int v); 140 static const char *tformat(void); 141 static time_t yeartot(long y); 142 static void usage(void); 143 144 #ifndef TYPECHECK 145 #define my_localtime localtime 146 #else /* !defined TYPECHECK */ 147 static struct tm * 148 my_localtime(time_t *tp) 149 { 150 struct tm * tmp; 151 152 tmp = localtime(tp); 153 if (tp != NULL && tmp != NULL) { 154 struct tm tm; 155 time_t t; 156 157 tm = *tmp; 158 t = mktime(&tm); 159 if (t - *tp >= 1 || *tp - t >= 1) { 160 fflush(stdout); 161 fprintf(stderr, "\n%s: ", progname); 162 fprintf(stderr, tformat(), *tp); 163 fprintf(stderr, " ->"); 164 fprintf(stderr, " year=%d", tmp->tm_year); 165 fprintf(stderr, " mon=%d", tmp->tm_mon); 166 fprintf(stderr, " mday=%d", tmp->tm_mday); 167 fprintf(stderr, " hour=%d", tmp->tm_hour); 168 fprintf(stderr, " min=%d", tmp->tm_min); 169 fprintf(stderr, " sec=%d", tmp->tm_sec); 170 fprintf(stderr, " isdst=%d", tmp->tm_isdst); 171 fprintf(stderr, " -> "); 172 fprintf(stderr, tformat(), t); 173 fprintf(stderr, "\n"); 174 } 175 } 176 return tmp; 177 } 178 #endif /* !defined TYPECHECK */ 179 180 static void 181 abbrok(const char * const abbrp, const char * const zone) 182 { 183 const char *cp; 184 char *wp; 185 186 if (warned) 187 return; 188 cp = abbrp; 189 wp = NULL; 190 while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp)) 191 ++cp; 192 if (cp - abbrp == 0) 193 wp = _("lacks alphabetic at start"); 194 else if (cp - abbrp < 3) 195 wp = _("has fewer than 3 alphabetics"); 196 else if (cp - abbrp > 6) 197 wp = _("has more than 6 alphabetics"); 198 if (wp == NULL && (*cp == '+' || *cp == '-')) { 199 ++cp; 200 if (isascii((unsigned char) *cp) && 201 isdigit((unsigned char) *cp)) 202 if (*cp++ == '1' && *cp >= '0' && *cp <= '4') 203 ++cp; 204 if (*cp != '\0') 205 wp = _("differs from POSIX standard"); 206 } 207 if (wp == NULL) 208 return; 209 fflush(stdout); 210 warnx(_("warning: zone \"%s\" abbreviation \"%s\" %s\n"), 211 zone, abbrp, wp); 212 warned = TRUE; 213 } 214 215 int 216 main(int argc, char *argv[]) 217 { 218 int i; 219 int c; 220 int vflag; 221 char *cutarg; 222 long cutloyear = ZDUMP_LO_YEAR; 223 long cuthiyear = ZDUMP_HI_YEAR; 224 time_t cutlotime; 225 time_t cuthitime; 226 char **fakeenv; 227 time_t now; 228 time_t t; 229 time_t newt; 230 struct tm tm; 231 struct tm newtm; 232 struct tm *tmp; 233 struct tm *newtmp; 234 235 INITIALIZE(cutlotime); 236 INITIALIZE(cuthitime); 237 vflag = 0; 238 cutarg = NULL; 239 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v') 240 if (c == 'v') 241 vflag = 1; 242 else cutarg = optarg; 243 if ((c != EOF && c != -1) || 244 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) { 245 usage(); 246 } 247 if (vflag) { 248 if (cutarg != NULL) { 249 long lo; 250 long hi; 251 char dummy; 252 253 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) { 254 cuthiyear = hi; 255 } else if (sscanf(cutarg, "%ld,%ld%c", 256 &lo, &hi, &dummy) == 2) { 257 cutloyear = lo; 258 cuthiyear = hi; 259 } else { 260 errx(EXIT_FAILURE, 261 _("wild -c argument %s\n"), 262 cutarg); 263 } 264 } 265 setabsolutes(); 266 cutlotime = yeartot(cutloyear); 267 cuthitime = yeartot(cuthiyear); 268 } 269 time(&now); 270 longest = 0; 271 for (i = optind; i < argc; ++i) 272 if (strlen(argv[i]) > longest) 273 longest = strlen(argv[i]); 274 { 275 int from; 276 int to; 277 278 for (i = 0; environ[i] != NULL; ++i) 279 continue; 280 fakeenv = (char **) malloc((size_t) ((i + 2) * 281 sizeof *fakeenv)); 282 if (fakeenv == NULL || 283 (fakeenv[0] = (char *) malloc((size_t) (longest + 284 4))) == NULL) 285 errx(EXIT_FAILURE, 286 _("malloc() failed")); 287 to = 0; 288 strcpy(fakeenv[to++], "TZ="); 289 for (from = 0; environ[from] != NULL; ++from) 290 if (strncmp(environ[from], "TZ=", 3) != 0) 291 fakeenv[to++] = environ[from]; 292 fakeenv[to] = NULL; 293 environ = fakeenv; 294 } 295 for (i = optind; i < argc; ++i) { 296 static char buf[MAX_STRING_LENGTH]; 297 298 strcpy(&fakeenv[0][3], argv[i]); 299 if (!vflag) { 300 show(argv[i], now, FALSE); 301 continue; 302 } 303 warned = FALSE; 304 t = absolute_min_time; 305 show(argv[i], t, TRUE); 306 t += SECSPERHOUR * HOURSPERDAY; 307 show(argv[i], t, TRUE); 308 if (t < cutlotime) 309 t = cutlotime; 310 tmp = my_localtime(&t); 311 if (tmp != NULL) { 312 tm = *tmp; 313 strncpy(buf, abbr(&tm), (sizeof buf) - 1); 314 } 315 for ( ; ; ) { 316 if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12) 317 break; 318 newt = t + SECSPERHOUR * 12; 319 newtmp = localtime(&newt); 320 if (newtmp != NULL) 321 newtm = *newtmp; 322 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) : 323 (delta(&newtm, &tm) != (newt - t) || 324 newtm.tm_isdst != tm.tm_isdst || 325 strcmp(abbr(&newtm), buf) != 0)) { 326 newt = hunt(argv[i], t, newt); 327 newtmp = localtime(&newt); 328 if (newtmp != NULL) { 329 newtm = *newtmp; 330 strncpy(buf, 331 abbr(&newtm), 332 (sizeof buf) - 1); 333 } 334 } 335 t = newt; 336 tm = newtm; 337 tmp = newtmp; 338 } 339 t = absolute_max_time; 340 t -= SECSPERHOUR * HOURSPERDAY; 341 show(argv[i], t, TRUE); 342 t += SECSPERHOUR * HOURSPERDAY; 343 show(argv[i], t, TRUE); 344 } 345 if (fflush(stdout) || ferror(stdout)) 346 errx(EXIT_FAILURE, _("error writing standard output")); 347 exit(EXIT_SUCCESS); 348 /* If exit fails to exit... */ 349 return EXIT_FAILURE; 350 } 351 352 static void 353 setabsolutes(void) 354 { 355 if (0.5 == (time_t) 0.5) { 356 /* 357 ** time_t is floating. 358 */ 359 if (sizeof (time_t) == sizeof (float)) { 360 absolute_min_time = (time_t) -FLT_MAX; 361 absolute_max_time = (time_t) FLT_MAX; 362 } else if (sizeof (time_t) == sizeof (double)) { 363 absolute_min_time = (time_t) -DBL_MAX; 364 absolute_max_time = (time_t) DBL_MAX; 365 } else { 366 errx(EXIT_FAILURE, 367 _("use of -v on system with floating time_t other than float or double\n")); 368 } 369 } else if (0 > (time_t) -1) { 370 /* 371 ** time_t is signed. Assume overflow wraps around. 372 */ 373 time_t t = 0; 374 time_t t1 = 1; 375 376 while (t < t1) { 377 t = t1; 378 t1 = 2 * t1 + 1; 379 } 380 381 absolute_max_time = t; 382 t = -t; 383 absolute_min_time = t - 1; 384 if (t < absolute_min_time) 385 absolute_min_time = t; 386 } else { 387 /* 388 ** time_t is unsigned. 389 */ 390 absolute_min_time = 0; 391 absolute_max_time = absolute_min_time - 1; 392 } 393 } 394 395 static void 396 usage(void) 397 { 398 fprintf(stderr, _("usage: zdump [-v] [-c [loyear,]hiyear] zonename ...\n")); 399 exit(EXIT_FAILURE); 400 } 401 402 static time_t 403 yeartot(const long y) 404 { 405 long myy; 406 long seconds; 407 time_t t; 408 409 myy = EPOCH_YEAR; 410 t = 0; 411 while (myy != y) { 412 if (myy < y) { 413 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR; 414 ++myy; 415 if (t > absolute_max_time - seconds) { 416 t = absolute_max_time; 417 break; 418 } 419 t += seconds; 420 } else { 421 --myy; 422 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR; 423 if (t < absolute_min_time + seconds) { 424 t = absolute_min_time; 425 break; 426 } 427 t -= seconds; 428 } 429 } 430 return t; 431 } 432 433 static time_t 434 hunt(char *name, time_t lot, time_t hit) 435 { 436 time_t t; 437 long diff; 438 struct tm lotm; 439 struct tm * lotmp; 440 struct tm tm; 441 struct tm * tmp; 442 char loab[MAX_STRING_LENGTH]; 443 444 lotmp = my_localtime(&lot); 445 if (lotmp != NULL) { 446 lotm = *lotmp; 447 strncpy(loab, abbr(&lotm), (sizeof loab) - 1); 448 } 449 for ( ; ; ) { 450 diff = (long) (hit - lot); 451 if (diff < 2) 452 break; 453 t = lot; 454 t += diff / 2; 455 if (t <= lot) 456 ++t; 457 else if (t >= hit) 458 --t; 459 tmp = my_localtime(&t); 460 if (tmp != NULL) 461 tm = *tmp; 462 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) : 463 (delta(&tm, &lotm) == (t - lot) && 464 tm.tm_isdst == lotm.tm_isdst && 465 strcmp(abbr(&tm), loab) == 0)) { 466 lot = t; 467 lotm = tm; 468 lotmp = tmp; 469 } else hit = t; 470 } 471 show(name, lot, TRUE); 472 show(name, hit, TRUE); 473 return hit; 474 } 475 476 /* 477 ** Thanks to Paul Eggert for logic used in delta. 478 */ 479 480 static long 481 delta(struct tm *newp, struct tm *oldp) 482 { 483 long result; 484 int tmy; 485 486 if (newp->tm_year < oldp->tm_year) 487 return -delta(oldp, newp); 488 result = 0; 489 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy) 490 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE); 491 result += newp->tm_yday - oldp->tm_yday; 492 result *= HOURSPERDAY; 493 result += newp->tm_hour - oldp->tm_hour; 494 result *= MINSPERHOUR; 495 result += newp->tm_min - oldp->tm_min; 496 result *= SECSPERMIN; 497 result += newp->tm_sec - oldp->tm_sec; 498 return result; 499 } 500 501 static void 502 show(char *zone, time_t t, int v) 503 { 504 struct tm * tmp; 505 506 printf("%-*s ", (int) longest, zone); 507 if (v) { 508 tmp = gmtime(&t); 509 if (tmp == NULL) { 510 printf(tformat(), t); 511 } else { 512 dumptime(tmp); 513 printf(" UTC"); 514 } 515 printf(" = "); 516 } 517 tmp = my_localtime(&t); 518 dumptime(tmp); 519 if (tmp != NULL) { 520 if (*abbr(tmp) != '\0') 521 printf(" %s", abbr(tmp)); 522 if (v) { 523 printf(" isdst=%d", tmp->tm_isdst); 524 #ifdef TM_GMTOFF 525 printf(" gmtoff=%ld", tmp->TM_GMTOFF); 526 #endif /* defined TM_GMTOFF */ 527 } 528 } 529 printf("\n"); 530 if (tmp != NULL && *abbr(tmp) != '\0') 531 abbrok(abbr(tmp), zone); 532 } 533 534 static char * 535 abbr(struct tm *tmp) 536 { 537 char * result; 538 static char nada; 539 540 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1) 541 return &nada; 542 result = tzname[tmp->tm_isdst]; 543 return (result == NULL) ? &nada : result; 544 } 545 546 /* 547 ** The code below can fail on certain theoretical systems; 548 ** it works on all known real-world systems as of 2004-12-30. 549 */ 550 551 static const char * 552 tformat(void) 553 { 554 if (0.5 == (time_t) 0.5) { /* floating */ 555 if (sizeof (time_t) > sizeof (double)) 556 return "%Lg"; 557 return "%g"; 558 } 559 if (0 > (time_t) -1) { /* signed */ 560 if (sizeof (time_t) > sizeof (long)) 561 return "%lld"; 562 if (sizeof (time_t) > sizeof (int)) 563 return "%ld"; 564 return "%d"; 565 } 566 if (sizeof (time_t) > sizeof (unsigned long)) 567 return "%llu"; 568 if (sizeof (time_t) > sizeof (unsigned int)) 569 return "%lu"; 570 return "%u"; 571 } 572 573 static void 574 dumptime(const struct tm *timeptr) 575 { 576 static const char wday_name[][3] = { 577 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 578 }; 579 static const char mon_name[][3] = { 580 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 581 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 582 }; 583 const char * wn; 584 const char * mn; 585 int lead; 586 int trail; 587 588 if (timeptr == NULL) { 589 printf("NULL"); 590 return; 591 } 592 /* 593 ** The packaged versions of localtime and gmtime never put out-of-range 594 ** values in tm_wday or tm_mon, but since this code might be compiled 595 ** with other (perhaps experimental) versions, paranoia is in order. 596 */ 597 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= 598 (int) (sizeof wday_name / sizeof wday_name[0])) 599 wn = "???"; 600 else wn = wday_name[timeptr->tm_wday]; 601 if (timeptr->tm_mon < 0 || timeptr->tm_mon >= 602 (int) (sizeof mon_name / sizeof mon_name[0])) 603 mn = "???"; 604 else mn = mon_name[timeptr->tm_mon]; 605 printf("%.3s %.3s%3d %.2d:%.2d:%.2d ", 606 wn, mn, 607 timeptr->tm_mday, timeptr->tm_hour, 608 timeptr->tm_min, timeptr->tm_sec); 609 #define DIVISOR 10 610 trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR; 611 lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR + 612 trail / DIVISOR; 613 trail %= DIVISOR; 614 if (trail < 0 && lead > 0) { 615 trail += DIVISOR; 616 --lead; 617 } else if (lead < 0 && trail > 0) { 618 trail -= DIVISOR; 619 ++lead; 620 } 621 if (lead == 0) 622 printf("%d", trail); 623 else printf("%d%d", lead, ((trail < 0) ? -trail : trail)); 624 } 625