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