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