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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/tools/tzcode/asctime.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,138 @@
     1.4 +/*
     1.5 +** This file is in the public domain, so clarified as of
     1.6 +** 1996-06-05 by Arthur David Olson.
     1.7 +*/
     1.8 +
     1.9 +/*
    1.10 +** Avoid the temptation to punt entirely to strftime;
    1.11 +** the output of strftime is supposed to be locale specific
    1.12 +** whereas the output of asctime is supposed to be constant.
    1.13 +*/
    1.14 +
    1.15 +#ifndef lint
    1.16 +#ifndef NOID
    1.17 +static char	elsieid[] = "@(#)asctime.c	8.2";
    1.18 +#endif /* !defined NOID */
    1.19 +#endif /* !defined lint */
    1.20 +
    1.21 +/*LINTLIBRARY*/
    1.22 +
    1.23 +#include "private.h"
    1.24 +#include "tzfile.h"
    1.25 +
    1.26 +/*
    1.27 +** Some systems only handle "%.2d"; others only handle "%02d";
    1.28 +** "%02.2d" makes (most) everybody happy.
    1.29 +** At least some versions of gcc warn about the %02.2d;
    1.30 +** we conditionalize below to avoid the warning.
    1.31 +*/
    1.32 +/*
    1.33 +** All years associated with 32-bit time_t values are exactly four digits long;
    1.34 +** some years associated with 64-bit time_t values are not.
    1.35 +** Vintage programs are coded for years that are always four digits long
    1.36 +** and may assume that the newline always lands in the same place.
    1.37 +** For years that are less than four digits, we pad the output with
    1.38 +** leading zeroes to get the newline in the traditional place.
    1.39 +** The -4 ensures that we get four characters of output even if
    1.40 +** we call a strftime variant that produces fewer characters for some years.
    1.41 +** The ISO C 1999 and POSIX 1003.1-2004 standards prohibit padding the year,
    1.42 +** but many implementations pad anyway; most likely the standards are buggy.
    1.43 +*/
    1.44 +#ifdef __GNUC__
    1.45 +#define ASCTIME_FMT	"%.3s %.3s%3d %2.2d:%2.2d:%2.2d %-4s\n"
    1.46 +#else /* !defined __GNUC__ */
    1.47 +#define ASCTIME_FMT	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d %-4s\n"
    1.48 +#endif /* !defined __GNUC__ */
    1.49 +/*
    1.50 +** For years that are more than four digits we put extra spaces before the year
    1.51 +** so that code trying to overwrite the newline won't end up overwriting
    1.52 +** a digit within a year and truncating the year (operating on the assumption
    1.53 +** that no output is better than wrong output).
    1.54 +*/
    1.55 +#ifdef __GNUC__
    1.56 +#define ASCTIME_FMT_B	"%.3s %.3s%3d %2.2d:%2.2d:%2.2d     %s\n"
    1.57 +#else /* !defined __GNUC__ */
    1.58 +#define ASCTIME_FMT_B	"%.3s %.3s%3d %02.2d:%02.2d:%02.2d     %s\n"
    1.59 +#endif /* !defined __GNUC__ */
    1.60 +
    1.61 +#define STD_ASCTIME_BUF_SIZE	26
    1.62 +/*
    1.63 +** Big enough for something such as
    1.64 +** ??? ???-2147483648 -2147483648:-2147483648:-2147483648     -2147483648\n
    1.65 +** (two three-character abbreviations, five strings denoting integers,
    1.66 +** seven explicit spaces, two explicit colons, a newline,
    1.67 +** and a trailing ASCII nul).
    1.68 +** The values above are for systems where an int is 32 bits and are provided
    1.69 +** as an example; the define below calculates the maximum for the system at
    1.70 +** hand.
    1.71 +*/
    1.72 +#define MAX_ASCTIME_BUF_SIZE	(2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1)
    1.73 +
    1.74 +static char	buf_asctime[MAX_ASCTIME_BUF_SIZE];
    1.75 +
    1.76 +/*
    1.77 +** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
    1.78 +*/
    1.79 +
    1.80 +char *
    1.81 +asctime_r(timeptr, buf)
    1.82 +register const struct tm *	timeptr;
    1.83 +char *				buf;
    1.84 +{
    1.85 +	static const char	wday_name[][3] = {
    1.86 +		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    1.87 +	};
    1.88 +	static const char	mon_name[][3] = {
    1.89 +		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
    1.90 +		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    1.91 +	};
    1.92 +	register const char *	wn;
    1.93 +	register const char *	mn;
    1.94 +	char			year[INT_STRLEN_MAXIMUM(int) + 2];
    1.95 +	char			result[MAX_ASCTIME_BUF_SIZE];
    1.96 +
    1.97 +	if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
    1.98 +		wn = "???";
    1.99 +	else	wn = wday_name[timeptr->tm_wday];
   1.100 +	if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
   1.101 +		mn = "???";
   1.102 +	else	mn = mon_name[timeptr->tm_mon];
   1.103 +	/*
   1.104 +	** Use strftime's %Y to generate the year, to avoid overflow problems
   1.105 +	** when computing timeptr->tm_year + TM_YEAR_BASE.
   1.106 +	** Assume that strftime is unaffected by other out-of-range members
   1.107 +	** (e.g., timeptr->tm_mday) when processing "%Y".
   1.108 +	*/
   1.109 +	(void) strftime(year, sizeof year, "%Y", timeptr);
   1.110 +	/*
   1.111 +	** We avoid using snprintf since it's not available on all systems.
   1.112 +	*/
   1.113 +	(void) sprintf(result,
   1.114 +		((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
   1.115 +		wn, mn,
   1.116 +		timeptr->tm_mday, timeptr->tm_hour,
   1.117 +		timeptr->tm_min, timeptr->tm_sec,
   1.118 +		year);
   1.119 +	if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime) {
   1.120 +		(void) strcpy(buf, result);
   1.121 +		return buf;
   1.122 +	} else {
   1.123 +#ifdef EOVERFLOW
   1.124 +		errno = EOVERFLOW;
   1.125 +#else /* !defined EOVERFLOW */
   1.126 +		errno = EINVAL;
   1.127 +#endif /* !defined EOVERFLOW */
   1.128 +		return NULL;
   1.129 +	}
   1.130 +}
   1.131 +
   1.132 +/*
   1.133 +** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
   1.134 +*/
   1.135 +
   1.136 +char *
   1.137 +asctime(timeptr)
   1.138 +register const struct tm *	timeptr;
   1.139 +{
   1.140 +	return asctime_r(timeptr, buf_asctime);
   1.141 +}

mercurial