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