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