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