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