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