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