1*a979394aSDag-Erling Smørgrav /* asctime a la ISO C. */ 2bc421551SDag-Erling Smørgrav 3bc421551SDag-Erling Smørgrav /* 4bc421551SDag-Erling Smørgrav ** This file is in the public domain, so clarified as of 5bc421551SDag-Erling Smørgrav ** 1996-06-05 by Arthur David Olson. 6bc421551SDag-Erling Smørgrav */ 7bc421551SDag-Erling Smørgrav 8bc421551SDag-Erling Smørgrav /* 9bc421551SDag-Erling Smørgrav ** Avoid the temptation to punt entirely to strftime; 10bc421551SDag-Erling Smørgrav ** the output of strftime is supposed to be locale specific 11bc421551SDag-Erling Smørgrav ** whereas the output of asctime is supposed to be constant. 12bc421551SDag-Erling Smørgrav */ 13bc421551SDag-Erling Smørgrav 14bc421551SDag-Erling Smørgrav /*LINTLIBRARY*/ 15bc421551SDag-Erling Smørgrav 16bc421551SDag-Erling Smørgrav #include "namespace.h" 17bc421551SDag-Erling Smørgrav #include "private.h" 18bc421551SDag-Erling Smørgrav #include "un-namespace.h" 19bc421551SDag-Erling Smørgrav #include <stdio.h> 20bc421551SDag-Erling Smørgrav 21bc421551SDag-Erling Smørgrav /* 22bc421551SDag-Erling Smørgrav ** All years associated with 32-bit time_t values are exactly four digits long; 23bc421551SDag-Erling Smørgrav ** some years associated with 64-bit time_t values are not. 24bc421551SDag-Erling Smørgrav ** Vintage programs are coded for years that are always four digits long 25bc421551SDag-Erling Smørgrav ** and may assume that the newline always lands in the same place. 26bc421551SDag-Erling Smørgrav ** For years that are less than four digits, we pad the output with 27bc421551SDag-Erling Smørgrav ** leading zeroes to get the newline in the traditional place. 28bc421551SDag-Erling Smørgrav ** The -4 ensures that we get four characters of output even if 29bc421551SDag-Erling Smørgrav ** we call a strftime variant that produces fewer characters for some years. 30*a979394aSDag-Erling Smørgrav ** This conforms to recent ISO C and POSIX standards, which say behavior 31*a979394aSDag-Erling Smørgrav ** is undefined when the year is less than 1000 or greater than 9999. 32bc421551SDag-Erling Smørgrav */ 33bc421551SDag-Erling Smørgrav static char const ASCTIME_FMT[] = "%s %s%3d %.2d:%.2d:%.2d %-4s\n"; 34bc421551SDag-Erling Smørgrav /* 35bc421551SDag-Erling Smørgrav ** For years that are more than four digits we put extra spaces before the year 36bc421551SDag-Erling Smørgrav ** so that code trying to overwrite the newline won't end up overwriting 37bc421551SDag-Erling Smørgrav ** a digit within a year and truncating the year (operating on the assumption 38bc421551SDag-Erling Smørgrav ** that no output is better than wrong output). 39bc421551SDag-Erling Smørgrav */ 40bc421551SDag-Erling Smørgrav static char const ASCTIME_FMT_B[] = "%s %s%3d %.2d:%.2d:%.2d %s\n"; 41bc421551SDag-Erling Smørgrav 42bc421551SDag-Erling Smørgrav enum { STD_ASCTIME_BUF_SIZE = 26 }; 43bc421551SDag-Erling Smørgrav /* 44bc421551SDag-Erling Smørgrav ** Big enough for something such as 45bc421551SDag-Erling Smørgrav ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n 46bc421551SDag-Erling Smørgrav ** (two three-character abbreviations, five strings denoting integers, 47bc421551SDag-Erling Smørgrav ** seven explicit spaces, two explicit colons, a newline, 48bc421551SDag-Erling Smørgrav ** and a trailing NUL byte). 49bc421551SDag-Erling Smørgrav ** The values above are for systems where an int is 32 bits and are provided 50bc421551SDag-Erling Smørgrav ** as an example; the size expression below is a bound for the system at 51bc421551SDag-Erling Smørgrav ** hand. 52bc421551SDag-Erling Smørgrav */ 53bc421551SDag-Erling Smørgrav static char buf_asctime[2*3 + 5*INT_STRLEN_MAXIMUM(int) + 7 + 2 + 1 + 1]; 54bc421551SDag-Erling Smørgrav 5575411d15SDag-Erling Smørgrav /* A similar buffer for ctime. 5675411d15SDag-Erling Smørgrav C89 requires that they be the same buffer. 5775411d15SDag-Erling Smørgrav This requirement was removed in C99, so support it only if requested, 5875411d15SDag-Erling Smørgrav as support is more likely to lead to bugs in badly written programs. */ 5975411d15SDag-Erling Smørgrav #if SUPPORT_C89 6075411d15SDag-Erling Smørgrav # define buf_ctime buf_asctime 6175411d15SDag-Erling Smørgrav #else 6275411d15SDag-Erling Smørgrav static char buf_ctime[sizeof buf_asctime]; 6375411d15SDag-Erling Smørgrav #endif 6475411d15SDag-Erling Smørgrav 65*a979394aSDag-Erling Smørgrav /* Publish asctime_r and ctime_r only when supporting older POSIX. */ 66*a979394aSDag-Erling Smørgrav #if SUPPORT_POSIX2008 67*a979394aSDag-Erling Smørgrav # define asctime_static 68*a979394aSDag-Erling Smørgrav #else 69*a979394aSDag-Erling Smørgrav # define asctime_static static 70*a979394aSDag-Erling Smørgrav # undef asctime_r 71*a979394aSDag-Erling Smørgrav # undef ctime_r 72*a979394aSDag-Erling Smørgrav # define asctime_r static_asctime_r 73*a979394aSDag-Erling Smørgrav # define ctime_r static_ctime_r 74*a979394aSDag-Erling Smørgrav #endif 75*a979394aSDag-Erling Smørgrav 76*a979394aSDag-Erling Smørgrav asctime_static 77bc421551SDag-Erling Smørgrav char * 7875411d15SDag-Erling Smørgrav asctime_r(struct tm const *restrict timeptr, char *restrict buf) 79bc421551SDag-Erling Smørgrav { 80bc421551SDag-Erling Smørgrav static const char wday_name[][4] = { 81bc421551SDag-Erling Smørgrav "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 82bc421551SDag-Erling Smørgrav }; 83bc421551SDag-Erling Smørgrav static const char mon_name[][4] = { 84bc421551SDag-Erling Smørgrav "Jan", "Feb", "Mar", "Apr", "May", "Jun", 85bc421551SDag-Erling Smørgrav "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 86bc421551SDag-Erling Smørgrav }; 87bc421551SDag-Erling Smørgrav const char * wn; 88bc421551SDag-Erling Smørgrav const char * mn; 89bc421551SDag-Erling Smørgrav char year[INT_STRLEN_MAXIMUM(int) + 2]; 90bc421551SDag-Erling Smørgrav char result[sizeof buf_asctime]; 91bc421551SDag-Erling Smørgrav 92bc421551SDag-Erling Smørgrav if (timeptr == NULL) { 93bc421551SDag-Erling Smørgrav errno = EINVAL; 94bc421551SDag-Erling Smørgrav return strcpy(buf, "??? ??? ?? ??:??:?? ????\n"); 95bc421551SDag-Erling Smørgrav } 96bc421551SDag-Erling Smørgrav if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK) 97bc421551SDag-Erling Smørgrav wn = "???"; 98bc421551SDag-Erling Smørgrav else wn = wday_name[timeptr->tm_wday]; 99bc421551SDag-Erling Smørgrav if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR) 100bc421551SDag-Erling Smørgrav mn = "???"; 101bc421551SDag-Erling Smørgrav else mn = mon_name[timeptr->tm_mon]; 102bc421551SDag-Erling Smørgrav /* 103bc421551SDag-Erling Smørgrav ** Use strftime's %Y to generate the year, to avoid overflow problems 104bc421551SDag-Erling Smørgrav ** when computing timeptr->tm_year + TM_YEAR_BASE. 105bc421551SDag-Erling Smørgrav ** Assume that strftime is unaffected by other out-of-range members 106bc421551SDag-Erling Smørgrav ** (e.g., timeptr->tm_mday) when processing "%Y". 107bc421551SDag-Erling Smørgrav */ 108bc421551SDag-Erling Smørgrav strftime(year, sizeof year, "%Y", timeptr); 109bc421551SDag-Erling Smørgrav /* 110bc421551SDag-Erling Smørgrav ** We avoid using snprintf since it's not available on all systems. 111bc421551SDag-Erling Smørgrav */ 112bc421551SDag-Erling Smørgrav sprintf(result, 113bc421551SDag-Erling Smørgrav ((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B), 114bc421551SDag-Erling Smørgrav wn, mn, 115bc421551SDag-Erling Smørgrav timeptr->tm_mday, timeptr->tm_hour, 116bc421551SDag-Erling Smørgrav timeptr->tm_min, timeptr->tm_sec, 117bc421551SDag-Erling Smørgrav year); 11875411d15SDag-Erling Smørgrav if (strlen(result) < STD_ASCTIME_BUF_SIZE 11975411d15SDag-Erling Smørgrav || buf == buf_ctime || buf == buf_asctime) 120bc421551SDag-Erling Smørgrav return strcpy(buf, result); 121bc421551SDag-Erling Smørgrav else { 122bc421551SDag-Erling Smørgrav errno = EOVERFLOW; 123bc421551SDag-Erling Smørgrav return NULL; 124bc421551SDag-Erling Smørgrav } 125bc421551SDag-Erling Smørgrav } 126bc421551SDag-Erling Smørgrav 127bc421551SDag-Erling Smørgrav char * 128bc421551SDag-Erling Smørgrav asctime(register const struct tm *timeptr) 129bc421551SDag-Erling Smørgrav { 130bc421551SDag-Erling Smørgrav return asctime_r(timeptr, buf_asctime); 131bc421551SDag-Erling Smørgrav } 13275411d15SDag-Erling Smørgrav 133*a979394aSDag-Erling Smørgrav asctime_static 13475411d15SDag-Erling Smørgrav char * 13575411d15SDag-Erling Smørgrav ctime_r(const time_t *timep, char *buf) 13675411d15SDag-Erling Smørgrav { 13775411d15SDag-Erling Smørgrav struct tm mytm; 13875411d15SDag-Erling Smørgrav struct tm *tmp = localtime_r(timep, &mytm); 13975411d15SDag-Erling Smørgrav return tmp ? asctime_r(tmp, buf) : NULL; 14075411d15SDag-Erling Smørgrav } 14175411d15SDag-Erling Smørgrav 14275411d15SDag-Erling Smørgrav char * 14375411d15SDag-Erling Smørgrav ctime(const time_t *timep) 14475411d15SDag-Erling Smørgrav { 14575411d15SDag-Erling Smørgrav return ctime_r(timep, buf_ctime); 14675411d15SDag-Erling Smørgrav } 147