asctime.c revision 1.2.2.2 1 /* $NetBSD: asctime.c,v 1.2.2.2 1995/03/09 23:41:12 jtc Exp $ */
2
3 #ifndef lint
4 #ifndef NOID
5 static char elsieid[] = "@(#)asctime.c 7.6";
6 #endif /* !defined NOID */
7 #endif /* !defined lint */
8
9 /*LINTLIBRARY*/
10
11 #include "private.h"
12 #include "tzfile.h"
13
14 /*
15 ** A la X3J11, with core dump avoidance.
16 */
17
18 char *
19 asctime(timeptr)
20 register const struct tm * timeptr;
21 {
22 static const char wday_name[][3] = {
23 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
24 };
25 static const char mon_name[][3] = {
26 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
27 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
28 };
29 /*
30 ** Big enough for something such as
31 ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
32 ** (two three-character abbreviations, five strings denoting integers,
33 ** three explicit spaces, two explicit colons, a newline,
34 ** and a trailing ASCII nul).
35 */
36 static char result[3 * 2 + 5 * INT_STRLEN_MAXIMUM(int) +
37 3 + 2 + 1 + 1];
38 register const char * wn;
39 register const char * mn;
40
41 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
42 wn = "???";
43 else wn = wday_name[timeptr->tm_wday];
44 if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
45 mn = "???";
46 else mn = mon_name[timeptr->tm_mon];
47 /*
48 ** The X3J11-suggested format is
49 ** "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n"
50 ** Since the .2 in 02.2d is ignored, we drop it.
51 */
52 (void) sprintf(result, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
53 wn, mn,
54 timeptr->tm_mday, timeptr->tm_hour,
55 timeptr->tm_min, timeptr->tm_sec,
56 TM_YEAR_BASE + timeptr->tm_year);
57 return result;
58 }
59