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