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