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