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