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