Home | History | Annotate | Line # | Download | only in time
asctime.c revision 1.23
      1 /*	$NetBSD: asctime.c,v 1.23 2018/01/05 20:01:31 kamil Exp $	*/
      2 
      3 /*
      4 ** This file is in the public domain, so clarified as of
      5 ** 1996-06-05 by Arthur David Olson.
      6 */
      7 
      8 /*
      9 ** Avoid the temptation to punt entirely to strftime;
     10 ** the output of strftime is supposed to be locale specific
     11 ** whereas the output of asctime is supposed to be constant.
     12 */
     13 
     14 #include <sys/cdefs.h>
     15 #if defined(LIBC_SCCS) && !defined(lint)
     16 #if 0
     17 static char	elsieid[] = "@(#)asctime.c	8.5";
     18 #else
     19 __RCSID("$NetBSD: asctime.c,v 1.23 2018/01/05 20:01:31 kamil Exp $");
     20 #endif
     21 #endif /* LIBC_SCCS and not lint */
     22 
     23 /*LINTLIBRARY*/
     24 
     25 #include "namespace.h"
     26 #include "private.h"
     27 #include <stdio.h>
     28 
     29 #ifdef __weak_alias
     30 __weak_alias(asctime,_asctime)
     31 __weak_alias(asctime_r,_asctime_r)
     32 #endif
     33 
     34 /*
     35 ** Some systems only handle "%.2d"; others only handle "%02d";
     36 ** "%02.2d" makes (most) everybody happy.
     37 ** At least some versions of gcc warn about the %02.2d;
     38 ** we conditionalize below to avoid the warning.
     39 */
     40 /*
     41 ** All years associated with 32-bit time_t values are exactly four digits long;
     42 ** some years associated with 64-bit time_t values are not.
     43 ** Vintage programs are coded for years that are always four digits long
     44 ** and may assume that the newline always lands in the same place.
     45 ** For years that are less than four digits, we pad the output with
     46 ** leading zeroes to get the newline in the traditional place.
     47 ** The -4 ensures that we get four characters of output even if
     48 ** we call a strftime variant that produces fewer characters for some years.
     49 ** The ISO C 1999 and POSIX 1003.1-2004 standards prohibit padding the year,
     50 ** but many implementations pad anyway; most likely the standards are buggy.
     51 */
     52 #ifdef __GNUC__
     53 #define ASCTIME_FMT	"%s %s%3d %2.2d:%2.2d:%2.2d %-4s\n"
     54 #else /* !defined __GNUC__ */
     55 #define ASCTIME_FMT	"%s %s%3d %02.2d:%02.2d:%02.2d %-4s\n"
     56 #endif /* !defined __GNUC__ */
     57 /*
     58 ** For years that are more than four digits we put extra spaces before the year
     59 ** so that code trying to overwrite the newline won't end up overwriting
     60 ** a digit within a year and truncating the year (operating on the assumption
     61 ** that no output is better than wrong output).
     62 */
     63 #ifdef __GNUC__
     64 #define ASCTIME_FMT_B	"%s %s%3d %2.2d:%2.2d:%2.2d     %s\n"
     65 #else /* !defined __GNUC__ */
     66 #define ASCTIME_FMT_B	"%s %s%3d %02.2d:%02.2d:%02.2d     %s\n"
     67 #endif /* !defined __GNUC__ */
     68 
     69 #define STD_ASCTIME_BUF_SIZE	26
     70 /*
     71 ** Big enough for something such as
     72 ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648     -2147483648\n
     73 ** (two three-character abbreviations, five strings denoting integers,
     74 ** seven explicit spaces, two explicit colons, a newline,
     75 ** and a trailing NUL byte).
     76 ** The values above are for systems where an int is 32 bits and are provided
     77 ** as an example; the define below calculates the maximum for the system at
     78 ** hand.
     79 */
     80 #define MAX_ASCTIME_BUF_SIZE	(2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1)
     81 
     82 static char	buf_asctime[MAX_ASCTIME_BUF_SIZE];
     83 
     84 /*
     85 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
     86 */
     87 
     88 char *
     89 asctime_r(const struct tm *timeptr, char *buf)
     90 {
     91 	static const char	wday_name[][4] = {
     92 		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
     93 	};
     94 	static const char	mon_name[][4] = {
     95 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
     96 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
     97 	};
     98 	const char *	wn;
     99 	const char *	mn;
    100 	char			year[INT_STRLEN_MAXIMUM(int) + 2];
    101 	char			result[MAX_ASCTIME_BUF_SIZE];
    102 
    103 	if (timeptr == NULL) {
    104 		errno = EINVAL;
    105 		return strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
    106 	}
    107 	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
    108 		wn = "???";
    109 	else	wn = wday_name[timeptr->tm_wday];
    110 	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
    111 		mn = "???";
    112 	else	mn = mon_name[timeptr->tm_mon];
    113 	/*
    114 	** Use strftime's %Y to generate the year, to avoid overflow problems
    115 	** when computing timeptr->tm_year + TM_YEAR_BASE.
    116 	** Assume that strftime is unaffected by other out-of-range members
    117 	** (e.g., timeptr->tm_mday) when processing "%Y".
    118 	*/
    119 	(void) strftime(year, sizeof year, "%Y", timeptr);
    120 	(void) snprintf(result,
    121 		sizeof(result),
    122 		((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
    123 		wn, mn,
    124 		timeptr->tm_mday, timeptr->tm_hour,
    125 		timeptr->tm_min, timeptr->tm_sec,
    126 		year);
    127 	if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime)
    128 		return strcpy(buf, result);
    129 	else {
    130 		errno = EOVERFLOW;
    131 		return NULL;
    132 	}
    133 }
    134 
    135 /*
    136 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
    137 */
    138 
    139 char *
    140 asctime(const struct tm *timeptr)
    141 {
    142 	return asctime_r(timeptr, buf_asctime);
    143 }
    144