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