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