asctime.c revision 35436
11556Srgrimes/*
21556Srgrimes** This file is in the public domain, so clarified as of
31556Srgrimes** June 5, 1996 by Arthur David Olson (arthur_david_olson@nih.gov).
41556Srgrimes*/
51556Srgrimes
61556Srgrimes#ifndef lint
71556Srgrimes#ifndef NOID
81556Srgrimesstatic char	elsieid[] = "@(#)asctime.c	7.7";
91556Srgrimes#endif /* !defined NOID */
101556Srgrimes#endif /* !defined lint */
111556Srgrimes
121556Srgrimes/*LINTLIBRARY*/
131556Srgrimes
141556Srgrimes#include "private.h"
151556Srgrimes#include "tzfile.h"
161556Srgrimes
171556Srgrimes#ifndef _THREAD_SAFE
181556Srgrimesstatic char *asctime_r __P((const struct tm *, char *));
191556Srgrimes#endif
201556Srgrimes
211556Srgrimes/*
221556Srgrimes** A la X3J11, with core dump avoidance.
231556Srgrimes*/
241556Srgrimes
251556Srgrimes
261556Srgrimeschar *
271556Srgrimesasctime(timeptr)
281556Srgrimesconst struct tm *	timeptr;
291556Srgrimes{
301556Srgrimes	static char		result[3 * 2 + 5 * INT_STRLEN_MAXIMUM(int) +
311556Srgrimes					3 + 2 + 1 + 1];
321556Srgrimes	return(asctime_r(timeptr, result));
331556Srgrimes}
341556Srgrimes
3536152Scharnier#ifndef _THREAD_SAFE
3636152Scharnierstatic
3736152Scharnier#endif
3836152Scharnierchar *
3937230Sbdeasctime_r(timeptr, result)
401556Srgrimesconst struct tm *	timeptr;
411556Srgrimeschar *result;
421556Srgrimes{
431556Srgrimes	static const char	wday_name[][3] = {
441556Srgrimes		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
451556Srgrimes	};
461556Srgrimes	static const char	mon_name[][3] = {
471556Srgrimes		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
481556Srgrimes		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
491556Srgrimes	};
501556Srgrimes	/*
511556Srgrimes	** Big enough for something such as
521556Srgrimes	** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
531556Srgrimes	** (two three-character abbreviations, five strings denoting integers,
541556Srgrimes	** three explicit spaces, two explicit colons, a newline,
551556Srgrimes	** and a trailing ASCII nul).
561556Srgrimes	*/
571556Srgrimes	register const char *	wn;
581556Srgrimes	register const char *	mn;
591556Srgrimes
601556Srgrimes	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
611556Srgrimes		wn = "???";
621556Srgrimes	else	wn = wday_name[timeptr->tm_wday];
631556Srgrimes	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
641556Srgrimes		mn = "???";
651556Srgrimes	else	mn = mon_name[timeptr->tm_mon];
661556Srgrimes	/*
671556Srgrimes	** The X3J11-suggested format is
681556Srgrimes	**	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n"
697165Sjoerg	** Since the .2 in 02.2d is ignored, we drop it.
7037230Sbde	*/
7137230Sbde	(void) sprintf(result, "%.3s %.3s%3d %02d:%02d:%02d %d\n",
721556Srgrimes		wn, mn,
731556Srgrimes		timeptr->tm_mday, timeptr->tm_hour,
7437230Sbde		timeptr->tm_min, timeptr->tm_sec,
7537230Sbde		TM_YEAR_BASE + timeptr->tm_year);
761556Srgrimes	return result;
771556Srgrimes}
781556Srgrimes