michael@0: /* michael@0: ** This file is in the public domain, so clarified as of michael@0: ** 1996-06-05 by Arthur David Olson. michael@0: */ michael@0: michael@0: /* michael@0: ** Avoid the temptation to punt entirely to strftime; michael@0: ** the output of strftime is supposed to be locale specific michael@0: ** whereas the output of asctime is supposed to be constant. michael@0: */ michael@0: michael@0: #ifndef lint michael@0: #ifndef NOID michael@0: static char elsieid[] = "@(#)asctime.c 8.2"; michael@0: #endif /* !defined NOID */ michael@0: #endif /* !defined lint */ michael@0: michael@0: /*LINTLIBRARY*/ michael@0: michael@0: #include "private.h" michael@0: #include "tzfile.h" michael@0: michael@0: /* michael@0: ** Some systems only handle "%.2d"; others only handle "%02d"; michael@0: ** "%02.2d" makes (most) everybody happy. michael@0: ** At least some versions of gcc warn about the %02.2d; michael@0: ** we conditionalize below to avoid the warning. michael@0: */ michael@0: /* michael@0: ** All years associated with 32-bit time_t values are exactly four digits long; michael@0: ** some years associated with 64-bit time_t values are not. michael@0: ** Vintage programs are coded for years that are always four digits long michael@0: ** and may assume that the newline always lands in the same place. michael@0: ** For years that are less than four digits, we pad the output with michael@0: ** leading zeroes to get the newline in the traditional place. michael@0: ** The -4 ensures that we get four characters of output even if michael@0: ** we call a strftime variant that produces fewer characters for some years. michael@0: ** The ISO C 1999 and POSIX 1003.1-2004 standards prohibit padding the year, michael@0: ** but many implementations pad anyway; most likely the standards are buggy. michael@0: */ michael@0: #ifdef __GNUC__ michael@0: #define ASCTIME_FMT "%.3s %.3s%3d %2.2d:%2.2d:%2.2d %-4s\n" michael@0: #else /* !defined __GNUC__ */ michael@0: #define ASCTIME_FMT "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %-4s\n" michael@0: #endif /* !defined __GNUC__ */ michael@0: /* michael@0: ** For years that are more than four digits we put extra spaces before the year michael@0: ** so that code trying to overwrite the newline won't end up overwriting michael@0: ** a digit within a year and truncating the year (operating on the assumption michael@0: ** that no output is better than wrong output). michael@0: */ michael@0: #ifdef __GNUC__ michael@0: #define ASCTIME_FMT_B "%.3s %.3s%3d %2.2d:%2.2d:%2.2d %s\n" michael@0: #else /* !defined __GNUC__ */ michael@0: #define ASCTIME_FMT_B "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %s\n" michael@0: #endif /* !defined __GNUC__ */ michael@0: michael@0: #define STD_ASCTIME_BUF_SIZE 26 michael@0: /* michael@0: ** Big enough for something such as michael@0: ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n michael@0: ** (two three-character abbreviations, five strings denoting integers, michael@0: ** seven explicit spaces, two explicit colons, a newline, michael@0: ** and a trailing ASCII nul). michael@0: ** The values above are for systems where an int is 32 bits and are provided michael@0: ** as an example; the define below calculates the maximum for the system at michael@0: ** hand. michael@0: */ michael@0: #define MAX_ASCTIME_BUF_SIZE (2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1) michael@0: michael@0: static char buf_asctime[MAX_ASCTIME_BUF_SIZE]; michael@0: michael@0: /* michael@0: ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition. michael@0: */ michael@0: michael@0: char * michael@0: asctime_r(timeptr, buf) michael@0: register const struct tm * timeptr; michael@0: char * buf; michael@0: { michael@0: static const char wday_name[][3] = { michael@0: "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" michael@0: }; michael@0: static const char mon_name[][3] = { michael@0: "Jan", "Feb", "Mar", "Apr", "May", "Jun", michael@0: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" michael@0: }; michael@0: register const char * wn; michael@0: register const char * mn; michael@0: char year[INT_STRLEN_MAXIMUM(int) + 2]; michael@0: char result[MAX_ASCTIME_BUF_SIZE]; michael@0: michael@0: if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK) michael@0: wn = "???"; michael@0: else wn = wday_name[timeptr->tm_wday]; michael@0: if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR) michael@0: mn = "???"; michael@0: else mn = mon_name[timeptr->tm_mon]; michael@0: /* michael@0: ** Use strftime's %Y to generate the year, to avoid overflow problems michael@0: ** when computing timeptr->tm_year + TM_YEAR_BASE. michael@0: ** Assume that strftime is unaffected by other out-of-range members michael@0: ** (e.g., timeptr->tm_mday) when processing "%Y". michael@0: */ michael@0: (void) strftime(year, sizeof year, "%Y", timeptr); michael@0: /* michael@0: ** We avoid using snprintf since it's not available on all systems. michael@0: */ michael@0: (void) sprintf(result, michael@0: ((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B), michael@0: wn, mn, michael@0: timeptr->tm_mday, timeptr->tm_hour, michael@0: timeptr->tm_min, timeptr->tm_sec, michael@0: year); michael@0: if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime) { michael@0: (void) strcpy(buf, result); michael@0: return buf; michael@0: } else { michael@0: #ifdef EOVERFLOW michael@0: errno = EOVERFLOW; michael@0: #else /* !defined EOVERFLOW */ michael@0: errno = EINVAL; michael@0: #endif /* !defined EOVERFLOW */ michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition. michael@0: */ michael@0: michael@0: char * michael@0: asctime(timeptr) michael@0: register const struct tm * timeptr; michael@0: { michael@0: return asctime_r(timeptr, buf_asctime); michael@0: }