xref: /openbsd-src/lib/libc/time/asctime.c (revision 9f7cd00b0bfc857cfd3a2ee9b3393d0394c768d7)
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson (arthur_david_olson@nih.gov).
4 */
5 
6 #if defined(LIBC_SCCS) && !defined(lint) && !defined(NOID)
7 static char elsieid[] = "@(#)asctime.c	7.9";
8 static char rcsid[] = "$OpenBSD: asctime.c,v 1.7 2000/01/06 08:24:17 d Exp $";
9 #endif /* LIBC_SCCS and not lint */
10 
11 /*LINTLIBRARY*/
12 
13 #include "private.h"
14 #include "tzfile.h"
15 #include "thread_private.h"
16 
17 /*
18 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, Second Edition, 1996-07-12.
19 */
20 
21 char *
22 asctime_r(timeptr, buf)
23 register const struct tm *	timeptr;
24 char *				buf;
25 {
26 	static const char	wday_name[][3] = {
27 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
28 	};
29 	static const char	mon_name[][3] = {
30 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
31 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
32 	};
33 	register const char *	wn;
34 	register const char *	mn;
35 	int size;
36 
37 	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
38 		wn = "???";
39 	else	wn = wday_name[timeptr->tm_wday];
40 	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
41 		mn = "???";
42 	else	mn = mon_name[timeptr->tm_mon];
43 	/*
44 	** The X3J11-suggested format is
45 	**	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n"
46 	** Since the .2 in 02.2d is ignored, we drop it.
47 	*/
48 	/*
49 	 * P1003 8.3.5.2 says that asctime_r() can only assume at most
50 	 * a 26 byte buffer.  *XXX*
51 	 */
52 	size = snprintf(buf, 26, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
53 		wn, mn,
54 		timeptr->tm_mday, timeptr->tm_hour,
55 		timeptr->tm_min, timeptr->tm_sec,
56 		TM_YEAR_BASE + timeptr->tm_year);
57 	if (size >= 26)
58 		return NULL;
59 	return buf;
60 }
61 
62 /*
63 ** A la X3J11, with core dump avoidance.
64 */
65 
66 char *
67 asctime(timeptr)
68 const struct tm *	timeptr;
69 {
70 	/* asctime_r won't exceed this buffer: */
71 	static char result[26];
72 	_THREAD_PRIVATE_KEY(asctime);
73 	char *resultp = (char*) _THREAD_PRIVATE(asctime, result, NULL);
74 
75 	if (resultp == NULL)
76 		return NULL;
77 	else
78 		return asctime_r(timeptr, resultp);
79 }
80