Home | History | Annotate | Line # | Download | only in time
asctime.c revision 1.6
      1  1.6  christos /*	$NetBSD: asctime.c,v 1.6 1997/07/13 20:26:47 christos Exp $	*/
      2  1.3       jtc 
      3  1.3       jtc /*
      4  1.3       jtc ** This file is in the public domain, so clarified as of
      5  1.5       jtc ** 1996-06-05 by Arthur David Olson (arthur_david_olson (at) nih.gov).
      6  1.3       jtc */
      7  1.2       jtc 
      8  1.6  christos #include <sys/cdefs.h>
      9  1.1       jtc #ifndef lint
     10  1.1       jtc #ifndef NOID
     11  1.6  christos #if 0
     12  1.5       jtc static char	elsieid[] = "@(#)asctime.c	7.8";
     13  1.6  christos #else
     14  1.6  christos __RCSID("$NetBSD: asctime.c,v 1.6 1997/07/13 20:26:47 christos Exp $");
     15  1.6  christos #endif
     16  1.1       jtc #endif /* !defined NOID */
     17  1.1       jtc #endif /* !defined lint */
     18  1.1       jtc 
     19  1.1       jtc /*LINTLIBRARY*/
     20  1.1       jtc 
     21  1.1       jtc #include "private.h"
     22  1.1       jtc #include "tzfile.h"
     23  1.1       jtc 
     24  1.1       jtc /*
     25  1.1       jtc ** A la X3J11, with core dump avoidance.
     26  1.1       jtc */
     27  1.1       jtc 
     28  1.1       jtc char *
     29  1.1       jtc asctime(timeptr)
     30  1.1       jtc register const struct tm *	timeptr;
     31  1.1       jtc {
     32  1.1       jtc 	static const char	wday_name[][3] = {
     33  1.1       jtc 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
     34  1.1       jtc 	};
     35  1.1       jtc 	static const char	mon_name[][3] = {
     36  1.1       jtc 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
     37  1.1       jtc 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
     38  1.1       jtc 	};
     39  1.1       jtc 	/*
     40  1.1       jtc 	** Big enough for something such as
     41  1.1       jtc 	** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
     42  1.1       jtc 	** (two three-character abbreviations, five strings denoting integers,
     43  1.1       jtc 	** three explicit spaces, two explicit colons, a newline,
     44  1.1       jtc 	** and a trailing ASCII nul).
     45  1.1       jtc 	*/
     46  1.1       jtc 	static char		result[3 * 2 + 5 * INT_STRLEN_MAXIMUM(int) +
     47  1.1       jtc 					3 + 2 + 1 + 1];
     48  1.1       jtc 	register const char *	wn;
     49  1.1       jtc 	register const char *	mn;
     50  1.1       jtc 
     51  1.1       jtc 	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
     52  1.1       jtc 		wn = "???";
     53  1.1       jtc 	else	wn = wday_name[timeptr->tm_wday];
     54  1.1       jtc 	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
     55  1.1       jtc 		mn = "???";
     56  1.1       jtc 	else	mn = mon_name[timeptr->tm_mon];
     57  1.1       jtc 	/*
     58  1.1       jtc 	** The X3J11-suggested format is
     59  1.1       jtc 	**	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n"
     60  1.1       jtc 	** Since the .2 in 02.2d is ignored, we drop it.
     61  1.1       jtc 	*/
     62  1.4       mrg 	(void)snprintf(result, sizeof result,"%.3s %.3s%3d %02d:%02d:%02d %d\n",
     63  1.1       jtc 		wn, mn,
     64  1.1       jtc 		timeptr->tm_mday, timeptr->tm_hour,
     65  1.1       jtc 		timeptr->tm_min, timeptr->tm_sec,
     66  1.1       jtc 		TM_YEAR_BASE + timeptr->tm_year);
     67  1.1       jtc 	return result;
     68  1.1       jtc }
     69