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