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