| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** This file is in the public domain, so clarified as of | 2 ** This file is in the public domain, so clarified as of |
| 3 ** 1996-06-05 by Arthur David Olson. | 3 ** 1996-06-05 by Arthur David Olson. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 /* | 6 /* |
| 7 ** Avoid the temptation to punt entirely to strftime; | 7 ** Avoid the temptation to punt entirely to strftime; |
| 8 ** the output of strftime is supposed to be locale specific | 8 ** the output of strftime is supposed to be locale specific |
| 9 ** whereas the output of asctime is supposed to be constant. | 9 ** whereas the output of asctime is supposed to be constant. |
| 10 */ | 10 */ |
| 11 | 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*/ | 12 /*LINTLIBRARY*/ |
| 19 | 13 |
| 20 #include "private.h" | 14 #include "private.h" |
| 21 #include "tzfile.h" | 15 #include "tzfile.h" |
| 22 | 16 |
| 23 /* | 17 /* |
| 24 ** Some systems only handle "%.2d"; others only handle "%02d"; | 18 ** Some systems only handle "%.2d"; others only handle "%02d"; |
| 25 ** "%02.2d" makes (most) everybody happy. | 19 ** "%02.2d" makes (most) everybody happy. |
| 26 ** At least some versions of gcc warn about the %02.2d; | 20 ** At least some versions of gcc warn about the %02.2d; |
| 27 ** we conditionalize below to avoid the warning. | 21 ** we conditionalize below to avoid the warning. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 */ | 62 */ |
| 69 #define MAX_ASCTIME_BUF_SIZE (2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1) | 63 #define MAX_ASCTIME_BUF_SIZE (2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1) |
| 70 | 64 |
| 71 static char buf_asctime[MAX_ASCTIME_BUF_SIZE]; | 65 static char buf_asctime[MAX_ASCTIME_BUF_SIZE]; |
| 72 | 66 |
| 73 /* | 67 /* |
| 74 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition. | 68 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition. |
| 75 */ | 69 */ |
| 76 | 70 |
| 77 char * | 71 char * |
| 78 asctime_r(timeptr, buf) | 72 asctime_r(register const struct tm *timeptr, char *buf) |
| 79 register const struct tm *» timeptr; | |
| 80 char *» » » » buf; | |
| 81 { | 73 { |
| 82 static const char wday_name[][3] = { | 74 static const char wday_name[][3] = { |
| 83 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" | 75 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" |
| 84 }; | 76 }; |
| 85 static const char mon_name[][3] = { | 77 static const char mon_name[][3] = { |
| 86 "Jan", "Feb", "Mar", "Apr", "May", "Jun", | 78 "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 87 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | 79 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 88 }; | 80 }; |
| 89 register const char * wn; | 81 register const char * wn; |
| 90 register const char * mn; | 82 register const char * mn; |
| 91 char year[INT_STRLEN_MAXIMUM(int) + 2]; | 83 char year[INT_STRLEN_MAXIMUM(int) + 2]; |
| 92 char result[MAX_ASCTIME_BUF_SIZE]; | 84 char result[MAX_ASCTIME_BUF_SIZE]; |
| 93 | 85 |
| 86 if (timeptr == NULL) { |
| 87 errno = EINVAL; |
| 88 return strcpy(buf, "??? ??? ?? ??:??:?? ????\n"); |
| 89 } |
| 94 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK) | 90 if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK) |
| 95 wn = "???"; | 91 wn = "???"; |
| 96 else wn = wday_name[timeptr->tm_wday]; | 92 else wn = wday_name[timeptr->tm_wday]; |
| 97 if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR) | 93 if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR) |
| 98 mn = "???"; | 94 mn = "???"; |
| 99 else mn = mon_name[timeptr->tm_mon]; | 95 else mn = mon_name[timeptr->tm_mon]; |
| 100 /* | 96 /* |
| 101 ** Use strftime's %Y to generate the year, to avoid overflow problems | 97 ** Use strftime's %Y to generate the year, to avoid overflow problems |
| 102 ** when computing timeptr->tm_year + TM_YEAR_BASE. | 98 ** when computing timeptr->tm_year + TM_YEAR_BASE. |
| 103 ** Assume that strftime is unaffected by other out-of-range members | 99 ** Assume that strftime is unaffected by other out-of-range members |
| 104 ** (e.g., timeptr->tm_mday) when processing "%Y". | 100 ** (e.g., timeptr->tm_mday) when processing "%Y". |
| 105 */ | 101 */ |
| 106 (void) strftime(year, sizeof year, "%Y", timeptr); | 102 (void) strftime(year, sizeof year, "%Y", timeptr); |
| 107 /* | 103 /* |
| 108 ** We avoid using snprintf since it's not available on all systems. | 104 ** We avoid using snprintf since it's not available on all systems. |
| 109 */ | 105 */ |
| 110 (void) sprintf(result, | 106 (void) sprintf(result, |
| 111 ((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B), | 107 ((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B), |
| 112 wn, mn, | 108 wn, mn, |
| 113 timeptr->tm_mday, timeptr->tm_hour, | 109 timeptr->tm_mday, timeptr->tm_hour, |
| 114 timeptr->tm_min, timeptr->tm_sec, | 110 timeptr->tm_min, timeptr->tm_sec, |
| 115 year); | 111 year); |
| 116 » if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime) { | 112 » if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime) |
| 117 » » (void) strcpy(buf, result); | 113 » » return strcpy(buf, result); |
| 118 » » return buf; | 114 » else { |
| 119 » } else { | |
| 120 #ifdef EOVERFLOW | 115 #ifdef EOVERFLOW |
| 121 errno = EOVERFLOW; | 116 errno = EOVERFLOW; |
| 122 #else /* !defined EOVERFLOW */ | 117 #else /* !defined EOVERFLOW */ |
| 123 errno = EINVAL; | 118 errno = EINVAL; |
| 124 #endif /* !defined EOVERFLOW */ | 119 #endif /* !defined EOVERFLOW */ |
| 125 return NULL; | 120 return NULL; |
| 126 } | 121 } |
| 127 } | 122 } |
| 128 | 123 |
| 129 /* | 124 /* |
| 130 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition. | 125 ** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition. |
| 131 */ | 126 */ |
| 132 | 127 |
| 133 char * | 128 char * |
| 134 asctime(timeptr) | 129 asctime(register const struct tm *timeptr) |
| 135 register const struct tm *» timeptr; | |
| 136 { | 130 { |
| 137 return asctime_r(timeptr, buf_asctime); | 131 return asctime_r(timeptr, buf_asctime); |
| 138 } | 132 } |
| OLD | NEW |