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