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