asctime.c revision 111010
117209Swollman/*
217209Swollman** This file is in the public domain, so clarified as of
317209Swollman** June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov).
417209Swollman*/
515923Sscrappy
6111010Snectar#include <sys/cdefs.h>
72708Swollman#ifndef lint
82708Swollman#ifndef NOID
9111010Snectarstatic char	elsieid[] __unused = "@(#)asctime.c	7.7";
102708Swollman#endif /* !defined NOID */
112708Swollman#endif /* !defined lint */
1292986Sobrien__FBSDID("$FreeBSD: head/lib/libc/stdtime/asctime.c 111010 2003-02-16 17:29:11Z nectar $");
132708Swollman
142708Swollman/*LINTLIBRARY*/
152708Swollman
1671579Sdeischen#include "namespace.h"
172708Swollman#include "private.h"
1871579Sdeischen#include "un-namespace.h"
192708Swollman#include "tzfile.h"
202708Swollman
212708Swollman/*
222708Swollman** A la X3J11, with core dump avoidance.
232708Swollman*/
242708Swollman
2535285Sphk
262708Swollmanchar *
272708Swollmanasctime(timeptr)
2835285Sphkconst struct tm *	timeptr;
292708Swollman{
3035285Sphk	static char		result[3 * 2 + 5 * INT_STRLEN_MAXIMUM(int) +
3135285Sphk					3 + 2 + 1 + 1];
3235285Sphk	return(asctime_r(timeptr, result));
3335285Sphk}
3435285Sphk
3535285Sphkchar *
3635285Sphkasctime_r(timeptr, result)
3735285Sphkconst struct tm *	timeptr;
3835285Sphkchar *result;
3935285Sphk{
402708Swollman	static const char	wday_name[][3] = {
412708Swollman		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
422708Swollman	};
432708Swollman	static const char	mon_name[][3] = {
442708Swollman		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
452708Swollman		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
462708Swollman	};
472708Swollman	/*
482708Swollman	** Big enough for something such as
492708Swollman	** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
502708Swollman	** (two three-character abbreviations, five strings denoting integers,
512708Swollman	** three explicit spaces, two explicit colons, a newline,
522708Swollman	** and a trailing ASCII nul).
532708Swollman	*/
5492889Sobrien	const char *	wn;
5592889Sobrien	const char *	mn;
562708Swollman
572708Swollman	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
582708Swollman		wn = "???";
592708Swollman	else	wn = wday_name[timeptr->tm_wday];
602708Swollman	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
612708Swollman		mn = "???";
622708Swollman	else	mn = mon_name[timeptr->tm_mon];
632708Swollman	/*
642708Swollman	** The X3J11-suggested format is
652708Swollman	**	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n"
662708Swollman	** Since the .2 in 02.2d is ignored, we drop it.
672708Swollman	*/
682708Swollman	(void) sprintf(result, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
692708Swollman		wn, mn,
702708Swollman		timeptr->tm_mday, timeptr->tm_hour,
712708Swollman		timeptr->tm_min, timeptr->tm_sec,
722708Swollman		TM_YEAR_BASE + timeptr->tm_year);
732708Swollman	return result;
742708Swollman}
75