Home | History | Annotate | Line # | Download | only in time
zdump.c revision 1.2
      1  1.2  jtc /*	$NetBSD: zdump.c,v 1.2 1995/03/10 18:12:43 jtc Exp $	*/
      2  1.2  jtc 
      3  1.1  jtc #ifndef lint
      4  1.1  jtc #ifndef NOID
      5  1.1  jtc static char	elsieid[] = "@(#)zdump.c	7.20";
      6  1.1  jtc #endif /* !defined NOID */
      7  1.1  jtc #endif /* !defined lint */
      8  1.1  jtc 
      9  1.1  jtc /*
     10  1.1  jtc ** This code has been made independent of the rest of the time
     11  1.1  jtc ** conversion package to increase confidence in the verification it provides.
     12  1.1  jtc ** You can use this code to help in verifying other implementations.
     13  1.1  jtc */
     14  1.1  jtc 
     15  1.1  jtc #include "stdio.h"	/* for stdout, stderr, perror */
     16  1.1  jtc #include "string.h"	/* for strcpy */
     17  1.1  jtc #include "sys/types.h"	/* for time_t */
     18  1.1  jtc #include "time.h"	/* for struct tm */
     19  1.1  jtc #include "stdlib.h"	/* for exit, malloc, atoi */
     20  1.1  jtc 
     21  1.1  jtc #ifndef MAX_STRING_LENGTH
     22  1.1  jtc #define MAX_STRING_LENGTH	1024
     23  1.1  jtc #endif /* !defined MAX_STRING_LENGTH */
     24  1.1  jtc 
     25  1.1  jtc #ifndef TRUE
     26  1.1  jtc #define TRUE		1
     27  1.1  jtc #endif /* !defined TRUE */
     28  1.1  jtc 
     29  1.1  jtc #ifndef FALSE
     30  1.1  jtc #define FALSE		0
     31  1.1  jtc #endif /* !defined FALSE */
     32  1.1  jtc 
     33  1.1  jtc #ifndef EXIT_SUCCESS
     34  1.1  jtc #define EXIT_SUCCESS	0
     35  1.1  jtc #endif /* !defined EXIT_SUCCESS */
     36  1.1  jtc 
     37  1.1  jtc #ifndef EXIT_FAILURE
     38  1.1  jtc #define EXIT_FAILURE	1
     39  1.1  jtc #endif /* !defined EXIT_FAILURE */
     40  1.1  jtc 
     41  1.1  jtc #ifndef SECSPERMIN
     42  1.1  jtc #define SECSPERMIN	60
     43  1.1  jtc #endif /* !defined SECSPERMIN */
     44  1.1  jtc 
     45  1.1  jtc #ifndef MINSPERHOUR
     46  1.1  jtc #define MINSPERHOUR	60
     47  1.1  jtc #endif /* !defined MINSPERHOUR */
     48  1.1  jtc 
     49  1.1  jtc #ifndef SECSPERHOUR
     50  1.1  jtc #define SECSPERHOUR	(SECSPERMIN * MINSPERHOUR)
     51  1.1  jtc #endif /* !defined SECSPERHOUR */
     52  1.1  jtc 
     53  1.1  jtc #ifndef HOURSPERDAY
     54  1.1  jtc #define HOURSPERDAY	24
     55  1.1  jtc #endif /* !defined HOURSPERDAY */
     56  1.1  jtc 
     57  1.1  jtc #ifndef EPOCH_YEAR
     58  1.1  jtc #define EPOCH_YEAR	1970
     59  1.1  jtc #endif /* !defined EPOCH_YEAR */
     60  1.1  jtc 
     61  1.1  jtc #ifndef TM_YEAR_BASE
     62  1.1  jtc #define TM_YEAR_BASE	1900
     63  1.1  jtc #endif /* !defined TM_YEAR_BASE */
     64  1.1  jtc 
     65  1.1  jtc #ifndef DAYSPERNYEAR
     66  1.1  jtc #define DAYSPERNYEAR	365
     67  1.1  jtc #endif /* !defined DAYSPERNYEAR */
     68  1.1  jtc 
     69  1.1  jtc #ifndef isleap
     70  1.1  jtc #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
     71  1.1  jtc #endif /* !defined isleap */
     72  1.1  jtc 
     73  1.1  jtc #ifndef GNUC_or_lint
     74  1.1  jtc #ifdef lint
     75  1.1  jtc #define GNUC_or_lint
     76  1.1  jtc #endif /* defined lint */
     77  1.1  jtc #ifndef lint
     78  1.1  jtc #ifdef __GNUC__
     79  1.1  jtc #define GNUC_or_lint
     80  1.1  jtc #endif /* defined __GNUC__ */
     81  1.1  jtc #endif /* !defined lint */
     82  1.1  jtc #endif /* !defined GNUC_or_lint */
     83  1.1  jtc 
     84  1.1  jtc #ifndef INITIALIZE
     85  1.1  jtc #ifdef GNUC_or_lint
     86  1.1  jtc #define INITIALIZE(x)	((x) = 0)
     87  1.1  jtc #endif /* defined GNUC_or_lint */
     88  1.1  jtc #ifndef GNUC_or_lint
     89  1.1  jtc #define INITIALIZE(x)
     90  1.1  jtc #endif /* !defined GNUC_or_lint */
     91  1.1  jtc #endif /* !defined INITIALIZE */
     92  1.1  jtc 
     93  1.1  jtc extern char **	environ;
     94  1.1  jtc extern int	getopt();
     95  1.1  jtc extern char *	optarg;
     96  1.1  jtc extern int	optind;
     97  1.1  jtc extern time_t	time();
     98  1.1  jtc extern char *	tzname[2];
     99  1.1  jtc 
    100  1.1  jtc static char *	abbr();
    101  1.1  jtc static long	delta();
    102  1.1  jtc static time_t	hunt();
    103  1.1  jtc static int	longest;
    104  1.1  jtc static char *	progname;
    105  1.1  jtc static void	show();
    106  1.1  jtc 
    107  1.1  jtc int
    108  1.1  jtc main(argc, argv)
    109  1.1  jtc int	argc;
    110  1.1  jtc char *	argv[];
    111  1.1  jtc {
    112  1.1  jtc 	register int		i;
    113  1.1  jtc 	register int		c;
    114  1.1  jtc 	register int		vflag;
    115  1.1  jtc 	register char *		cutoff;
    116  1.1  jtc 	register int		cutyear;
    117  1.1  jtc 	register long		cuttime;
    118  1.1  jtc 	char **			fakeenv;
    119  1.1  jtc 	time_t			now;
    120  1.1  jtc 	time_t			t;
    121  1.1  jtc 	time_t			newt;
    122  1.1  jtc 	time_t			hibit;
    123  1.1  jtc 	struct tm		tm;
    124  1.1  jtc 	struct tm		newtm;
    125  1.1  jtc 
    126  1.1  jtc 	INITIALIZE(cuttime);
    127  1.1  jtc 	progname = argv[0];
    128  1.1  jtc 	vflag = 0;
    129  1.1  jtc 	cutoff = NULL;
    130  1.1  jtc 	while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
    131  1.1  jtc 		if (c == 'v')
    132  1.1  jtc 			vflag = 1;
    133  1.1  jtc 		else	cutoff = optarg;
    134  1.1  jtc 	if (c != EOF ||
    135  1.1  jtc 		(optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
    136  1.1  jtc 			(void) fprintf(stderr,
    137  1.1  jtc "%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n",
    138  1.1  jtc 				argv[0], argv[0]);
    139  1.1  jtc 			(void) exit(EXIT_FAILURE);
    140  1.1  jtc 	}
    141  1.1  jtc 	if (cutoff != NULL) {
    142  1.1  jtc 		int	y;
    143  1.1  jtc 
    144  1.1  jtc 		cutyear = atoi(cutoff);
    145  1.1  jtc 		cuttime = 0;
    146  1.1  jtc 		for (y = EPOCH_YEAR; y < cutyear; ++y)
    147  1.1  jtc 			cuttime += DAYSPERNYEAR + isleap(y);
    148  1.1  jtc 		cuttime *= SECSPERHOUR * HOURSPERDAY;
    149  1.1  jtc 	}
    150  1.1  jtc 	(void) time(&now);
    151  1.1  jtc 	longest = 0;
    152  1.1  jtc 	for (i = optind; i < argc; ++i)
    153  1.1  jtc 		if (strlen(argv[i]) > longest)
    154  1.1  jtc 			longest = strlen(argv[i]);
    155  1.1  jtc 	for (hibit = 1; (hibit << 1) != 0; hibit <<= 1)
    156  1.1  jtc 		continue;
    157  1.1  jtc 	{
    158  1.1  jtc 		register int	from;
    159  1.1  jtc 		register int	to;
    160  1.1  jtc 
    161  1.1  jtc 		for (i = 0;  environ[i] != NULL;  ++i)
    162  1.1  jtc 			continue;
    163  1.1  jtc 		fakeenv = (char **) malloc((size_t) ((i + 2) *
    164  1.1  jtc 			sizeof *fakeenv));
    165  1.1  jtc 		if (fakeenv == NULL ||
    166  1.1  jtc 			(fakeenv[0] = (char *) malloc((size_t) (longest +
    167  1.1  jtc 				4))) == NULL) {
    168  1.1  jtc 					(void) perror(progname);
    169  1.1  jtc 					(void) exit(EXIT_FAILURE);
    170  1.1  jtc 		}
    171  1.1  jtc 		to = 0;
    172  1.1  jtc 		(void) strcpy(fakeenv[to++], "TZ=");
    173  1.1  jtc 		for (from = 0; environ[from] != NULL; ++from)
    174  1.1  jtc 			if (strncmp(environ[from], "TZ=", 3) != 0)
    175  1.1  jtc 				fakeenv[to++] = environ[from];
    176  1.1  jtc 		fakeenv[to] = NULL;
    177  1.1  jtc 		environ = fakeenv;
    178  1.1  jtc 	}
    179  1.1  jtc 	for (i = optind; i < argc; ++i) {
    180  1.1  jtc 		static char	buf[MAX_STRING_LENGTH];
    181  1.1  jtc 
    182  1.1  jtc 		(void) strcpy(&fakeenv[0][3], argv[i]);
    183  1.1  jtc 		show(argv[i], now, FALSE);
    184  1.1  jtc 		if (!vflag)
    185  1.1  jtc 			continue;
    186  1.1  jtc 		/*
    187  1.1  jtc 		** Get lowest value of t.
    188  1.1  jtc 		*/
    189  1.1  jtc 		t = hibit;
    190  1.1  jtc 		if (t > 0)		/* time_t is unsigned */
    191  1.1  jtc 			t = 0;
    192  1.1  jtc 		show(argv[i], t, TRUE);
    193  1.1  jtc 		t += SECSPERHOUR * HOURSPERDAY;
    194  1.1  jtc 		show(argv[i], t, TRUE);
    195  1.1  jtc 		tm = *localtime(&t);
    196  1.1  jtc 		(void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
    197  1.1  jtc 		for ( ; ; ) {
    198  1.1  jtc 			if (cutoff != NULL && t >= cuttime)
    199  1.1  jtc 				break;
    200  1.1  jtc 			newt = t + SECSPERHOUR * 12;
    201  1.1  jtc 			if (cutoff != NULL && newt >= cuttime)
    202  1.1  jtc 				break;
    203  1.1  jtc 			if (newt <= t)
    204  1.1  jtc 				break;
    205  1.1  jtc 			newtm = *localtime(&newt);
    206  1.1  jtc 			if (delta(&newtm, &tm) != (newt - t) ||
    207  1.1  jtc 				newtm.tm_isdst != tm.tm_isdst ||
    208  1.1  jtc 				strcmp(abbr(&newtm), buf) != 0) {
    209  1.1  jtc 					newt = hunt(argv[i], t, newt);
    210  1.1  jtc 					newtm = *localtime(&newt);
    211  1.1  jtc 					(void) strncpy(buf, abbr(&newtm),
    212  1.1  jtc 						(sizeof buf) - 1);
    213  1.1  jtc 			}
    214  1.1  jtc 			t = newt;
    215  1.1  jtc 			tm = newtm;
    216  1.1  jtc 		}
    217  1.1  jtc 		/*
    218  1.1  jtc 		** Get highest value of t.
    219  1.1  jtc 		*/
    220  1.1  jtc 		t = ~((time_t) 0);
    221  1.1  jtc 		if (t < 0)		/* time_t is signed */
    222  1.1  jtc 			t &= ~hibit;
    223  1.1  jtc 		t -= SECSPERHOUR * HOURSPERDAY;
    224  1.1  jtc 		show(argv[i], t, TRUE);
    225  1.1  jtc 		t += SECSPERHOUR * HOURSPERDAY;
    226  1.1  jtc 		show(argv[i], t, TRUE);
    227  1.1  jtc 	}
    228  1.1  jtc 	if (fflush(stdout) || ferror(stdout)) {
    229  1.1  jtc 		(void) fprintf(stderr, "%s: Error writing standard output ",
    230  1.1  jtc 			argv[0]);
    231  1.1  jtc 		(void) perror("standard output");
    232  1.1  jtc 		(void) exit(EXIT_FAILURE);
    233  1.1  jtc 	}
    234  1.1  jtc 	exit(EXIT_SUCCESS);
    235  1.1  jtc 
    236  1.1  jtc 	/* gcc -Wall pacifier */
    237  1.1  jtc 	for ( ; ; )
    238  1.1  jtc 		continue;
    239  1.1  jtc }
    240  1.1  jtc 
    241  1.1  jtc static time_t
    242  1.1  jtc hunt(name, lot, hit)
    243  1.1  jtc char *	name;
    244  1.1  jtc time_t	lot;
    245  1.1  jtc time_t	hit;
    246  1.1  jtc {
    247  1.1  jtc 	time_t		t;
    248  1.1  jtc 	struct tm	lotm;
    249  1.1  jtc 	struct tm	tm;
    250  1.1  jtc 	static char	loab[MAX_STRING_LENGTH];
    251  1.1  jtc 
    252  1.1  jtc 	lotm = *localtime(&lot);
    253  1.1  jtc 	(void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
    254  1.1  jtc 	while ((hit - lot) >= 2) {
    255  1.1  jtc 		t = lot / 2 + hit / 2;
    256  1.1  jtc 		if (t <= lot)
    257  1.1  jtc 			++t;
    258  1.1  jtc 		else if (t >= hit)
    259  1.1  jtc 			--t;
    260  1.1  jtc 		tm = *localtime(&t);
    261  1.1  jtc 		if (delta(&tm, &lotm) == (t - lot) &&
    262  1.1  jtc 			tm.tm_isdst == lotm.tm_isdst &&
    263  1.1  jtc 			strcmp(abbr(&tm), loab) == 0) {
    264  1.1  jtc 				lot = t;
    265  1.1  jtc 				lotm = tm;
    266  1.1  jtc 		} else	hit = t;
    267  1.1  jtc 	}
    268  1.1  jtc 	show(name, lot, TRUE);
    269  1.1  jtc 	show(name, hit, TRUE);
    270  1.1  jtc 	return hit;
    271  1.1  jtc }
    272  1.1  jtc 
    273  1.1  jtc /*
    274  1.1  jtc ** Thanks to Paul Eggert (eggert (at) twinsun.com) for logic used in delta.
    275  1.1  jtc */
    276  1.1  jtc 
    277  1.1  jtc static long
    278  1.1  jtc delta(newp, oldp)
    279  1.1  jtc struct tm *	newp;
    280  1.1  jtc struct tm *	oldp;
    281  1.1  jtc {
    282  1.1  jtc 	long	result;
    283  1.1  jtc 	int	tmy;
    284  1.1  jtc 
    285  1.1  jtc 	if (newp->tm_year < oldp->tm_year)
    286  1.1  jtc 		return -delta(oldp, newp);
    287  1.1  jtc 	result = 0;
    288  1.1  jtc 	for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
    289  1.1  jtc 		result += DAYSPERNYEAR + isleap(tmy + TM_YEAR_BASE);
    290  1.1  jtc 	result += newp->tm_yday - oldp->tm_yday;
    291  1.1  jtc 	result *= HOURSPERDAY;
    292  1.1  jtc 	result += newp->tm_hour - oldp->tm_hour;
    293  1.1  jtc 	result *= MINSPERHOUR;
    294  1.1  jtc 	result += newp->tm_min - oldp->tm_min;
    295  1.1  jtc 	result *= SECSPERMIN;
    296  1.1  jtc 	result += newp->tm_sec - oldp->tm_sec;
    297  1.1  jtc 	return result;
    298  1.1  jtc }
    299  1.1  jtc 
    300  1.1  jtc extern struct tm *	localtime();
    301  1.1  jtc 
    302  1.1  jtc static void
    303  1.1  jtc show(zone, t, v)
    304  1.1  jtc char *	zone;
    305  1.1  jtc time_t	t;
    306  1.1  jtc int	v;
    307  1.1  jtc {
    308  1.1  jtc 	struct tm *	tmp;
    309  1.1  jtc 
    310  1.1  jtc 	(void) printf("%-*s  ", longest, zone);
    311  1.1  jtc 	if (v)
    312  1.1  jtc 		(void) printf("%.24s GMT = ", asctime(gmtime(&t)));
    313  1.1  jtc 	tmp = localtime(&t);
    314  1.1  jtc 	(void) printf("%.24s", asctime(tmp));
    315  1.1  jtc 	if (*abbr(tmp) != '\0')
    316  1.1  jtc 		(void) printf(" %s", abbr(tmp));
    317  1.1  jtc 	if (v) {
    318  1.1  jtc 		(void) printf(" isdst=%d", tmp->tm_isdst);
    319  1.1  jtc #ifdef TM_GMTOFF
    320  1.1  jtc 		(void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
    321  1.1  jtc #endif /* defined TM_GMTOFF */
    322  1.1  jtc 	}
    323  1.1  jtc 	(void) printf("\n");
    324  1.1  jtc }
    325  1.1  jtc 
    326  1.1  jtc static char *
    327  1.1  jtc abbr(tmp)
    328  1.1  jtc struct tm *	tmp;
    329  1.1  jtc {
    330  1.1  jtc 	register char *	result;
    331  1.1  jtc 	static char	nada;
    332  1.1  jtc 
    333  1.1  jtc 	if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
    334  1.1  jtc 		return &nada;
    335  1.1  jtc 	result = tzname[tmp->tm_isdst];
    336  1.1  jtc 	return (result == NULL) ? &nada : result;
    337  1.1  jtc }
    338