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