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