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