xref: /dragonfly/lib/libc/stdtime/asctime.c (revision 5153f92b)
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov).
4 **
5 ** $FreeBSD: src/lib/libc/stdtime/asctime.c,v 1.7.6.1 2001/03/05 11:37:20 obrien Exp $
6 ** $DragonFly: src/lib/libc/stdtime/asctime.c,v 1.3 2004/10/25 19:38:02 drhodus Exp $
7 */
8 
9 /*
10  * @(#)asctime.c	7.7
11  */
12 /*LINTLIBRARY*/
13 
14 #include "private.h"
15 #include "tzfile.h"
16 
17 /*
18 ** A la X3J11, with core dump avoidance.
19 */
20 
21 
22 char *
23 asctime(timeptr)
24 const struct tm *	timeptr;
25 {
26 	static char		result[3 * 2 + 5 * INT_STRLEN_MAXIMUM(int) +
27 					3 + 2 + 1 + 1];
28 	return(asctime_r(timeptr, result));
29 }
30 
31 char *
32 asctime_r(timeptr, result)
33 const struct tm *	timeptr;
34 char *result;
35 {
36 	static const char	wday_name[][3] = {
37 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
38 	};
39 	static const char	mon_name[][3] = {
40 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
41 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
42 	};
43 	/*
44 	** Big enough for something such as
45 	** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
46 	** (two three-character abbreviations, five strings denoting integers,
47 	** three explicit spaces, two explicit colons, a newline,
48 	** and a trailing ASCII nul).
49 	*/
50 	const char *	wn;
51 	const char *	mn;
52 
53 	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
54 		wn = "???";
55 	else	wn = wday_name[timeptr->tm_wday];
56 	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
57 		mn = "???";
58 	else	mn = mon_name[timeptr->tm_mon];
59 	/*
60 	** The X3J11-suggested format is
61 	**	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n"
62 	** Since the .2 in 02.2d is ignored, we drop it.
63 	*/
64 	(void) sprintf(result, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
65 		wn, mn,
66 		timeptr->tm_mday, timeptr->tm_hour,
67 		timeptr->tm_min, timeptr->tm_sec,
68 		TM_YEAR_BASE + timeptr->tm_year);
69 	return result;
70 }
71