asctime.c revision 1.7 1 1.7 kleink /* $NetBSD: asctime.c,v 1.7 1998/09/10 15:58:39 kleink Exp $ */
2 1.3 jtc
3 1.3 jtc /*
4 1.3 jtc ** This file is in the public domain, so clarified as of
5 1.5 jtc ** 1996-06-05 by Arthur David Olson (arthur_david_olson (at) nih.gov).
6 1.3 jtc */
7 1.2 jtc
8 1.6 christos #include <sys/cdefs.h>
9 1.1 jtc #ifndef lint
10 1.1 jtc #ifndef NOID
11 1.6 christos #if 0
12 1.5 jtc static char elsieid[] = "@(#)asctime.c 7.8";
13 1.6 christos #else
14 1.7 kleink __RCSID("$NetBSD: asctime.c,v 1.7 1998/09/10 15:58:39 kleink Exp $");
15 1.6 christos #endif
16 1.1 jtc #endif /* !defined NOID */
17 1.1 jtc #endif /* !defined lint */
18 1.1 jtc
19 1.1 jtc /*LINTLIBRARY*/
20 1.1 jtc
21 1.1 jtc #include "private.h"
22 1.1 jtc #include "tzfile.h"
23 1.1 jtc
24 1.7 kleink #ifdef __weak_alias
25 1.7 kleink __weak_alias(asctime_r,_asctime_r);
26 1.7 kleink #endif
27 1.7 kleink
28 1.7 kleink /*
29 1.7 kleink ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, Second Edition, 1996-07-12.
30 1.7 kleink */
31 1.7 kleink
32 1.1 jtc /*
33 1.7 kleink ** Big enough for something such as
34 1.7 kleink ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
35 1.7 kleink ** (two three-character abbreviations, five strings denoting integers,
36 1.7 kleink ** three explicit spaces, two explicit colons, a newline,
37 1.7 kleink ** and a trailing ASCII nul).
38 1.1 jtc */
39 1.7 kleink #define ASCTIME_BUFLEN (3 * 2 + 5 * INT_STRLEN_MAXIMUM(int) + 3 + 2 + 1 + 1)
40 1.1 jtc
41 1.1 jtc char *
42 1.7 kleink asctime_r(timeptr, buf)
43 1.1 jtc register const struct tm * timeptr;
44 1.7 kleink char * buf;
45 1.1 jtc {
46 1.1 jtc static const char wday_name[][3] = {
47 1.1 jtc "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
48 1.1 jtc };
49 1.1 jtc static const char mon_name[][3] = {
50 1.1 jtc "Jan", "Feb", "Mar", "Apr", "May", "Jun",
51 1.1 jtc "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
52 1.1 jtc };
53 1.1 jtc register const char * wn;
54 1.1 jtc register const char * mn;
55 1.1 jtc
56 1.1 jtc if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
57 1.1 jtc wn = "???";
58 1.1 jtc else wn = wday_name[timeptr->tm_wday];
59 1.1 jtc if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
60 1.1 jtc mn = "???";
61 1.1 jtc else mn = mon_name[timeptr->tm_mon];
62 1.1 jtc /*
63 1.1 jtc ** The X3J11-suggested format is
64 1.1 jtc ** "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n"
65 1.1 jtc ** Since the .2 in 02.2d is ignored, we drop it.
66 1.1 jtc */
67 1.7 kleink (void)snprintf(buf,
68 1.7 kleink sizeof (char[ASCTIME_BUFLEN]),
69 1.7 kleink "%.3s %.3s%3d %02d:%02d:%02d %d\n",
70 1.1 jtc wn, mn,
71 1.1 jtc timeptr->tm_mday, timeptr->tm_hour,
72 1.1 jtc timeptr->tm_min, timeptr->tm_sec,
73 1.1 jtc TM_YEAR_BASE + timeptr->tm_year);
74 1.7 kleink return buf;
75 1.7 kleink }
76 1.7 kleink
77 1.7 kleink /*
78 1.7 kleink ** A la X3J11, with core dump avoidance.
79 1.7 kleink */
80 1.7 kleink
81 1.7 kleink char *
82 1.7 kleink asctime(timeptr)
83 1.7 kleink register const struct tm * timeptr;
84 1.7 kleink {
85 1.7 kleink static char result[ASCTIME_BUFLEN];
86 1.7 kleink
87 1.7 kleink return asctime_r(timeptr, result);
88 1.1 jtc }
89