1bc421551SDag-Erling Smørgrav /* asctime and asctime_r a la POSIX and ISO C, except pad years before 1000. */ 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. 30bc421551SDag-Erling Smørgrav ** The ISO C and POSIX standards prohibit padding the year, 31bc421551SDag-Erling Smørgrav ** but many implementations pad anyway; most likely the standards are buggy. 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 55*75411d15SDag-Erling Smørgrav /* A similar buffer for ctime. 56*75411d15SDag-Erling Smørgrav C89 requires that they be the same buffer. 57*75411d15SDag-Erling Smørgrav This requirement was removed in C99, so support it only if requested, 58*75411d15SDag-Erling Smørgrav as support is more likely to lead to bugs in badly written programs. */ 59*75411d15SDag-Erling Smørgrav #if SUPPORT_C89 60*75411d15SDag-Erling Smørgrav # define buf_ctime buf_asctime 61*75411d15SDag-Erling Smørgrav #else 62*75411d15SDag-Erling Smørgrav static char buf_ctime[sizeof buf_asctime]; 63*75411d15SDag-Erling Smørgrav #endif 64*75411d15SDag-Erling Smørgrav 65bc421551SDag-Erling Smørgrav char * 66*75411d15SDag-Erling Smørgrav asctime_r(struct tm const *restrict timeptr, char *restrict buf) 67bc421551SDag-Erling Smørgrav { 68bc421551SDag-Erling Smørgrav static const char wday_name[][4] = { 69bc421551SDag-Erling Smørgrav "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 70bc421551SDag-Erling Smørgrav }; 71bc421551SDag-Erling Smørgrav static const char mon_name[][4] = { 72bc421551SDag-Erling Smørgrav "Jan", "Feb", "Mar", "Apr", "May", "Jun", 73bc421551SDag-Erling Smørgrav "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 74bc421551SDag-Erling Smørgrav }; 75bc421551SDag-Erling Smørgrav const char * wn; 76bc421551SDag-Erling Smørgrav const char * mn; 77bc421551SDag-Erling Smørgrav char year[INT_STRLEN_MAXIMUM(int) + 2]; 78bc421551SDag-Erling Smørgrav char result[sizeof buf_asctime]; 79bc421551SDag-Erling Smørgrav 80bc421551SDag-Erling Smørgrav if (timeptr == NULL) { 81bc421551SDag-Erling Smørgrav errno = EINVAL; 82bc421551SDag-Erling Smørgrav return strcpy(buf, "??? ??? ?? ??:??:?? ????\n"); 83bc421551SDag-Erling Smørgrav } 84bc421551SDag-Erling Smørgrav if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK) 85bc421551SDag-Erling Smørgrav wn = "???"; 86bc421551SDag-Erling Smørgrav else wn = wday_name[timeptr->tm_wday]; 87bc421551SDag-Erling Smørgrav if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR) 88bc421551SDag-Erling Smørgrav mn = "???"; 89bc421551SDag-Erling Smørgrav else mn = mon_name[timeptr->tm_mon]; 90bc421551SDag-Erling Smørgrav /* 91bc421551SDag-Erling Smørgrav ** Use strftime's %Y to generate the year, to avoid overflow problems 92bc421551SDag-Erling Smørgrav ** when computing timeptr->tm_year + TM_YEAR_BASE. 93bc421551SDag-Erling Smørgrav ** Assume that strftime is unaffected by other out-of-range members 94bc421551SDag-Erling Smørgrav ** (e.g., timeptr->tm_mday) when processing "%Y". 95bc421551SDag-Erling Smørgrav */ 96bc421551SDag-Erling Smørgrav strftime(year, sizeof year, "%Y", timeptr); 97bc421551SDag-Erling Smørgrav /* 98bc421551SDag-Erling Smørgrav ** We avoid using snprintf since it's not available on all systems. 99bc421551SDag-Erling Smørgrav */ 100bc421551SDag-Erling Smørgrav sprintf(result, 101bc421551SDag-Erling Smørgrav ((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B), 102bc421551SDag-Erling Smørgrav wn, mn, 103bc421551SDag-Erling Smørgrav timeptr->tm_mday, timeptr->tm_hour, 104bc421551SDag-Erling Smørgrav timeptr->tm_min, timeptr->tm_sec, 105bc421551SDag-Erling Smørgrav year); 106*75411d15SDag-Erling Smørgrav if (strlen(result) < STD_ASCTIME_BUF_SIZE 107*75411d15SDag-Erling Smørgrav || buf == buf_ctime || buf == buf_asctime) 108bc421551SDag-Erling Smørgrav return strcpy(buf, result); 109bc421551SDag-Erling Smørgrav else { 110bc421551SDag-Erling Smørgrav errno = EOVERFLOW; 111bc421551SDag-Erling Smørgrav return NULL; 112bc421551SDag-Erling Smørgrav } 113bc421551SDag-Erling Smørgrav } 114bc421551SDag-Erling Smørgrav 115bc421551SDag-Erling Smørgrav char * 116bc421551SDag-Erling Smørgrav asctime(register const struct tm *timeptr) 117bc421551SDag-Erling Smørgrav { 118bc421551SDag-Erling Smørgrav return asctime_r(timeptr, buf_asctime); 119bc421551SDag-Erling Smørgrav } 120*75411d15SDag-Erling Smørgrav 121*75411d15SDag-Erling Smørgrav char * 122*75411d15SDag-Erling Smørgrav ctime_r(const time_t *timep, char *buf) 123*75411d15SDag-Erling Smørgrav { 124*75411d15SDag-Erling Smørgrav struct tm mytm; 125*75411d15SDag-Erling Smørgrav struct tm *tmp = localtime_r(timep, &mytm); 126*75411d15SDag-Erling Smørgrav return tmp ? asctime_r(tmp, buf) : NULL; 127*75411d15SDag-Erling Smørgrav } 128*75411d15SDag-Erling Smørgrav 129*75411d15SDag-Erling Smørgrav char * 130*75411d15SDag-Erling Smørgrav ctime(const time_t *timep) 131*75411d15SDag-Erling Smørgrav { 132*75411d15SDag-Erling Smørgrav return ctime_r(timep, buf_ctime); 133*75411d15SDag-Erling Smørgrav } 134