Home | History | Annotate | Line # | Download | only in time
zdump.c revision 1.25.2.3
      1 /*	$NetBSD: zdump.c,v 1.25.2.3 2013/06/23 06:21:06 tls Exp $	*/
      2 /*
      3 ** This file is in the public domain, so clarified as of
      4 ** 2009-05-17 by Arthur David Olson.
      5 */
      6 
      7 #include <sys/cdefs.h>
      8 #ifndef lint
      9 __RCSID("$NetBSD: zdump.c,v 1.25.2.3 2013/06/23 06:21:06 tls Exp $");
     10 #endif /* !defined lint */
     11 
     12 #include "version.h"
     13 /*
     14 ** This code has been made independent of the rest of the time
     15 ** conversion package to increase confidence in the verification it provides.
     16 ** You can use this code to help in verifying other implementations.
     17 */
     18 
     19 #include "stdio.h"	/* for stdout, stderr */
     20 #include "string.h"	/* for strcpy */
     21 #include "sys/types.h"	/* for time_t */
     22 #include "time.h"	/* for struct tm */
     23 #include "stdlib.h"	/* for exit, malloc, atoi */
     24 #include <err.h>
     25 #include "float.h"	/* for FLT_MAX and DBL_MAX */
     26 #include "ctype.h"	/* for isalpha et al. */
     27 #ifndef isascii
     28 #define isascii(x) 1
     29 #endif /* !defined isascii */
     30 
     31 #include "private.h"
     32 
     33 #ifndef ZDUMP_LO_YEAR
     34 #define ZDUMP_LO_YEAR	(-500)
     35 #endif /* !defined ZDUMP_LO_YEAR */
     36 
     37 #ifndef ZDUMP_HI_YEAR
     38 #define ZDUMP_HI_YEAR	2500
     39 #endif /* !defined ZDUMP_HI_YEAR */
     40 
     41 #ifndef MAX_STRING_LENGTH
     42 #define MAX_STRING_LENGTH	1024
     43 #endif /* !defined MAX_STRING_LENGTH */
     44 
     45 #ifndef TRUE
     46 #define TRUE		1
     47 #endif /* !defined TRUE */
     48 
     49 #ifndef FALSE
     50 #define FALSE		0
     51 #endif /* !defined FALSE */
     52 
     53 #ifndef EXIT_SUCCESS
     54 #define EXIT_SUCCESS	0
     55 #endif /* !defined EXIT_SUCCESS */
     56 
     57 #ifndef EXIT_FAILURE
     58 #define EXIT_FAILURE	1
     59 #endif /* !defined EXIT_FAILURE */
     60 
     61 #ifndef SECSPERMIN
     62 #define SECSPERMIN	60
     63 #endif /* !defined SECSPERMIN */
     64 
     65 #ifndef MINSPERHOUR
     66 #define MINSPERHOUR	60
     67 #endif /* !defined MINSPERHOUR */
     68 
     69 #ifndef SECSPERHOUR
     70 #define SECSPERHOUR	(SECSPERMIN * MINSPERHOUR)
     71 #endif /* !defined SECSPERHOUR */
     72 
     73 #ifndef HOURSPERDAY
     74 #define HOURSPERDAY	24
     75 #endif /* !defined HOURSPERDAY */
     76 
     77 #ifndef EPOCH_YEAR
     78 #define EPOCH_YEAR	1970
     79 #endif /* !defined EPOCH_YEAR */
     80 
     81 #ifndef TM_YEAR_BASE
     82 #define TM_YEAR_BASE	1900
     83 #endif /* !defined TM_YEAR_BASE */
     84 
     85 #ifndef DAYSPERNYEAR
     86 #define DAYSPERNYEAR	365
     87 #endif /* !defined DAYSPERNYEAR */
     88 
     89 #ifndef isleap
     90 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
     91 #endif /* !defined isleap */
     92 
     93 #ifndef isleap_sum
     94 /*
     95 ** See tzfile.h for details on isleap_sum.
     96 */
     97 #define isleap_sum(a, b)	isleap((a) % 400 + (b) % 400)
     98 #endif /* !defined isleap_sum */
     99 
    100 #define SECSPERDAY	((long) SECSPERHOUR * HOURSPERDAY)
    101 #define SECSPERNYEAR	(SECSPERDAY * DAYSPERNYEAR)
    102 #define SECSPERLYEAR	(SECSPERNYEAR + SECSPERDAY)
    103 
    104 #ifndef HAVE_GETTEXT
    105 #define HAVE_GETTEXT 0
    106 #endif
    107 #if HAVE_GETTEXT
    108 #include "locale.h"	/* for setlocale */
    109 #include "libintl.h"
    110 #endif /* HAVE_GETTEXT */
    111 
    112 #ifndef GNUC_or_lint
    113 #ifdef lint
    114 #define GNUC_or_lint
    115 #else /* !defined lint */
    116 #ifdef __GNUC__
    117 #define GNUC_or_lint
    118 #endif /* defined __GNUC__ */
    119 #endif /* !defined lint */
    120 #endif /* !defined GNUC_or_lint */
    121 
    122 #ifndef __pure
    123 #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
    124 # define __pure __attribute__ ((__pure__))
    125 #else
    126 # define __pure /* empty */
    127 #endif
    128 #endif
    129 
    130 #ifndef INITIALIZE
    131 #ifdef GNUC_or_lint
    132 #define INITIALIZE(x)	((x) = 0)
    133 #else /* !defined GNUC_or_lint */
    134 #define INITIALIZE(x)
    135 #endif /* !defined GNUC_or_lint */
    136 #endif /* !defined INITIALIZE */
    137 
    138 /*
    139 ** For the benefit of GNU folk...
    140 ** `_(MSGID)' uses the current locale's message library string for MSGID.
    141 ** The default is to use gettext if available, and use MSGID otherwise.
    142 */
    143 
    144 #ifndef _
    145 #if HAVE_GETTEXT
    146 #define _(msgid) gettext(msgid)
    147 #else /* !HAVE_GETTEXT */
    148 #define _(msgid) msgid
    149 #endif /* !HAVE_GETTEXT */
    150 #endif /* !defined _ */
    151 
    152 #ifndef TZ_DOMAIN
    153 #define TZ_DOMAIN "tz"
    154 #endif /* !defined TZ_DOMAIN */
    155 
    156 extern char **	environ;
    157 extern int	getopt(int argc, char * const argv[],
    158 			const char * options);
    159 extern char *	optarg;
    160 extern int	optind;
    161 
    162 static time_t	absolute_min_time;
    163 static time_t	absolute_max_time;
    164 static size_t	longest;
    165 static char *	progname;
    166 static int	warned;
    167 
    168 static const char *	abbr(struct tm * tmp);
    169 static void	abbrok(const char * abbrp, const char * zone);
    170 static long	delta(struct tm * newp, struct tm * oldp) __pure;
    171 static void	dumptime(const struct tm * tmp);
    172 static time_t	hunt(char * name, time_t lot, time_t	hit);
    173 int		main(int, char **);
    174 static void	setabsolutes(void);
    175 static void	show(char * zone, time_t t, int v);
    176 static const char *	tformat(void);
    177 static time_t	yeartot(long y) __pure;
    178 
    179 #ifndef TYPECHECK
    180 #define my_localtime	localtime
    181 #else /* !defined TYPECHECK */
    182 static struct tm *
    183 my_localtime(time_t *tp)
    184 {
    185 	struct tm *tmp;
    186 
    187 	tmp = localtime(tp);
    188 	if (tp != NULL && tmp != NULL) {
    189 		struct tm	tm;
    190 		time_t	t;
    191 
    192 		tm = *tmp;
    193 		t = mktime(&tm);
    194 		if (t - *tp >= 1 || *tp - t >= 1) {
    195 			(void) fflush(stdout);
    196 			(void) fprintf(stderr, "\n%s: ", progname);
    197 			(void) fprintf(stderr, tformat(), *tp);
    198 			(void) fprintf(stderr, " ->");
    199 			(void) fprintf(stderr, " year=%d", tmp->tm_year);
    200 			(void) fprintf(stderr, " mon=%d", tmp->tm_mon);
    201 			(void) fprintf(stderr, " mday=%d", tmp->tm_mday);
    202 			(void) fprintf(stderr, " hour=%d", tmp->tm_hour);
    203 			(void) fprintf(stderr, " min=%d", tmp->tm_min);
    204 			(void) fprintf(stderr, " sec=%d", tmp->tm_sec);
    205 			(void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
    206 			(void) fprintf(stderr, " -> ");
    207 			(void) fprintf(stderr, tformat(), t);
    208 			(void) fprintf(stderr, "\n");
    209 		}
    210 	}
    211 	return tmp;
    212 }
    213 #endif /* !defined TYPECHECK */
    214 
    215 static void
    216 abbrok(const char *const abbrp, const char *const zone)
    217 {
    218 	const char *cp;
    219 	const char *wp;
    220 
    221 	if (warned)
    222 		return;
    223 	cp = abbrp;
    224 	wp = NULL;
    225 	while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
    226 		++cp;
    227 	if (cp - abbrp == 0)
    228 		wp = _("lacks alphabetic at start");
    229 	else if (cp - abbrp < 3)
    230 		wp = _("has fewer than 3 alphabetics");
    231 	else if (cp - abbrp > 6)
    232 		wp = _("has more than 6 alphabetics");
    233 	if (wp == NULL && (*cp == '+' || *cp == '-')) {
    234 		++cp;
    235 		if (isascii((unsigned char) *cp) &&
    236 			isdigit((unsigned char) *cp))
    237 				if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
    238 					++cp;
    239 		if (*cp != '\0')
    240 			wp = _("differs from POSIX standard");
    241 	}
    242 	if (wp == NULL)
    243 		return;
    244 	(void) fflush(stdout);
    245 	(void) fprintf(stderr,
    246 		_("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
    247 		progname, zone, abbrp, wp);
    248 	warned = TRUE;
    249 }
    250 
    251 __dead static void
    252 usage(FILE *const stream, const int status)
    253 {
    254 	(void) fprintf(stream,
    255 _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
    256 \n\
    257 Report bugs to %s.\n"),
    258 		       progname, progname, REPORT_BUGS_TO);
    259 	exit(status);
    260 }
    261 
    262 int
    263 main(int argc, char *argv[])
    264 {
    265 	int		i;
    266 	int		c;
    267 	int		vflag;
    268 	char *		cutarg;
    269 	long		cutloyear = ZDUMP_LO_YEAR;
    270 	long		cuthiyear = ZDUMP_HI_YEAR;
    271 	time_t		cutlotime;
    272 	time_t		cuthitime;
    273 	char **		fakeenv;
    274 	time_t		now;
    275 	time_t		t;
    276 	time_t		newt;
    277 	struct tm	tm;
    278 	struct tm	newtm;
    279 	struct tm *	tmp;
    280 	struct tm *	newtmp;
    281 
    282 	INITIALIZE(cutlotime);
    283 	INITIALIZE(cuthitime);
    284 #if HAVE_GETTEXT
    285 	(void) setlocale(LC_ALL, "");
    286 #ifdef TZ_DOMAINDIR
    287 	(void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
    288 #endif /* defined TEXTDOMAINDIR */
    289 	(void) textdomain(TZ_DOMAIN);
    290 #endif /* HAVE_GETTEXT */
    291 	progname = argv[0];
    292 	for (i = 1; i < argc; ++i)
    293 		if (strcmp(argv[i], "--version") == 0) {
    294 			(void) printf("zdump %s%s\n", PKGVERSION, TZVERSION);
    295 			exit(EXIT_SUCCESS);
    296 		} else if (strcmp(argv[i], "--help") == 0) {
    297 			usage(stdout, EXIT_SUCCESS);
    298 		}
    299 	vflag = 0;
    300 	cutarg = NULL;
    301 	while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
    302 		if (c == 'v')
    303 			vflag = 1;
    304 		else	cutarg = optarg;
    305 	if ((c != EOF && c != -1) ||
    306 		(optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
    307 			usage(stderr, EXIT_FAILURE);
    308 	}
    309 	if (vflag) {
    310 		if (cutarg != NULL) {
    311 			long	lo;
    312 			long	hi;
    313 			char	dummy;
    314 
    315 			if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
    316 				cuthiyear = hi;
    317 			} else if (sscanf(cutarg, "%ld,%ld%c",
    318 				&lo, &hi, &dummy) == 2) {
    319 					cutloyear = lo;
    320 					cuthiyear = hi;
    321 			} else {
    322 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
    323 					progname, cutarg);
    324 				exit(EXIT_FAILURE);
    325 			}
    326 		}
    327 		setabsolutes();
    328 		cutlotime = yeartot(cutloyear);
    329 		cuthitime = yeartot(cuthiyear);
    330 	}
    331 	(void) time(&now);
    332 	longest = 0;
    333 	for (i = optind; i < argc; ++i)
    334 		if (strlen(argv[i]) > longest)
    335 			longest = strlen(argv[i]);
    336 	{
    337 		int	from;
    338 		int	to;
    339 
    340 		for (i = 0; environ[i] != NULL; ++i)
    341 			continue;
    342 		fakeenv = malloc((i + 2) * sizeof *fakeenv);
    343 		if (fakeenv == NULL ||
    344 			(fakeenv[0] = malloc(longest + 4)) == NULL) {
    345 			err(EXIT_FAILURE, "Can't allocated %zu bytes",
    346 			    longest + 4);
    347 		}
    348 		to = 0;
    349 		(void)strcpy(fakeenv[to++], "TZ=");	/* XXX strcpy is safe */
    350 		for (from = 0; environ[from] != NULL; ++from)
    351 			if (strncmp(environ[from], "TZ=", 3) != 0)
    352 				fakeenv[to++] = environ[from];
    353 		fakeenv[to] = NULL;
    354 		environ = fakeenv;
    355 	}
    356 	for (i = optind; i < argc; ++i) {
    357 		static char	buf[MAX_STRING_LENGTH];
    358 
    359 		(void) strcpy(&fakeenv[0][3], argv[i]);	/* XXX strcpy is safe */
    360 		if (!vflag) {
    361 			show(argv[i], now, FALSE);
    362 			continue;
    363 		}
    364 		warned = FALSE;
    365 		t = absolute_min_time;
    366 		show(argv[i], t, TRUE);
    367 		t += SECSPERHOUR * HOURSPERDAY;
    368 		show(argv[i], t, TRUE);
    369 		if (t < cutlotime)
    370 			t = cutlotime;
    371 		tmp = my_localtime(&t);
    372 		if (tmp != NULL) {
    373 			tm = *tmp;
    374 			(void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
    375 		}
    376 		for ( ; ; ) {
    377 			if (t >= cuthitime || t >= cuthitime - SECSPERHOUR * 12)
    378 				break;
    379 			newt = t + SECSPERHOUR * 12;
    380 			newtmp = localtime(&newt);
    381 			if (newtmp != NULL)
    382 				newtm = *newtmp;
    383 			if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
    384 				(delta(&newtm, &tm) != (newt - t) ||
    385 				newtm.tm_isdst != tm.tm_isdst ||
    386 				strcmp(abbr(&newtm), buf) != 0)) {
    387 					newt = hunt(argv[i], t, newt);
    388 					newtmp = localtime(&newt);
    389 					if (newtmp != NULL) {
    390 						newtm = *newtmp;
    391 						(void) strncpy(buf,
    392 							abbr(&newtm),
    393 							(sizeof buf) - 1);
    394 					}
    395 			}
    396 			t = newt;
    397 			tm = newtm;
    398 			tmp = newtmp;
    399 		}
    400 		t = absolute_max_time;
    401 		t -= SECSPERHOUR * HOURSPERDAY;
    402 		show(argv[i], t, TRUE);
    403 		t += SECSPERHOUR * HOURSPERDAY;
    404 		show(argv[i], t, TRUE);
    405 	}
    406 	if (fflush(stdout) || ferror(stdout)) {
    407 		err(EXIT_FAILURE, _("Error writing standard output"));
    408 	}
    409 	exit(EXIT_SUCCESS);
    410 	/* If exit fails to exit... */
    411 	return EXIT_FAILURE;
    412 }
    413 
    414 static time_t
    415 ovfl_check(time_t t)
    416 {
    417 	if (t < 0 && t - 1 >= 0)
    418 		return t;
    419 	else
    420 		return t - 1;
    421 }
    422 
    423 static void
    424 setabsolutes(void)
    425 {
    426 	if (0.5 == (time_t) 0.5) {
    427 		/*
    428 		** time_t is floating.
    429 		*/
    430 		if (sizeof (time_t) == sizeof (float)) {
    431 			absolute_min_time = (time_t) -FLT_MAX;
    432 			absolute_max_time = (time_t) FLT_MAX;
    433 		} else if (sizeof (time_t) == sizeof (double)) {
    434 			absolute_min_time = (time_t) -DBL_MAX;
    435 			absolute_max_time = (time_t) DBL_MAX;
    436 		} else {
    437 			(void) fprintf(stderr,
    438 _("%s: use of -v on system with floating time_t other than float or double\n"),
    439 				progname);
    440 			exit(EXIT_FAILURE);
    441 		}
    442 	} else if (0 > (time_t) -1) {
    443 		/*
    444 		** time_t is signed.  Assume overflow wraps around.
    445 		*/
    446 		time_t t = 0;
    447 		time_t t1 = 1;
    448 
    449 		while (t < t1) {
    450 			t = t1;
    451 			t1 = 2 * t1 + 1;
    452 		}
    453 
    454 		absolute_max_time = t;
    455 		t = -t;
    456 		absolute_min_time = ovfl_check(t);
    457 	} else {
    458 		/*
    459 		** time_t is unsigned.
    460 		*/
    461 		absolute_min_time = 0;
    462 		absolute_max_time = absolute_min_time - 1;
    463 	}
    464 }
    465 
    466 static time_t
    467 yeartot(const long y)
    468 {
    469 	long	myy;
    470 	long	seconds;
    471 	time_t	t;
    472 
    473 	myy = EPOCH_YEAR;
    474 	t = 0;
    475 	while (myy != y) {
    476 		if (myy < y) {
    477 			seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
    478 			++myy;
    479 			if (t > absolute_max_time - seconds) {
    480 				t = absolute_max_time;
    481 				break;
    482 			}
    483 			t += seconds;
    484 		} else {
    485 			--myy;
    486 			seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
    487 			if (t < absolute_min_time + seconds) {
    488 				t = absolute_min_time;
    489 				break;
    490 			}
    491 			t -= seconds;
    492 		}
    493 	}
    494 	return t;
    495 }
    496 
    497 static time_t
    498 hunt(char *name, time_t lot, time_t hit)
    499 {
    500 	time_t			t;
    501 	long			diff;
    502 	struct tm		lotm;
    503 	struct tm *	lotmp;
    504 	struct tm		tm;
    505 	struct tm *	tmp;
    506 	char			loab[MAX_STRING_LENGTH];
    507 
    508 	lotmp = my_localtime(&lot);
    509 	if (lotmp != NULL) {
    510 		lotm = *lotmp;
    511 		(void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
    512 	}
    513 	for ( ; ; ) {
    514 		diff = (long) (hit - lot);
    515 		if (diff < 2)
    516 			break;
    517 		t = lot;
    518 		t += diff / 2;
    519 		if (t <= lot)
    520 			++t;
    521 		else if (t >= hit)
    522 			--t;
    523 		tmp = my_localtime(&t);
    524 		if (tmp != NULL)
    525 			tm = *tmp;
    526 		if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
    527 			(delta(&tm, &lotm) == (t - lot) &&
    528 			tm.tm_isdst == lotm.tm_isdst &&
    529 			strcmp(abbr(&tm), loab) == 0)) {
    530 				lot = t;
    531 				lotm = tm;
    532 				lotmp = tmp;
    533 		} else	hit = t;
    534 	}
    535 	show(name, lot, TRUE);
    536 	show(name, hit, TRUE);
    537 	return hit;
    538 }
    539 
    540 /*
    541 ** Thanks to Paul Eggert for logic used in delta.
    542 */
    543 
    544 static long
    545 delta(struct tm *newp, struct tm *oldp)
    546 {
    547 	long	result;
    548 	int	tmy;
    549 
    550 	if (newp->tm_year < oldp->tm_year)
    551 		return -delta(oldp, newp);
    552 	result = 0;
    553 	for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
    554 		result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
    555 	result += newp->tm_yday - oldp->tm_yday;
    556 	result *= HOURSPERDAY;
    557 	result += newp->tm_hour - oldp->tm_hour;
    558 	result *= MINSPERHOUR;
    559 	result += newp->tm_min - oldp->tm_min;
    560 	result *= SECSPERMIN;
    561 	result += newp->tm_sec - oldp->tm_sec;
    562 	return result;
    563 }
    564 
    565 static void
    566 show(char *zone, time_t t, int v)
    567 {
    568 	struct tm *	tmp;
    569 
    570 	(void) printf("%-*s  ", (int) longest, zone);
    571 	if (v) {
    572 		tmp = gmtime(&t);
    573 		if (tmp == NULL) {
    574 			(void) printf(tformat(), t);
    575 		} else {
    576 			dumptime(tmp);
    577 			(void) printf(" UTC");
    578 		}
    579 		(void) printf(" = ");
    580 	}
    581 	tmp = my_localtime(&t);
    582 	dumptime(tmp);
    583 	if (tmp != NULL) {
    584 		if (*abbr(tmp) != '\0')
    585 			(void) printf(" %s", abbr(tmp));
    586 		if (v) {
    587 			(void) printf(" isdst=%d", tmp->tm_isdst);
    588 #ifdef TM_GMTOFF
    589 			(void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
    590 #endif /* defined TM_GMTOFF */
    591 		}
    592 	}
    593 	(void) printf("\n");
    594 	if (tmp != NULL && *abbr(tmp) != '\0')
    595 		abbrok(abbr(tmp), zone);
    596 }
    597 
    598 static const char *
    599 abbr(struct tm *tmp)
    600 {
    601 	const char *	result;
    602 	static const char	nada;
    603 
    604 	if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
    605 		return &nada;
    606 	result = tzname[tmp->tm_isdst];
    607 	return (result == NULL) ? &nada : result;
    608 }
    609 
    610 /*
    611 ** The code below can fail on certain theoretical systems;
    612 ** it works on all known real-world systems as of 2004-12-30.
    613 */
    614 
    615 static const char *
    616 tformat(void)
    617 {
    618 	if (0.5 == (time_t) 0.5) {	/* floating */
    619 		if (sizeof (time_t) > sizeof (double))
    620 			return "%Lg";
    621 		return "%g";
    622 	}
    623 	if (0 > (time_t) -1) {		/* signed */
    624 		if (sizeof (time_t) > sizeof (long))
    625 			return "%lld";
    626 		if (sizeof (time_t) > sizeof (int))
    627 			return "%ld";
    628 		return "%d";
    629 	}
    630 	if (sizeof (time_t) > sizeof (unsigned long))
    631 		return "%llu";
    632 	if (sizeof (time_t) > sizeof (unsigned int))
    633 		return "%lu";
    634 	return "%u";
    635 }
    636 
    637 static void
    638 dumptime(const struct tm *timeptr)
    639 {
    640 	static const char	wday_name[][3] = {
    641 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    642 	};
    643 	static const char	mon_name[][3] = {
    644 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
    645 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    646 	};
    647 	const char *	wn;
    648 	const char *	mn;
    649 	int		lead;
    650 	int		trail;
    651 
    652 	if (timeptr == NULL) {
    653 		(void) printf("NULL");
    654 		return;
    655 	}
    656 	/*
    657 	** The packaged versions of localtime and gmtime never put out-of-range
    658 	** values in tm_wday or tm_mon, but since this code might be compiled
    659 	** with other (perhaps experimental) versions, paranoia is in order.
    660 	*/
    661 	if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
    662 		(int) (sizeof wday_name / sizeof wday_name[0]))
    663 			wn = "???";
    664 	else		wn = wday_name[timeptr->tm_wday];
    665 	if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
    666 		(int) (sizeof mon_name / sizeof mon_name[0]))
    667 			mn = "???";
    668 	else		mn = mon_name[timeptr->tm_mon];
    669 	(void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
    670 		wn, mn,
    671 		timeptr->tm_mday, timeptr->tm_hour,
    672 		timeptr->tm_min, timeptr->tm_sec);
    673 #define DIVISOR	10
    674 	trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
    675 	lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
    676 		trail / DIVISOR;
    677 	trail %= DIVISOR;
    678 	if (trail < 0 && lead > 0) {
    679 		trail += DIVISOR;
    680 		--lead;
    681 	} else if (lead < 0 && trail > 0) {
    682 		trail -= DIVISOR;
    683 		++lead;
    684 	}
    685 	if (lead == 0)
    686 		(void) printf("%d", trail);
    687 	else	(void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));
    688 }
    689