17c478bdstevel@tonic-gate/*
2f430f59robbin * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bdstevel@tonic-gate * Use is subject to license terms.
47c478bdstevel@tonic-gate */
57c478bdstevel@tonic-gate
67c478bdstevel@tonic-gate/*
7bc54f85John Levon * Copyright (c) 2018, Joyent, Inc.
8bc54f85John Levon */
9bc54f85John Levon
10bc54f85John Levon/*
117c478bdstevel@tonic-gate * zdump 7.24
127c478bdstevel@tonic-gate * Taken from elsie.nci.nih.gov to replace the existing Solaris zdump,
137c478bdstevel@tonic-gate * which was based on an earlier version of the elsie code.
147c478bdstevel@tonic-gate *
157c478bdstevel@tonic-gate * For zdump 7.24, the following changes were made to the elsie code:
167c478bdstevel@tonic-gate *   locale/textdomain/messages to match existing Solaris style.
177c478bdstevel@tonic-gate *   Solaris verbose mode is documented to display the current time first.
187c478bdstevel@tonic-gate *   cstyle cleaned code.
197c478bdstevel@tonic-gate *   removed old locale/textdomain code.
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate
22f430f59robbinstatic char	elsieid[] = "@(#)zdump.c	7.74";
237c478bdstevel@tonic-gate
247c478bdstevel@tonic-gate/*
257c478bdstevel@tonic-gate * This code has been made independent of the rest of the time
267c478bdstevel@tonic-gate * conversion package to increase confidence in the verification it provides.
277c478bdstevel@tonic-gate * You can use this code to help in verifying other implementations.
287c478bdstevel@tonic-gate */
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate#include "stdio.h"	/* for stdout, stderr, perror */
317c478bdstevel@tonic-gate#include "string.h"	/* for strcpy */
327c478bdstevel@tonic-gate#include "sys/types.h"	/* for time_t */
337c478bdstevel@tonic-gate#include "time.h"	/* for struct tm */
347c478bdstevel@tonic-gate#include "stdlib.h"	/* for exit, malloc, atoi */
357c478bdstevel@tonic-gate#include "locale.h"	/* for setlocale, textdomain */
367c478bdstevel@tonic-gate#include "libintl.h"
3780868c5robbin#include <ctype.h>
387c478bdstevel@tonic-gate#include "tzfile.h"	/* for defines */
3980868c5robbin#include <limits.h>
4080868c5robbin
4180868c5robbin#ifndef ZDUMP_LO_YEAR
4280868c5robbin#define	ZDUMP_LO_YEAR	(-500)
4380868c5robbin#endif /* !defined ZDUMP_LO_YEAR */
4480868c5robbin
4580868c5robbin#ifndef ZDUMP_HI_YEAR
4680868c5robbin#define	ZDUMP_HI_YEAR	2500
4780868c5robbin#endif /* !defined ZDUMP_HI_YEAR */
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate#ifndef MAX_STRING_LENGTH
507c478bdstevel@tonic-gate#define	MAX_STRING_LENGTH	1024
517c478bdstevel@tonic-gate#endif /* !defined MAX_STRING_LENGTH */
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gate#ifndef TRUE
547c478bdstevel@tonic-gate#define	TRUE		1
557c478bdstevel@tonic-gate#endif /* !defined TRUE */
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate#ifndef FALSE
587c478bdstevel@tonic-gate#define	FALSE		0
597c478bdstevel@tonic-gate#endif /* !defined FALSE */
607c478bdstevel@tonic-gate
6180868c5robbin#ifndef isleap_sum
6280868c5robbin/*
6380868c5robbin * See tzfile.h for details on isleap_sum.
6480868c5robbin */
6580868c5robbin#define	isleap_sum(a, b)	isleap((a) % 400 + (b) % 400)
6680868c5robbin#endif /* !defined isleap_sum */
6780868c5robbin
6880868c5robbin#ifndef SECSPERDAY
6980868c5robbin#define	SECSPERDAY	((long)SECSPERHOUR * HOURSPERDAY)
7080868c5robbin#endif
7180868c5robbin#define	SECSPERNYEAR	(SECSPERDAY * DAYSPERNYEAR)
7280868c5robbin#define	SECSPERLYEAR	(SECSPERNYEAR + SECSPERDAY)
7380868c5robbin
7480868c5robbin#ifndef GNUC_or_lint
757c478bdstevel@tonic-gate#ifdef lint
7680868c5robbin#define	GNUC_or_lint
7780868c5robbin#else /* !defined lint */
7880868c5robbin#ifdef __GNUC__
7980868c5robbin#define	GNUC_or_lint
8080868c5robbin#endif /* defined __GNUC__ */
8180868c5robbin#endif /* !defined lint */
8280868c5robbin#endif /* !defined GNUC_or_lint */
8380868c5robbin
8480868c5robbin#ifndef INITIALIZE
8580868c5robbin#ifdef	GNUC_or_lint
867c478bdstevel@tonic-gate#define	INITIALIZE(x)	((x) = 0)
8780868c5robbin#else /* !defined GNUC_or_lint */
887c478bdstevel@tonic-gate#define	INITIALIZE(x)
8980868c5robbin#endif /* !defined GNUC_or_lint */
907c478bdstevel@tonic-gate#endif /* !defined INITIALIZE */
917c478bdstevel@tonic-gate
9280868c5robbinstatic time_t	absolute_min_time;
9380868c5robbinstatic time_t	absolute_max_time;
9480868c5robbinstatic size_t	longest;
9580868c5robbinstatic char	*progname;
9680868c5robbinstatic int	warned;
977c478bdstevel@tonic-gate
9880868c5robbinstatic char	*abbr(struct tm *);
9980868c5robbinstatic void	abbrok(const char *, const char *);
1007c478bdstevel@tonic-gatestatic long	delta(struct tm *, struct tm *);
10180868c5robbinstatic void	dumptime(const struct tm *);
1027c478bdstevel@tonic-gatestatic time_t	hunt(char *, time_t, time_t);
10380868c5robbinstatic void	setabsolutes(void);
1047c478bdstevel@tonic-gatestatic void	show(char *, time_t, int);
10580868c5robbinstatic void	usage(void);
10680868c5robbinstatic const char	*tformat(void);
10780868c5robbinstatic time_t	yeartot(long y);
10880868c5robbin
10980868c5robbin#ifndef TYPECHECK
11080868c5robbin#define	my_localtime	localtime
11180868c5robbin#else /* !defined TYPECHECK */
11280868c5robbinstatic struct tm *
11380868c5robbinmy_localtime(tp)
11480868c5robbintime_t *tp;
11580868c5robbin{
11680868c5robbin	register struct tm *tmp;
11780868c5robbin
11880868c5robbin	tmp = localtime(tp);
11980868c5robbin	if (tp != NULL && tmp != NULL) {
12080868c5robbin		struct tm	tm;
12180868c5robbin		register time_t	t;
12280868c5robbin
12380868c5robbin		tm = *tmp;
12480868c5robbin		t = mktime(&tm);
12580868c5robbin		if (t - *tp >= 1 || *tp - t >= 1) {
12680868c5robbin			(void) fflush(stdout);
12780868c5robbin			(void) fprintf(stderr, "\n%s: ", progname);
12880868c5robbin			(void) fprintf(stderr, tformat(), *tp);
12980868c5robbin			(void) fprintf(stderr, " ->");
13080868c5robbin			(void) fprintf(stderr, " year=%d", tmp->tm_year);
13180868c5robbin			(void) fprintf(stderr, " mon=%d", tmp->tm_mon);
13280868c5robbin			(void) fprintf(stderr, " mday=%d", tmp->tm_mday);
13380868c5robbin			(void) fprintf(stderr, " hour=%d", tmp->tm_hour);
13480868c5robbin			(void) fprintf(stderr, " min=%d", tmp->tm_min);
13580868c5robbin			(void) fprintf(stderr, " sec=%d", tmp->tm_sec);
13680868c5robbin			(void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
13780868c5robbin			(void) fprintf(stderr, " -> ");
13880868c5robbin			(void) fprintf(stderr, tformat(), t);
13980868c5robbin			(void) fprintf(stderr, "\n");
14080868c5robbin		}
14180868c5robbin	}
14280868c5robbin	return (tmp);
14380868c5robbin}
14480868c5robbin#endif /* !defined TYPECHECK */
14580868c5robbin
14680868c5robbinstatic void
14767e157eDan McDonaldabbrok(const char * const abbrp, const char * const zone)
14880868c5robbin{
14980868c5robbin	register const char *cp;
15080868c5robbin	int error = 0;
15180868c5robbin
15280868c5robbin	if (warned)
15380868c5robbin		return;
15480868c5robbin	cp = abbrp;
15567e157eDan McDonald	while (isalpha(*cp) || isdigit(*cp) || *cp == '-' || *cp == '+')
15680868c5robbin		++cp;
15780868c5robbin	(void) fflush(stdout);
15867e157eDan McDonald	if (cp - abbrp < 3) {
15980868c5robbin		(void) fprintf(stderr, gettext("%s: warning: zone \"%s\" "
16080868c5robbin		    "abbreviation \"%s\" has fewer than 3 alphabetics\n"),
16180868c5robbin		    progname, zone, abbrp);
16280868c5robbin		error = 1;
16380868c5robbin	} else if (cp - abbrp > 6) {
16480868c5robbin		(void) fprintf(stderr, gettext("%s: warning: zone \"%s\" "
16567e157eDan McDonald		    "abbreviation \"%s\" has more than 6 characters\n"),
16680868c5robbin		    progname, zone, abbrp);
16780868c5robbin		error = 1;
16867e157eDan McDonald	} else if (*cp != '\0') {
16967e157eDan McDonald		(void) fprintf(stderr, gettext("%s: warning: zone \"%s\" "
17067e157eDan McDonald		    "abbreviation \"%s\" has characters other than "
17167e157eDan McDonald		    "alphanumerics\n"), progname, zone, abbrp);
17267e157eDan McDonald		error = 1;
17380868c5robbin	}
17480868c5robbin	if (error)
17580868c5robbin		warned = TRUE;
17680868c5robbin}
1777c478bdstevel@tonic-gate
1787c478bdstevel@tonic-gateint
1797c478bdstevel@tonic-gatemain(argc, argv)
1807c478bdstevel@tonic-gateint	argc;
18180868c5robbinchar	*argv[];
1827c478bdstevel@tonic-gate{
1837c478bdstevel@tonic-gate	register int		i;
1847c478bdstevel@tonic-gate	register int		c;
1857c478bdstevel@tonic-gate	register int		vflag;
18680868c5robbin	register char		*cutarg;
18780868c5robbin	register long		cutloyear = ZDUMP_LO_YEAR;
18880868c5robbin	register long		cuthiyear = ZDUMP_HI_YEAR;
18980868c5robbin	register time_t		cutlotime;
19080868c5robbin	register time_t		cuthitime;
1917c478bdstevel@tonic-gate	time_t			now;
1927c478bdstevel@tonic-gate	time_t			t;
1937c478bdstevel@tonic-gate	time_t			newt;
1947c478bdstevel@tonic-gate	struct tm		tm;
1957c478bdstevel@tonic-gate	struct tm		newtm;
19680868c5robbin	register struct tm	*tmp;
19780868c5robbin	register struct tm	*newtmp;
1987c478bdstevel@tonic-gate
19980868c5robbin	INITIALIZE(cutlotime);
20080868c5robbin	INITIALIZE(cuthitime);
2017c478bdstevel@tonic-gate
2027c478bdstevel@tonic-gate	(void) setlocale(LC_ALL, "");
2037c478bdstevel@tonic-gate#if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
2047c478bdstevel@tonic-gate#define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
2057c478bdstevel@tonic-gate#endif
2067c478bdstevel@tonic-gate	(void) textdomain(TEXT_DOMAIN);
2077c478bdstevel@tonic-gate
2087c478bdstevel@tonic-gate	progname = argv[0];
20980868c5robbin	for (i = 1; i < argc; ++i)
21080868c5robbin		if (strcmp(argv[i], "--version") == 0) {
21180868c5robbin			(void) printf("%s\n", elsieid);
21280868c5robbin			exit(EXIT_SUCCESS);
21380868c5robbin		}
2147c478bdstevel@tonic-gate	vflag = 0;
21580868c5robbin	cutarg = NULL;
2167c478bdstevel@tonic-gate	while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
2177c478bdstevel@tonic-gate		if (c == 'v')
2187c478bdstevel@tonic-gate			vflag = 1;
21980868c5robbin		else	cutarg = optarg;
2207c478bdstevel@tonic-gate	if (c != EOF ||
2217c478bdstevel@tonic-gate		(optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
22280868c5robbin			usage();
2237c478bdstevel@tonic-gate			/* NOTREACHED */
2247c478bdstevel@tonic-gate	}
22580868c5robbin	if (vflag) {
22680868c5robbin		if (cutarg != NULL) {
22780868c5robbin			long	lo;
22880868c5robbin			long	hi;
22980868c5robbin			char	dummy;
23080868c5robbin
23180868c5robbin			if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
23280868c5robbin				cuthiyear = hi;
23380868c5robbin			} else if (sscanf(cutarg, "%ld,%ld%c",
23480868c5robbin				&lo, &hi, &dummy) == 2) {
23580868c5robbin					cutloyear = lo;
23680868c5robbin					cuthiyear = hi;
23780868c5robbin			} else {
238bc54f85John Levon				(void) fprintf(stderr,
239bc54f85John Levon				    gettext("%s: wild -c argument %s\n"),
240bc54f85John Levon				    progname, cutarg);
24180868c5robbin				exit(EXIT_FAILURE);
24280868c5robbin			}
24380868c5robbin		}
24480868c5robbin		setabsolutes();
24580868c5robbin		cutlotime = yeartot(cutloyear);
24680868c5robbin		cuthitime = yeartot(cuthiyear);
2477c478bdstevel@tonic-gate	}
2487c478bdstevel@tonic-gate	(void) time(&now);
2497c478bdstevel@tonic-gate	longest = 0;
2507c478bdstevel@tonic-gate	for (i = optind; i < argc; ++i)
2517c478bdstevel@tonic-gate		if (strlen(argv[i]) > longest)
2527c478bdstevel@tonic-gate			longest = strlen(argv[i]);
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gate	for (i = optind; i < argc; ++i) {
2557c478bdstevel@tonic-gate		static char	buf[MAX_STRING_LENGTH];
25680868c5robbin		static char	*tzp = NULL;
2577c478bdstevel@tonic-gate
25880868c5robbin		(void) unsetenv("TZ");
25980868c5robbin		if (tzp != NULL)
26080868c5robbin			free(tzp);
26180868c5robbin		if ((tzp = malloc(3 + strlen(argv[i]) + 1)) == NULL) {
26280868c5robbin			perror(progname);
26380868c5robbin			exit(EXIT_FAILURE);
26480868c5robbin		}
26580868c5robbin		(void) strcpy(tzp, "TZ=");
26680868c5robbin		(void) strcat(tzp, argv[i]);
26780868c5robbin		if (putenv(tzp) != 0) {
26880868c5robbin			perror(progname);
26980868c5robbin			exit(EXIT_FAILURE);
27080868c5robbin		}
2717c478bdstevel@tonic-gate		if (!vflag) {
2727c478bdstevel@tonic-gate			show(argv[i], now, FALSE);
2737c478bdstevel@tonic-gate			continue;
2747c478bdstevel@tonic-gate		}
2757c478bdstevel@tonic-gate
2767c478bdstevel@tonic-gate#if defined(sun)
2777c478bdstevel@tonic-gate		/*
2787c478bdstevel@tonic-gate		 * We show the current time first, probably because we froze
2797c478bdstevel@tonic-gate		 * the behavior of zdump some time ago and then it got
2807c478bdstevel@tonic-gate		 * changed.
2817c478bdstevel@tonic-gate		 */
2827c478bdstevel@tonic-gate		show(argv[i], now, TRUE);
2837c478bdstevel@tonic-gate#endif
28480868c5robbin		warned = FALSE;
28580868c5robbin		t = absolute_min_time;
2867c478bdstevel@tonic-gate		show(argv[i], t, TRUE);
2877c478bdstevel@tonic-gate		t += SECSPERHOUR * HOURSPERDAY;
2887c478bdstevel@tonic-gate		show(argv[i], t, TRUE);
28980868c5robbin		if (t < cutlotime)
29080868c5robbin			t = cutlotime;
29180868c5robbin		tmp = my_localtime(&t);
29280868c5robbin		if (tmp != NULL) {
29380868c5robbin			tm = *tmp;
29480868c5robbin			(void) strncpy(buf, abbr(&tm), sizeof (buf) - 1);
29580868c5robbin		}
2967c478bdstevel@tonic-gate		for (;;) {
29780868c5robbin			if (t >= cuthitime)
2987c478bdstevel@tonic-gate				break;
299292f4c1robbin			/* check if newt will overrun maximum time_t value */
300292f4c1robbin			if (t > LONG_MAX - (SECSPERHOUR * 12))
301292f4c1robbin				break;
3027c478bdstevel@tonic-gate			newt = t + SECSPERHOUR * 12;
30380868c5robbin			if (newt >= cuthitime)
3047c478bdstevel@tonic-gate				break;
30580868c5robbin			newtmp = localtime(&newt);
30680868c5robbin			if (newtmp != NULL)
30780868c5robbin				newtm = *newtmp;
30880868c5robbin			if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
30980868c5robbin				(delta(&newtm, &tm) != (newt - t) ||
3107c478bdstevel@tonic-gate				newtm.tm_isdst != tm.tm_isdst ||
31180868c5robbin				strcmp(abbr(&newtm), buf) != 0)) {
3127c478bdstevel@tonic-gate					newt = hunt(argv[i], t, newt);
31380868c5robbin					newtmp = localtime(&newt);
31480868c5robbin					if (newtmp != NULL) {
31580868c5robbin						newtm = *newtmp;
31680868c5robbin						(void) strncpy(buf,
31780868c5robbin							abbr(&newtm),
31880868c5robbin							sizeof (buf) - 1);
31980868c5robbin					}
3207c478bdstevel@tonic-gate			}
3217c478bdstevel@tonic-gate			t = newt;
3227c478bdstevel@tonic-gate			tm = newtm;
32380868c5robbin			tmp = newtmp;
3247c478bdstevel@tonic-gate		}
32580868c5robbin		t = absolute_max_time;
3267c478bdstevel@tonic-gate#if defined(sun)
3277c478bdstevel@tonic-gate		show(argv[i], t, TRUE);
3287c478bdstevel@tonic-gate		t -= SECSPERHOUR * HOURSPERDAY;
3297c478bdstevel@tonic-gate		show(argv[i], t, TRUE);
3307c478bdstevel@tonic-gate#else /* !defined(sun) */
3317c478bdstevel@tonic-gate		t -= SECSPERHOUR * HOURSPERDAY;
3327c478bdstevel@tonic-gate		show(argv[i], t, TRUE);
3337c478bdstevel@tonic-gate		t += SECSPERHOUR * HOURSPERDAY;
3347c478bdstevel@tonic-gate		show(argv[i], t, TRUE);
3357c478bdstevel@tonic-gate#endif /* !defined(sun) */
3367c478bdstevel@tonic-gate	}
3377c478bdstevel@tonic-gate	if (fflush(stdout) || ferror(stdout)) {
33880868c5robbin		(void) fprintf(stderr, "%s: ", progname);
33980868c5robbin		(void) perror(gettext("Error writing standard output"));
34080868c5robbin		exit(EXIT_FAILURE);
3417c478bdstevel@tonic-gate	}
3427c478bdstevel@tonic-gate	return (EXIT_SUCCESS);
3437c478bdstevel@tonic-gate}
3447c478bdstevel@tonic-gate
34580868c5robbinstatic void
34680868c5robbinsetabsolutes()
34780868c5robbin{
34880868c5robbin#if defined(sun)
34980868c5robbin	absolute_min_time = LONG_MIN;
35080868c5robbin	absolute_max_time = LONG_MAX;
35180868c5robbin#else
35280868c5robbin	if (0.5 == (time_t)0.5) {
35380868c5robbin		/*
35480868c5robbin		 * time_t is floating.
35580868c5robbin		 */
35680868c5robbin		if (sizeof (time_t) == sizeof (float)) {
35780868c5robbin			absolute_min_time = (time_t)-FLT_MAX;
35880868c5robbin			absolute_max_time = (time_t)FLT_MAX;
35980868c5robbin		} else if (sizeof (time_t) == sizeof (double)) {
36080868c5robbin			absolute_min_time = (time_t)-DBL_MAX;
36180868c5robbin			absolute_max_time = (time_t)DBL_MAX;
36280868c5robbin		} else {
36380868c5robbin			(void) fprintf(stderr, gettext("%s: use of -v on "
36480868c5robbin			    "system with floating time_t other than float "
36580868c5robbin			    "or double\n"), progname);
36680868c5robbin			exit(EXIT_FAILURE);
36780868c5robbin		}
36880868c5robbin	} else
36980868c5robbin	/*CONSTANTCONDITION*/
37080868c5robbin	if (0 > (time_t)-1) {
37180868c5robbin		/*
37280868c5robbin		 * time_t is signed.
37380868c5robbin		 */
37480868c5robbin		register time_t	hibit;
37580868c5robbin
37680868c5robbin		for (hibit = 1; (hibit * 2) != 0; hibit *= 2)
37780868c5robbin			continue;
37880868c5robbin		absolute_min_time = hibit;
37980868c5robbin		absolute_max_time = -(hibit + 1);
38080868c5robbin	} else {
38180868c5robbin		/*
38280868c5robbin		 * time_t is unsigned.
38380868c5robbin		 */
38480868c5robbin		absolute_min_time = 0;
38580868c5robbin		absolute_max_time = absolute_min_time - 1;
38680868c5robbin	}
38780868c5robbin#endif
38880868c5robbin}
38980868c5robbin
39080868c5robbinstatic time_t
39180868c5robbinyeartot(y)
39280868c5robbinconst long	y;
39380868c5robbin{
39480868c5robbin	register long	myy;
39580868c5robbin	register long	seconds;
39680868c5robbin	register time_t	t;
39780868c5robbin
39880868c5robbin	myy = EPOCH_YEAR;
39980868c5robbin	t = 0;
40080868c5robbin	while (myy != y) {
40180868c5robbin		if (myy < y) {
40280868c5robbin			seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
40380868c5robbin			++myy;
40480868c5robbin			if (t > absolute_max_time - seconds) {
40580868c5robbin				t = absolute_max_time;
40680868c5robbin				break;
40780868c5robbin			}
40880868c5robbin			t += seconds;
40980868c5robbin		} else {
41080868c5robbin			--myy;
41180868c5robbin			seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
41280868c5robbin			if (t < absolute_min_time + seconds) {
41380868c5robbin				t = absolute_min_time;
41480868c5robbin				break;
41580868c5robbin			}
41680868c5robbin			t -= seconds;
41780868c5robbin		}
41880868c5robbin	}
41980868c5robbin	return (t);
42080868c5robbin}
42180868c5robbin
4227c478bdstevel@tonic-gatestatic time_t
4237c478bdstevel@tonic-gatehunt(name, lot, hit)
42480868c5robbinchar	*name;
4257c478bdstevel@tonic-gatetime_t	lot;
4267c478bdstevel@tonic-gatetime_t	hit;
4277c478bdstevel@tonic-gate{
42880868c5robbin	time_t			t;
42980868c5robbin	long			diff;
43080868c5robbin	struct tm		lotm;
43180868c5robbin	register struct tm	*lotmp;
43280868c5robbin	struct tm		tm;
43380868c5robbin	register struct tm	*tmp;
43480868c5robbin	char			loab[MAX_STRING_LENGTH];
43580868c5robbin
43680868c5robbin	lotmp = my_localtime(&lot);
43780868c5robbin	if (lotmp != NULL) {
43880868c5robbin		lotm = *lotmp;
43980868c5robbin		(void) strncpy(loab, abbr(&lotm), sizeof (loab) - 1);
44080868c5robbin	}
44180868c5robbin	for (;;) {
44280868c5robbin		diff = (long)(hit - lot);
44380868c5robbin		if (diff < 2)
44480868c5robbin			break;
44580868c5robbin		t = lot;
44680868c5robbin		t += diff / 2;
4477c478bdstevel@tonic-gate		if (t <= lot)
4487c478bdstevel@tonic-gate			++t;
4497c478bdstevel@tonic-gate		else if (t >= hit)
4507c478bdstevel@tonic-gate			--t;
45180868c5robbin		tmp = my_localtime(&t);
45280868c5robbin		if (tmp != NULL)
45380868c5robbin			tm = *tmp;
45480868c5robbin		if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
45580868c5robbin			(delta(&tm, &lotm) == (t - lot) &&
4567c478bdstevel@tonic-gate			tm.tm_isdst == lotm.tm_isdst &&
45780868c5robbin			strcmp(abbr(&tm), loab) == 0)) {
4587c478bdstevel@tonic-gate				lot = t;
4597c478bdstevel@tonic-gate				lotm = tm;
46080868c5robbin				lotmp = tmp;
4617c478bdstevel@tonic-gate		} else	hit = t;
4627c478bdstevel@tonic-gate	}
4637c478bdstevel@tonic-gate	show(name, lot, TRUE);
4647c478bdstevel@tonic-gate	show(name, hit, TRUE);
4657c478bdstevel@tonic-gate	return (hit);
4667c478bdstevel@tonic-gate}
4677c478bdstevel@tonic-gate
4687c478bdstevel@tonic-gate/*
469f430f59robbin * Thanks to Paul Eggert for logic used in delta.
4707c478bdstevel@tonic-gate */
4717c478bdstevel@tonic-gate
4727c478bdstevel@tonic-gatestatic long
4737c478bdstevel@tonic-gatedelta(newp, oldp)
47480868c5robbinstruct tm	*newp;
47580868c5robbinstruct tm	*oldp;
4767c478bdstevel@tonic-gate{
47780868c5robbin	register long	result;
47880868c5robbin	register int	tmy;
4797c478bdstevel@tonic-gate
4807c478bdstevel@tonic-gate	if (newp->tm_year < oldp->tm_year)
4817c478bdstevel@tonic-gate		return (-delta(oldp, newp));
4827c478bdstevel@tonic-gate	result = 0;
4837c478bdstevel@tonic-gate	for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
48480868c5robbin		result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
4857c478bdstevel@tonic-gate	result += newp->tm_yday - oldp->tm_yday;
4867c478bdstevel@tonic-gate	result *= HOURSPERDAY;
4877c478bdstevel@tonic-gate	result += newp->tm_hour - oldp->tm_hour;
4887c478bdstevel@tonic-gate	result *= MINSPERHOUR;
4897c478bdstevel@tonic-gate	result += newp->tm_min - oldp->tm_min;
4907c478bdstevel@tonic-gate	result *= SECSPERMIN;
4917c478bdstevel@tonic-gate	result += newp->tm_sec - oldp->tm_sec;
4927c478bdstevel@tonic-gate	return (result);
4937c478bdstevel@tonic-gate}
4947c478bdstevel@tonic-gate
4957c478bdstevel@tonic-gatestatic void
4967c478bdstevel@tonic-gateshow(zone, t, v)
49780868c5robbinchar	*zone;
4987c478bdstevel@tonic-gatetime_t	t;
4997c478bdstevel@tonic-gateint	v;
5007c478bdstevel@tonic-gate{
50180868c5robbin	register struct tm	*tmp;
5027c478bdstevel@tonic-gate
5037c478bdstevel@tonic-gate	(void) printf("%-*s  ", (int)longest, zone);
5047c478bdstevel@tonic-gate	if (v) {
50580868c5robbin		tmp = gmtime(&t);
50680868c5robbin		if (tmp == NULL) {
50780868c5robbin			(void) printf(tformat(), t);
50880868c5robbin		} else {
50980868c5robbin			dumptime(tmp);
51080868c5robbin			(void) printf(" UTC");
51180868c5robbin		}
51280868c5robbin		(void) printf(" = ");
51380868c5robbin	}
51480868c5robbin	tmp = my_localtime(&t);
51580868c5robbin	dumptime(tmp);
51680868c5robbin	if (tmp != NULL) {
51780868c5robbin		if (*abbr(tmp) != '\0')
51880868c5robbin			(void) printf(" %s", abbr(tmp));
51980868c5robbin		if (v) {
52080868c5robbin			(void) printf(" isdst=%d", tmp->tm_isdst);
52180868c5robbin#ifdef TM_GMTOFF
52280868c5robbin			(void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
52380868c5robbin#endif /* defined TM_GMTOFF */
52480868c5robbin		}
5257c478bdstevel@tonic-gate	}
5267c478bdstevel@tonic-gate	(void) printf("\n");
52780868c5robbin	if (tmp != NULL && *abbr(tmp) != '\0')
52880868c5robbin		abbrok(abbr(tmp), zone);
5297c478bdstevel@tonic-gate}
5307c478bdstevel@tonic-gate
5317c478bdstevel@tonic-gatestatic char *
5327c478bdstevel@tonic-gateabbr(tmp)
53380868c5robbinstruct tm	*tmp;
5347c478bdstevel@tonic-gate{
53580868c5robbin	register char	*result;
5367c478bdstevel@tonic-gate	static char	nada;
5377c478bdstevel@tonic-gate
5387c478bdstevel@tonic-gate	if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
5397c478bdstevel@tonic-gate		return (&nada);
5407c478bdstevel@tonic-gate	result = tzname[tmp->tm_isdst];
5417c478bdstevel@tonic-gate	return ((result == NULL) ? &nada : result);
5427c478bdstevel@tonic-gate}
5437c478bdstevel@tonic-gate
54480868c5robbin/*
54580868c5robbin * The code below can fail on certain theoretical systems;
54680868c5robbin * it works on all known real-world systems as of 2004-12-30.
54780868c5robbin */
54880868c5robbin
54980868c5robbinstatic const char *
55080868c5robbintformat()
55180868c5robbin{
55280868c5robbin#if defined(sun)
55380868c5robbin	/* time_t is signed long */
55480868c5robbin	return ("%ld");
55580868c5robbin#else
55680868c5robbin	/*CONSTANTCONDITION*/
55780868c5robbin	if (0.5 == (time_t)0.5) {	/* floating */
55880868c5robbin		/*CONSTANTCONDITION*/
55980868c5robbin		if (sizeof (time_t) > sizeof (double))
56080868c5robbin			return ("%Lg");
56180868c5robbin		return ("%g");
56280868c5robbin	}
56380868c5robbin	/*CONSTANTCONDITION*/
56480868c5robbin	if (0 > (time_t)-1) {		/* signed */
56580868c5robbin		/*CONSTANTCONDITION*/
56680868c5robbin		if (sizeof (time_t) > sizeof (long))
56780868c5robbin			return ("%lld");
56880868c5robbin		/*CONSTANTCONDITION*/
56980868c5robbin		if (sizeof (time_t) > sizeof (int))
57080868c5robbin			return ("%ld");
57180868c5robbin		return ("%d");
57280868c5robbin	}
57380868c5robbin	/*CONSTANTCONDITION*/
57480868c5robbin	if (sizeof (time_t) > sizeof (unsigned long))
57580868c5robbin		return ("%llu");
57680868c5robbin	/*CONSTANTCONDITION*/
57780868c5robbin	if (sizeof (time_t) > sizeof (unsigned int))
57880868c5robbin		return ("%lu");
57980868c5robbin	return ("%u");
58080868c5robbin#endif
58180868c5robbin}
58280868c5robbin
58380868c5robbinstatic void
58480868c5robbindumptime(timeptr)
58580868c5robbinregister const struct tm	*timeptr;
58680868c5robbin{
58780868c5robbin	static const char	wday_name[][3] = {
58880868c5robbin		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
58980868c5robbin	};
59080868c5robbin	static const char	mon_name[][3] = {
59180868c5robbin		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
59280868c5robbin		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
59380868c5robbin	};
59480868c5robbin	register const char	*wn;
59580868c5robbin	register const char	*mn;
59680868c5robbin	register int		lead;
59780868c5robbin	register int		trail;
59880868c5robbin
59980868c5robbin	if (timeptr == NULL) {
60080868c5robbin		(void) printf("NULL");
60180868c5robbin		return;
60280868c5robbin	}
60380868c5robbin	/*
60480868c5robbin	 * The packaged versions of localtime and gmtime never put out-of-range
60580868c5robbin	 * values in tm_wday or tm_mon, but since this code might be compiled
60680868c5robbin	 * with other (perhaps experimental) versions, paranoia is in order.
60780868c5robbin	 */
60880868c5robbin	if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
60980868c5robbin		(int)(sizeof (wday_name) / sizeof (wday_name[0])))
61080868c5robbin			wn = "???";
61180868c5robbin	else		wn = wday_name[timeptr->tm_wday];
61280868c5robbin	if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
61380868c5robbin		(int)(sizeof (mon_name) / sizeof (mon_name[0])))
61480868c5robbin			mn = "???";
61580868c5robbin	else		mn = mon_name[timeptr->tm_mon];
61680868c5robbin	(void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
61780868c5robbin		wn, mn,
61880868c5robbin		timeptr->tm_mday, timeptr->tm_hour,
61980868c5robbin		timeptr->tm_min, timeptr->tm_sec);
62080868c5robbin#define	DIVISOR	10
62180868c5robbin	trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
62280868c5robbin	lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
62380868c5robbin		trail / DIVISOR;
62480868c5robbin	trail %= DIVISOR;
62580868c5robbin	if (trail < 0 && lead > 0) {
62680868c5robbin		trail += DIVISOR;
62780868c5robbin		--lead;
62880868c5robbin	} else if (lead < 0 && trail > 0) {
62980868c5robbin		trail -= DIVISOR;
63080868c5robbin		++lead;
63180868c5robbin	}
63280868c5robbin	if (lead == 0)
63380868c5robbin		(void) printf("%d", trail);
63480868c5robbin	else
63580868c5robbin		(void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
63680868c5robbin}
63780868c5robbin
6387c478bdstevel@tonic-gatestatic void
63980868c5robbinusage()
6407c478bdstevel@tonic-gate{
6417c478bdstevel@tonic-gate	(void) fprintf(stderr, gettext(
64280868c5robbin	    "%s: [ --version ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n"),
64380868c5robbin		progname);
64480868c5robbin	exit(EXIT_FAILURE);
6457c478bdstevel@tonic-gate	/* NOTREACHED */
6467c478bdstevel@tonic-gate}
647