xref: /openbsd/lib/libc/time/asctime.c (revision 3d8817e4)
1 /*	$OpenBSD: asctime.c,v 1.16 2010/08/23 22:35:34 millert Exp $ */
2 /*
3 ** This file is in the public domain, so clarified as of
4 ** 1996-06-05 by Arthur David Olson.
5 */
6 
7 /*
8 ** Avoid the temptation to punt entirely to strftime;
9 ** the output of strftime is supposed to be locale specific
10 ** whereas the output of asctime is supposed to be constant.
11 */
12 
13 /*LINTLIBRARY*/
14 
15 #include "private.h"
16 #include "tzfile.h"
17 #include "thread_private.h"
18 
19 /*
20 ** Some systems only handle "%.2d"; others only handle "%02d";
21 ** "%02.2d" makes (most) everybody happy.
22 ** At least some versions of gcc warn about the %02.2d;
23 ** we conditionalize below to avoid the warning.
24 */
25 /*
26 ** All years associated with 32-bit time_t values are exactly four digits long;
27 ** some years associated with 64-bit time_t values are not.
28 ** Vintage programs are coded for years that are always four digits long
29 ** and may assume that the newline always lands in the same place.
30 ** For years that are less than four digits, we pad the output with
31 ** leading zeroes to get the newline in the traditional place.
32 ** The -4 ensures that we get four characters of output even if
33 ** we call a strftime variant that produces fewer characters for some years.
34 ** The ISO C 1999 and POSIX 1003.1-2004 standards prohibit padding the year,
35 ** but many implementations pad anyway; most likely the standards are buggy.
36 */
37 #ifdef __GNUC__
38 #define ASCTIME_FMT	"%.3s %.3s%3d %2.2d:%2.2d:%2.2d %-4s\n"
39 #else /* !defined __GNUC__ */
40 #define ASCTIME_FMT	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d %-4s\n"
41 #endif /* !defined __GNUC__ */
42 /*
43 ** For years that are more than four digits we put extra spaces before the year
44 ** so that code trying to overwrite the newline won't end up overwriting
45 ** a digit within a year and truncating the year (operating on the assumption
46 ** that no output is better than wrong output).
47 */
48 #ifdef __GNUC__
49 #define ASCTIME_FMT_B	"%.3s %.3s%3d %2.2d:%2.2d:%2.2d     %s\n"
50 #else /* !defined __GNUC__ */
51 #define ASCTIME_FMT_B	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d     %s\n"
52 #endif /* !defined __GNUC__ */
53 
54 #define STD_ASCTIME_BUF_SIZE	26
55 /*
56 ** Big enough for something such as
57 ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648     -2147483648\n
58 ** (two three-character abbreviations, five strings denoting integers,
59 ** seven explicit spaces, two explicit colons, a newline,
60 ** and a trailing ASCII nul).
61 ** The values above are for systems where an int is 32 bits and are provided
62 ** as an example; the define below calculates the maximum for the system at
63 ** hand.
64 */
65 #define MAX_ASCTIME_BUF_SIZE	(2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1)
66 
67 static char *
68 asctime3(timeptr, buf, bufsize)
69 register const struct tm *	timeptr;
70 char *				buf;
71 int				bufsize;
72 {
73 	static const char	wday_name[][4] = {
74 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
75 	};
76 	static const char	mon_name[][4] = {
77 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
78 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
79 	};
80 	register const char *	wn;
81 	register const char *	mn;
82 	char			year[INT_STRLEN_MAXIMUM(int) + 2];
83 	int			len;
84 
85 	if (timeptr == NULL) {
86 		errno = EINVAL;
87 		strlcpy(buf, "??? ??? ?? ??:??:?? ????\n", bufsize);
88 		return buf;
89 	}
90 	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
91 		wn = "???";
92 	else	wn = wday_name[timeptr->tm_wday];
93 	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
94 		mn = "???";
95 	else	mn = mon_name[timeptr->tm_mon];
96 	/*
97 	** Use strftime's %Y to generate the year, to avoid overflow problems
98 	** when computing timeptr->tm_year + TM_YEAR_BASE.
99 	** Assume that strftime is unaffected by other out-of-range members
100 	** (e.g., timeptr->tm_mday) when processing "%Y".
101 	*/
102 	(void) strftime(year, sizeof year, "%Y", timeptr);
103 	len = snprintf(buf, bufsize,
104 		((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
105 		wn, mn,
106 		timeptr->tm_mday, timeptr->tm_hour,
107 		timeptr->tm_min, timeptr->tm_sec,
108 		year);
109 	if (len != -1 && len < bufsize) {
110 		return buf;
111 	} else {
112 #ifdef EOVERFLOW
113 		errno = EOVERFLOW;
114 #else /* !defined EOVERFLOW */
115 		errno = EINVAL;
116 #endif /* !defined EOVERFLOW */
117 		return NULL;
118 	}
119 }
120 
121 /*
122 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
123 */
124 
125 char *
126 asctime_r(timeptr, buf)
127 register const struct tm *	timeptr;
128 char *				buf;
129 {
130 	/*
131 	** P1003 8.3.5.2 says that asctime_r() can only assume at most
132 	** a 26 byte buffer.
133 	*/
134 	return asctime3(timeptr, buf, STD_ASCTIME_BUF_SIZE);
135 }
136 
137 /*
138 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
139 */
140 
141 char *
142 asctime(timeptr)
143 const struct tm *	timeptr;
144 {
145 	static char result[MAX_ASCTIME_BUF_SIZE];
146 	_THREAD_PRIVATE_KEY(asctime);
147 	char *resultp = (char *)_THREAD_PRIVATE(asctime, result, NULL);
148 
149 	if (resultp == NULL)
150 		return NULL;
151 	else
152 		return asctime3(timeptr, resultp, sizeof(result));
153 }
154