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