]> git.saurik.com Git - apple/libc.git/blob - stdtime/asctime-fbsd.c
Libc-583.tar.gz
[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 #define EXPECTEDLEN 26
26
27 char *
28 asctime_r(const struct tm * __restrict timeptr, char * __restrict buf)
29 {
30 static const char wday_name[][3] = {
31 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
32 };
33 static const char mon_name[][3] = {
34 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
35 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
36 };
37 const char * wn;
38 const char * mn;
39 int len;
40 char tmp[EXPECTEDLEN];
41
42 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
43 wn = "???";
44 else wn = wday_name[timeptr->tm_wday];
45 if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
46 mn = "???";
47 else mn = mon_name[timeptr->tm_mon];
48 /*
49 ** The X3J11-suggested format is
50 ** "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n"
51 ** Since the .2 in 02.2d is ignored, we drop it.
52 */
53 /*
54 ** Because various values in the tm structure may cause the
55 ** resulting string to be longer than the 26-bytes that is
56 ** specified in the spec, we should return NULL rather than
57 ** possibly overwrite beyond the string.
58 */
59 len = snprintf(tmp, EXPECTEDLEN, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
60 wn, mn,
61 timeptr->tm_mday, timeptr->tm_hour,
62 timeptr->tm_min, timeptr->tm_sec,
63 TM_YEAR_BASE + timeptr->tm_year);
64 if (len >= EXPECTEDLEN)
65 return NULL;
66 strcpy(buf, tmp);
67 return buf;
68 }
69
70 char *
71 asctime(timeptr)
72 const struct tm * timeptr;
73 {
74 static char result[EXPECTEDLEN];
75
76 return asctime_r(timeptr, result);
77 }