xref: /netbsd-src/lib/libc/time/asctime.c (revision c41a4eebefede43f6950f838a387dc18c6a431bf)
1 /*	$NetBSD: asctime.c,v 1.6 1997/07/13 20:26:47 christos 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@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.6 1997/07/13 20:26:47 christos 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 /*
25 ** A la X3J11, with core dump avoidance.
26 */
27 
28 char *
29 asctime(timeptr)
30 register const struct tm *	timeptr;
31 {
32 	static const char	wday_name[][3] = {
33 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
34 	};
35 	static const char	mon_name[][3] = {
36 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
37 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
38 	};
39 	/*
40 	** Big enough for something such as
41 	** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
42 	** (two three-character abbreviations, five strings denoting integers,
43 	** three explicit spaces, two explicit colons, a newline,
44 	** and a trailing ASCII nul).
45 	*/
46 	static char		result[3 * 2 + 5 * INT_STRLEN_MAXIMUM(int) +
47 					3 + 2 + 1 + 1];
48 	register const char *	wn;
49 	register const char *	mn;
50 
51 	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
52 		wn = "???";
53 	else	wn = wday_name[timeptr->tm_wday];
54 	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
55 		mn = "???";
56 	else	mn = mon_name[timeptr->tm_mon];
57 	/*
58 	** The X3J11-suggested format is
59 	**	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n"
60 	** Since the .2 in 02.2d is ignored, we drop it.
61 	*/
62 	(void)snprintf(result, sizeof result,"%.3s %.3s%3d %02d:%02d:%02d %d\n",
63 		wn, mn,
64 		timeptr->tm_mday, timeptr->tm_hour,
65 		timeptr->tm_min, timeptr->tm_sec,
66 		TM_YEAR_BASE + timeptr->tm_year);
67 	return result;
68 }
69