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