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