intl/icu/source/tools/tzcode/asctime.c

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial