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