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