xref: /minix3/lib/libc/time/asctime.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: asctime.c,v 1.20 2014/10/23 18:45:58 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras ** This file is in the public domain, so clarified as of
52fe8fb19SBen Gras ** 1996-06-05 by Arthur David Olson.
62fe8fb19SBen Gras */
72fe8fb19SBen Gras 
82fe8fb19SBen Gras /*
92fe8fb19SBen Gras ** Avoid the temptation to punt entirely to strftime;
102fe8fb19SBen Gras ** the output of strftime is supposed to be locale specific
112fe8fb19SBen Gras ** whereas the output of asctime is supposed to be constant.
122fe8fb19SBen Gras */
132fe8fb19SBen Gras 
142fe8fb19SBen Gras #include <sys/cdefs.h>
152fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
162fe8fb19SBen Gras #if 0
17f14fb602SLionel Sambuc static char	elsieid[] = "@(#)asctime.c	8.5";
182fe8fb19SBen Gras #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: asctime.c,v 1.20 2014/10/23 18:45:58 christos Exp $");
202fe8fb19SBen Gras #endif
212fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
222fe8fb19SBen Gras 
232fe8fb19SBen Gras /*LINTLIBRARY*/
242fe8fb19SBen Gras 
252fe8fb19SBen Gras #include "namespace.h"
262fe8fb19SBen Gras #include "private.h"
272fe8fb19SBen Gras #include "tzfile.h"
282fe8fb19SBen Gras 
292fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(asctime_r,_asctime_r)302fe8fb19SBen Gras __weak_alias(asctime_r,_asctime_r)
312fe8fb19SBen Gras #endif
322fe8fb19SBen Gras 
332fe8fb19SBen Gras /*
342fe8fb19SBen Gras ** Some systems only handle "%.2d"; others only handle "%02d";
352fe8fb19SBen Gras ** "%02.2d" makes (most) everybody happy.
362fe8fb19SBen Gras ** At least some versions of gcc warn about the %02.2d;
372fe8fb19SBen Gras ** we conditionalize below to avoid the warning.
382fe8fb19SBen Gras */
392fe8fb19SBen Gras /*
402fe8fb19SBen Gras ** All years associated with 32-bit time_t values are exactly four digits long;
412fe8fb19SBen Gras ** some years associated with 64-bit time_t values are not.
422fe8fb19SBen Gras ** Vintage programs are coded for years that are always four digits long
432fe8fb19SBen Gras ** and may assume that the newline always lands in the same place.
442fe8fb19SBen Gras ** For years that are less than four digits, we pad the output with
452fe8fb19SBen Gras ** leading zeroes to get the newline in the traditional place.
462fe8fb19SBen Gras ** The -4 ensures that we get four characters of output even if
472fe8fb19SBen Gras ** we call a strftime variant that produces fewer characters for some years.
482fe8fb19SBen Gras ** The ISO C 1999 and POSIX 1003.1-2004 standards prohibit padding the year,
492fe8fb19SBen Gras ** but many implementations pad anyway; most likely the standards are buggy.
502fe8fb19SBen Gras */
512fe8fb19SBen Gras #ifdef __GNUC__
522fe8fb19SBen Gras #define ASCTIME_FMT	"%.3s %.3s%3d %2.2d:%2.2d:%2.2d %-4s\n"
532fe8fb19SBen Gras #else /* !defined __GNUC__ */
542fe8fb19SBen Gras #define ASCTIME_FMT	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d %-4s\n"
552fe8fb19SBen Gras #endif /* !defined __GNUC__ */
562fe8fb19SBen Gras /*
572fe8fb19SBen Gras ** For years that are more than four digits we put extra spaces before the year
582fe8fb19SBen Gras ** so that code trying to overwrite the newline won't end up overwriting
592fe8fb19SBen Gras ** a digit within a year and truncating the year (operating on the assumption
602fe8fb19SBen Gras ** that no output is better than wrong output).
612fe8fb19SBen Gras */
622fe8fb19SBen Gras #ifdef __GNUC__
632fe8fb19SBen Gras #define ASCTIME_FMT_B	"%.3s %.3s%3d %2.2d:%2.2d:%2.2d     %s\n"
642fe8fb19SBen Gras #else /* !defined __GNUC__ */
652fe8fb19SBen Gras #define ASCTIME_FMT_B	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d     %s\n"
662fe8fb19SBen Gras #endif /* !defined __GNUC__ */
672fe8fb19SBen Gras 
682fe8fb19SBen Gras #define STD_ASCTIME_BUF_SIZE	26
692fe8fb19SBen Gras /*
702fe8fb19SBen Gras ** Big enough for something such as
712fe8fb19SBen Gras ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648     -2147483648\n
722fe8fb19SBen Gras ** (two three-character abbreviations, five strings denoting integers,
732fe8fb19SBen Gras ** seven explicit spaces, two explicit colons, a newline,
74*0a6a1f1dSLionel Sambuc ** and a trailing NUL byte).
752fe8fb19SBen Gras ** The values above are for systems where an int is 32 bits and are provided
762fe8fb19SBen Gras ** as an example; the define below calculates the maximum for the system at
772fe8fb19SBen Gras ** hand.
782fe8fb19SBen Gras */
792fe8fb19SBen Gras #define MAX_ASCTIME_BUF_SIZE	(2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1)
802fe8fb19SBen Gras 
812fe8fb19SBen Gras static char	buf_asctime[MAX_ASCTIME_BUF_SIZE];
822fe8fb19SBen Gras 
832fe8fb19SBen Gras /*
842fe8fb19SBen Gras ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
852fe8fb19SBen Gras */
862fe8fb19SBen Gras 
872fe8fb19SBen Gras char *
88f14fb602SLionel Sambuc asctime_r(const struct tm *timeptr, char *buf)
892fe8fb19SBen Gras {
9084d9c625SLionel Sambuc 	static const char	wday_name[][3] = {
912fe8fb19SBen Gras 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
922fe8fb19SBen Gras 	};
9384d9c625SLionel Sambuc 	static const char	mon_name[][3] = {
942fe8fb19SBen Gras 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
952fe8fb19SBen Gras 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
962fe8fb19SBen Gras 	};
9784d9c625SLionel Sambuc 	const char *	wn;
9884d9c625SLionel Sambuc 	const char *	mn;
992fe8fb19SBen Gras 	char			year[INT_STRLEN_MAXIMUM(int) + 2];
1002fe8fb19SBen Gras 	char			result[MAX_ASCTIME_BUF_SIZE];
1012fe8fb19SBen Gras 
102f14fb602SLionel Sambuc 	if (timeptr == NULL) {
103f14fb602SLionel Sambuc 		errno = EINVAL;
104f14fb602SLionel Sambuc 		return strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
105f14fb602SLionel Sambuc 	}
1062fe8fb19SBen Gras 	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
1072fe8fb19SBen Gras 		wn = "???";
1082fe8fb19SBen Gras 	else	wn = wday_name[timeptr->tm_wday];
1092fe8fb19SBen Gras 	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
1102fe8fb19SBen Gras 		mn = "???";
1112fe8fb19SBen Gras 	else	mn = mon_name[timeptr->tm_mon];
1122fe8fb19SBen Gras 	/*
1132fe8fb19SBen Gras 	** Use strftime's %Y to generate the year, to avoid overflow problems
1142fe8fb19SBen Gras 	** when computing timeptr->tm_year + TM_YEAR_BASE.
1152fe8fb19SBen Gras 	** Assume that strftime is unaffected by other out-of-range members
1162fe8fb19SBen Gras 	** (e.g., timeptr->tm_mday) when processing "%Y".
1172fe8fb19SBen Gras 	*/
1182fe8fb19SBen Gras 	(void) strftime(year, sizeof year, "%Y", timeptr);
1192fe8fb19SBen Gras 	(void) snprintf(result,
1202fe8fb19SBen Gras 		sizeof(result),
1212fe8fb19SBen Gras 		((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
1222fe8fb19SBen Gras 		wn, mn,
1232fe8fb19SBen Gras 		timeptr->tm_mday, timeptr->tm_hour,
1242fe8fb19SBen Gras 		timeptr->tm_min, timeptr->tm_sec,
1252fe8fb19SBen Gras 		year);
126f14fb602SLionel Sambuc 	if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime)
127f14fb602SLionel Sambuc 		return strcpy(buf, result);
128f14fb602SLionel Sambuc 	else {
1292fe8fb19SBen Gras 		errno = EOVERFLOW;
1302fe8fb19SBen Gras 		return NULL;
1312fe8fb19SBen Gras 	}
1322fe8fb19SBen Gras }
1332fe8fb19SBen Gras 
1342fe8fb19SBen Gras /*
1352fe8fb19SBen Gras ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
1362fe8fb19SBen Gras */
1372fe8fb19SBen Gras 
1382fe8fb19SBen Gras char *
asctime(const struct tm * timeptr)139f14fb602SLionel Sambuc asctime(const struct tm *timeptr)
1402fe8fb19SBen Gras {
1412fe8fb19SBen Gras 	return asctime_r(timeptr, buf_asctime);
1422fe8fb19SBen Gras }
143