asctime.c revision 111010
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
6#include <sys/cdefs.h>
7#ifndef lint
8#ifndef NOID
9static char	elsieid[] __unused = "@(#)asctime.c	7.7";
10#endif /* !defined NOID */
11#endif /* !defined lint */
12__FBSDID("$FreeBSD: head/lib/libc/stdtime/asctime.c 111010 2003-02-16 17:29:11Z nectar $");
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 X3J11, with core dump avoidance.
23*/
24
25
26char *
27asctime(timeptr)
28const struct tm *	timeptr;
29{
30	static char		result[3 * 2 + 5 * INT_STRLEN_MAXIMUM(int) +
31					3 + 2 + 1 + 1];
32	return(asctime_r(timeptr, result));
33}
34
35char *
36asctime_r(timeptr, result)
37const struct tm *	timeptr;
38char *result;
39{
40	static const char	wday_name[][3] = {
41		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
42	};
43	static const char	mon_name[][3] = {
44		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
45		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
46	};
47	/*
48	** Big enough for something such as
49	** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
50	** (two three-character abbreviations, five strings denoting integers,
51	** three explicit spaces, two explicit colons, a newline,
52	** and a trailing ASCII nul).
53	*/
54	const char *	wn;
55	const char *	mn;
56
57	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
58		wn = "???";
59	else	wn = wday_name[timeptr->tm_wday];
60	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
61		mn = "???";
62	else	mn = mon_name[timeptr->tm_mon];
63	/*
64	** The X3J11-suggested format is
65	**	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n"
66	** Since the .2 in 02.2d is ignored, we drop it.
67	*/
68	(void) sprintf(result, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
69		wn, mn,
70		timeptr->tm_mday, timeptr->tm_hour,
71		timeptr->tm_min, timeptr->tm_sec,
72		TM_YEAR_BASE + timeptr->tm_year);
73	return result;
74}
75