9fb22a3ec4
This is quite a large patch because we haven't updated for some time, but the good news is that upstream is now thread-safe so a lot of our changes go away in this update and the remaining diff is a lot smaller. (Note that our whitespace still doesn't match upstream. I use diff -wub to compare. Upstream doesn't even really have a consistent style. New code seems to be two spaces, old code tabs.) From the intervening changelogs (eliding the changes that only affected the tools, which we don't use): 2014a: An uninitialized-storage bug in 'localtime' has been fixed. (Thanks to Logan Chien.) 2014b: 'zic' and 'localtime' no longer reject locations needing four transitions per year for the forseeable future. (Thanks to Andrew Main (Zefram).) 2014c: <None> 2014d: <None> 2014e: <None> 2014f: 'localtime', 'mktime', etc. now use much less stack space if ALL_STATE is defined. (Thanks to Elliott Hughes for reporting the problem.) Some lint has been removed when using GCC_DEBUG_FLAGS with GCC 4.9.0. 2014g: Unless NETBSD_INSPIRED is defined to 0, the tz library now supplies functions for creating and using objects that represent time zones. The new functions are tzalloc, tzfree, localtime_rz, mktime_z, and (if STD_INSPIRED is also defined) posix2time_z and time2posix_z. They are intended for performance: for example, localtime_rz (unlike localtime_r) is trivially thread-safe without locking. (Thanks to Christos Zoulas for proposing NetBSD-inspired functions, and to Alan Barrett and Jonathan Lennox for helping to debug the change.) If THREAD_SAFE is defined to 1, the tz library is now thread-safe. Although not needed for tz's own applications, which are single-threaded, this supports POSIX better if the tz library is used in multithreaded apps. Some crashes have been fixed when zdump or the tz library is given invalid or outlandish input. The tz library no longer mishandles leap seconds on platforms with unsigned time_t in time zones that lack ordinary transitions after 1970. The tz code now attempts to infer TM_GMTOFF and TM_ZONE if not already defined, to make it easier to configure on common platforms. Define NO_TM_GMTOFF and NO_TM_ZONE to suppress this. Unless the new macro UNINIT_TRAP is defined to 1, the tz code now assumes that reading uninitialized memory yields garbage values but does not cause other problems such as traps. If TM_GMTOFF is defined and UNINIT_TRAP is 0, mktime is now more likely to guess right for ambiguous time stamps near transitions where tm_isdst does not change. If HAVE_STRFTIME_L is defined to 1, the tz library now defines strftime_l for compatibility with recent versions of POSIX. Only the C locale is supported, though. HAVE_STRFTIME_L defaults to 1 on recent POSIX versions, and to 0 otherwise. tzselect -c now uses a hybrid distance measure that works better in Africa. (Thanks to Alan Barrett for noting the problem.) The C source code now ports to NetBSD when GCC_DEBUG_FLAGS is used, or when time_tz is defined. When HAVE_UTMPX_H is set the 'date' command now builds on systems whose <utmpx.h> file does not define WTMPX_FILE, and when setting the date it updates the wtmpx file if _PATH_WTMPX is defined. This affects GNU/Linux and similar systems. For easier maintenance later, some C code has been simplified, some lint has been removed, and the code has been tweaked so that plain 'make' is more likely to work. The C type 'bool' is now used for boolean values, instead of 'int'. The long-obsolete LOCALE_HOME code has been removed. The long-obsolete 'gtime' function has been removed. 2014h: The tz library's localtime and mktime functions now set tzname to a value appropriate for the requested time stamp, and zdump now uses this on platforms not defining TM_ZONE, fixing a 2014g regression. (Thanks to Tim Parenti for reporting the problem.) The tz library no longer sets tzname if localtime or mktime fails. An access to uninitalized data has been fixed. (Thanks to Jörg Richter for reporting the problem.) When THREAD_SAFE is defined, the code ports to the C11 memory model. A memory leak has been fixed if ALL_STATE and THREAD_SAFE are defined and two threads race to initialize data used by gmtime-like functions. (Thanks to Andy Heninger for reporting the problems.) 2014i: The time-related library functions now set errno on failure, and some crashes in the new tzalloc-related library functions have been fixed. (Thanks to Christos Zoulas for reporting most of these problems and for suggesting fixes.) If USG_COMPAT is defined and the requested time stamp is standard time, the tz library's localtime and mktime functions now set the extern variable timezone to a value appropriate for that time stamp; and similarly for ALTZONE, daylight saving time, and the altzone variable. This change is a companion to the tzname change in 2014h, and is designed to make timezone and altzone more compatible with tzname. The tz library's functions now set errno to EOVERFLOW if they fail because the result cannot be represented. ctime and ctime_r now return NULL and set errno when a time stamp is out of range, rather than having undefined behavior. Some bugs associated with the new 2014g functions have been fixed. This includes a bug that largely incapacitated the new functions time2posix_z and posix2time_z. (Thanks to Christos Zoulas.) It also includes some uses of uninitialized variables after tzalloc. The new code uses the standard type 'ssize_t', which the Makefile now gives porting advice about. 2014j: <None> 2015a: tzalloc now scrubs time zone abbreviations compatibly with the way that tzset always has, by replacing invalid bytes with '_' and by shortening too-long abbreviations. 2015b: Fix integer overflow bug in reference 'mktime' implementation. (Problem reported by Jörg Richter.) Allow -Dtime_tz=time_t compilations, and allow -Dtime_tz=... libraries to be used in the same executable as standard-library time_t functions. (Problems reported by Bradley White.) 2015c: <None> 2015d: <None> 2015e: <None> 2015f: <None> 2015g: localtime no longer mishandles America/Anchorage after 2037. (Thanks to Bradley White for reporting the bug.) On hosts with signed 32-bit time_t, localtime no longer mishandles Pacific/Fiji after 2038-01-16 14:00 UTC. The localtime module allows the variables 'timezone', 'daylight', and 'altzone' to be in common storage shared with other modules, and declares them in case the system <time.h> does not. (Problems reported by Kees Dekker.) On platforms with tm_zone, strftime.c now assumes it is not NULL. This simplifies the code and is consistent with zdump.c. (Problem reported by Christos Zoulas.) Change-Id: I9eb0a8323cb8bd9968fcfe612dc14f45aa3b59d2
128 lines
4.2 KiB
C
128 lines
4.2 KiB
C
/*
|
|
** This file is in the public domain, so clarified as of
|
|
** 1996-06-05 by Arthur David Olson.
|
|
*/
|
|
|
|
/*
|
|
** Avoid the temptation to punt entirely to strftime;
|
|
** the output of strftime is supposed to be locale specific
|
|
** whereas the output of asctime is supposed to be constant.
|
|
*/
|
|
|
|
/*LINTLIBRARY*/
|
|
|
|
#include "private.h"
|
|
#include "tzfile.h"
|
|
|
|
/*
|
|
** Some systems only handle "%.2d"; others only handle "%02d";
|
|
** "%02.2d" makes (most) everybody happy.
|
|
** At least some versions of gcc warn about the %02.2d;
|
|
** we conditionalize below to avoid the warning.
|
|
*/
|
|
/*
|
|
** All years associated with 32-bit time_t values are exactly four digits long;
|
|
** some years associated with 64-bit time_t values are not.
|
|
** Vintage programs are coded for years that are always four digits long
|
|
** and may assume that the newline always lands in the same place.
|
|
** For years that are less than four digits, we pad the output with
|
|
** leading zeroes to get the newline in the traditional place.
|
|
** The -4 ensures that we get four characters of output even if
|
|
** we call a strftime variant that produces fewer characters for some years.
|
|
** The ISO C 1999 and POSIX 1003.1-2004 standards prohibit padding the year,
|
|
** but many implementations pad anyway; most likely the standards are buggy.
|
|
*/
|
|
#ifdef __GNUC__
|
|
#define ASCTIME_FMT "%.3s %.3s%3d %2.2d:%2.2d:%2.2d %-4s\n"
|
|
#else /* !defined __GNUC__ */
|
|
#define ASCTIME_FMT "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %-4s\n"
|
|
#endif /* !defined __GNUC__ */
|
|
/*
|
|
** For years that are more than four digits we put extra spaces before the year
|
|
** so that code trying to overwrite the newline won't end up overwriting
|
|
** a digit within a year and truncating the year (operating on the assumption
|
|
** that no output is better than wrong output).
|
|
*/
|
|
#ifdef __GNUC__
|
|
#define ASCTIME_FMT_B "%.3s %.3s%3d %2.2d:%2.2d:%2.2d %s\n"
|
|
#else /* !defined __GNUC__ */
|
|
#define ASCTIME_FMT_B "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %s\n"
|
|
#endif /* !defined __GNUC__ */
|
|
|
|
#define STD_ASCTIME_BUF_SIZE 26
|
|
/*
|
|
** Big enough for something such as
|
|
** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n
|
|
** (two three-character abbreviations, five strings denoting integers,
|
|
** seven explicit spaces, two explicit colons, a newline,
|
|
** and a trailing NUL byte).
|
|
** The values above are for systems where an int is 32 bits and are provided
|
|
** as an example; the define below calculates the maximum for the system at
|
|
** hand.
|
|
*/
|
|
#define MAX_ASCTIME_BUF_SIZE (2*3+5*INT_STRLEN_MAXIMUM(int)+7+2+1+1)
|
|
|
|
static char buf_asctime[MAX_ASCTIME_BUF_SIZE];
|
|
|
|
/*
|
|
** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
|
|
*/
|
|
|
|
char *
|
|
asctime_r(register const struct tm *timeptr, char *buf)
|
|
{
|
|
static const char wday_name[][3] = {
|
|
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
|
};
|
|
static const char mon_name[][3] = {
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
};
|
|
register const char * wn;
|
|
register const char * mn;
|
|
char year[INT_STRLEN_MAXIMUM(int) + 2];
|
|
char result[MAX_ASCTIME_BUF_SIZE];
|
|
|
|
if (timeptr == NULL) {
|
|
errno = EINVAL;
|
|
return strcpy(buf, "??? ??? ?? ??:??:?? ????\n");
|
|
}
|
|
if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK)
|
|
wn = "???";
|
|
else wn = wday_name[timeptr->tm_wday];
|
|
if (timeptr->tm_mon < 0 || timeptr->tm_mon >= MONSPERYEAR)
|
|
mn = "???";
|
|
else mn = mon_name[timeptr->tm_mon];
|
|
/*
|
|
** Use strftime's %Y to generate the year, to avoid overflow problems
|
|
** when computing timeptr->tm_year + TM_YEAR_BASE.
|
|
** Assume that strftime is unaffected by other out-of-range members
|
|
** (e.g., timeptr->tm_mday) when processing "%Y".
|
|
*/
|
|
strftime(year, sizeof year, "%Y", timeptr);
|
|
/*
|
|
** We avoid using snprintf since it's not available on all systems.
|
|
*/
|
|
snprintf(result, sizeof(result), /* Android change: use snprintf. */
|
|
((strlen(year) <= 4) ? ASCTIME_FMT : ASCTIME_FMT_B),
|
|
wn, mn,
|
|
timeptr->tm_mday, timeptr->tm_hour,
|
|
timeptr->tm_min, timeptr->tm_sec,
|
|
year);
|
|
if (strlen(result) < STD_ASCTIME_BUF_SIZE || buf == buf_asctime)
|
|
return strcpy(buf, result);
|
|
else {
|
|
errno = EOVERFLOW;
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
/*
|
|
** A la ISO/IEC 9945-1, ANSI/IEEE Std 1003.1, 2004 Edition.
|
|
*/
|
|
|
|
char *
|
|
asctime(register const struct tm *timeptr)
|
|
{
|
|
return asctime_r(timeptr, buf_asctime);
|
|
}
|