auto import //branches/master/...@140412

This commit is contained in:
The Android Open Source Project 2009-03-18 22:20:24 -07:00
parent 78bf5fc677
commit edbe7fc97b
9 changed files with 2654 additions and 2690 deletions

View File

@ -63,6 +63,18 @@ time_t:
Instead, Bionic provides a <time64.h> header that defines a time64_t type,
and related functions like mktime64(), localtime64(), etc...
strftime() uses time64_t internally, so the '%s' format (seconds since the
epoch) is supported for dates >= 2038.
strftime_tz():
Bionic also provides the non-standard strftime_tz() function, a variant
of strftime() which also accepts a time locale descriptor as defined
by "struct strftime_locale" in <time.h>.
This function is used by the low-level framework code in Android.
Timezone management:

View File

@ -79,9 +79,28 @@ extern struct tm* gmtime_r(const time_t *timep, struct tm *result);
extern char* strptime(const char *buf, const char *fmt, struct tm *tm);
extern size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
/* ANDROID-BEGIN */
struct strftime_locale {
const char * mon[12];
const char * month[12];
const char * wday[7];
const char * weekday[7];
const char * X_fmt;
const char * x_fmt;
const char * c_fmt;
const char * am;
const char * pm;
const char * date_fmt;
};
extern size_t strftime_tz(char *s, size_t max, const char *format, const struct tm *tm, const struct strftime_locale* lc);
/* ANDROID-END */
extern char *ctime(const time_t *timep);
extern char *ctime_r(const time_t *timep, char *buf);
extern void tzset(void);
/* global includes */
extern char* tzname[];
extern int daylight;

View File

@ -21,6 +21,7 @@ static char elsieid[] = "@(#)localtime.c 8.3";
#include "fcntl.h"
#include "float.h" /* for FLT_MAX and DBL_MAX */
#include "thread_private.h"
#include <sys/system_properties.h>
#ifndef TZ_ABBR_MAX_LEN
@ -59,6 +60,20 @@ static char elsieid[] = "@(#)localtime.c 8.3";
# define XLOG(x) do{}while (0)
#endif
/* THREAD-SAFETY SUPPORT GOES HERE */
static pthread_mutex_t _tzMutex = PTHREAD_MUTEX_INITIALIZER;
static __inline__ void _tzLock(void)
{
if (__isthreaded)
pthread_mutex_lock(&_tzMutex);
}
static __inline__ void _tzUnlock(void)
{
if (__isthreaded)
pthread_mutex_unlock(&_tzMutex);
}
#ifndef WILDABBR
/*
@ -120,6 +135,10 @@ struct lsinfo { /* leap second information */
#define MY_TZNAME_MAX 255
#endif /* !defined TZNAME_MAX */
/* XXX: This code should really use time64_t instead of time_t
* but we can't change it without re-generating the index
* file first with the correct data.
*/
struct state {
int leapcnt;
int timecnt;
@ -151,6 +170,8 @@ struct rule {
** Prototypes for static functions.
*/
/* NOTE: all internal functions assume that _tzLock() was already called */
static long detzcode P((const char * codep));
static time_t detzcode64 P((const char * codep));
static int differ_by_repeat P((time_t t1, time_t t0));
@ -230,7 +251,7 @@ char * tzname[2] = {
** Thanks to Paul Eggert for noting this.
*/
static struct tm tm;
static struct tm tmGlobal;
#ifdef USG_COMPAT
time_t timezone = 0;
@ -1184,14 +1205,7 @@ struct state * const sp;
(void) tzparse(gmt, sp, TRUE);
}
#ifndef STD_INSPIRED
/*
** A non-static declaration of tzsetwall in a system header file
** may cause a warning about this upcoming static declaration...
*/
static
#endif /* !defined STD_INSPIRED */
void
static void
tzsetwall P((void))
{
if (lcl_is_set < 0)
@ -1212,8 +1226,8 @@ tzsetwall P((void))
settzname();
}
void
tzset P((void))
static void
tzset_locked P((void))
{
register const char * name = NULL;
static char buf[PROP_VALUE_MAX];
@ -1261,6 +1275,14 @@ tzset P((void))
settzname();
}
void
tzset P((void))
{
_tzLock();
tzset_locked();
_tzUnlock();
}
/*
** The easy way to behave "as if no library function calls" localtime
** is to not call it--so we drop its guts into "localsub", which can be
@ -1367,8 +1389,7 @@ struct tm *
localtime(timep)
const time_t * const timep;
{
tzset();
return localsub(timep, 0L, &tm);
return localtime_r(timep, &tmGlobal);
}
/*
@ -1380,8 +1401,14 @@ localtime_r(timep, tmp)
const time_t * const timep;
struct tm * tmp;
{
tzset();
return localsub(timep, 0L, tmp);
struct tm* result;
_tzLock();
tzset_locked();
result = localsub(timep, 0L, tmp);
_tzUnlock();
return result;
}
/*
@ -1431,7 +1458,7 @@ struct tm *
gmtime(timep)
const time_t * const timep;
{
return gmtsub(timep, 0L, &tm);
return gmtime_r(timep, &tmGlobal);
}
/*
@ -1443,7 +1470,13 @@ gmtime_r(timep, tmp)
const time_t * const timep;
struct tm * tmp;
{
return gmtsub(timep, 0L, tmp);
struct tm* result;
_tzLock();
result = gmtsub(timep, 0L, tmp);
_tzUnlock();
return result;
}
#ifdef STD_INSPIRED
@ -1453,7 +1486,7 @@ offtime(timep, offset)
const time_t * const timep;
const long offset;
{
return gmtsub(timep, offset, &tm);
return gmtsub(timep, offset, &tmGlobal);
}
#endif /* defined STD_INSPIRED */
@ -2002,8 +2035,12 @@ time_t
mktime(tmp)
struct tm * const tmp;
{
tzset();
return time1(tmp, localsub, 0L);
time_t result;
_tzLock();
tzset_locked();
result = time1(tmp, localsub, 0L);
_tzUnlock();
return result;
}
#ifdef STD_INSPIRED
@ -2020,8 +2057,14 @@ time_t
timegm(tmp)
struct tm * const tmp;
{
time_t result;
tmp->tm_isdst = 0;
return time1(tmp, gmtsub, 0L);
_tzLock();
result = time1(tmp, gmtsub, 0L);
_tzUnlock();
return result;
}
time_t
@ -2029,8 +2072,14 @@ timeoff(tmp, offset)
struct tm * const tmp;
const long offset;
{
time_t result;
tmp->tm_isdst = 0;
return time1(tmp, gmtsub, offset);
_tzLock();
result = time1(tmp, gmtsub, offset);
_tzUnlock();
return result;
}
#endif /* defined STD_INSPIRED */

View File

@ -72,10 +72,6 @@ static char privatehid[] = "@(#)private.h 8.2";
#define HAVE_UTMPX_H 0
#endif /* !defined HAVE_UTMPX_H */
#ifndef LOCALE_HOME
#define LOCALE_HOME "/usr/lib/locale"
#endif /* !defined LOCALE_HOME */
#if HAVE_INCOMPATIBLE_CTIME_R
#define asctime_r _incompatible_asctime_r
#define ctime_r _incompatible_ctime_r

View File

@ -37,7 +37,14 @@ static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
#include "fcntl.h"
#include "locale.h"
#include <ctype.h>
#include <time64.h>
/* struct lc_time_T is now defined as strftime_locale
* in <time.h>
*/
#if 1
#define lc_time_T strftime_locale
#else
struct lc_time_T {
const char * mon[MONSPERYEAR];
const char * month[MONSPERYEAR];
@ -50,16 +57,9 @@ struct lc_time_T {
const char * pm;
const char * date_fmt;
};
#endif
#ifdef LOCALE_HOME
#include "sys/stat.h"
static struct lc_time_T localebuf;
static struct lc_time_T * _loc P((void));
#define Locale _loc()
#endif /* defined LOCALE_HOME */
#ifndef LOCALE_HOME
#define Locale (&C_time_locale)
#endif /* !defined LOCALE_HOME */
static const struct lc_time_T C_time_locale = {
{
@ -110,7 +110,7 @@ static const struct lc_time_T C_time_locale = {
static char * _add P((const char *, char *, const char *, int));
static char * _conv P((int, const char *, char *, const char *));
static char * _fmt P((const char *, const struct tm *, char *, const char *,
int *));
int *, const struct strftime_locale*));
static char * _yconv P((int, int, int, int, char *, const char *, int));
static char * getformat P((int, char *, char *, char *, char *));
@ -133,17 +133,25 @@ char * const s;
const size_t maxsize;
const char * const format;
const struct tm * const t;
{
return strftime_tz(s, maxsize, format, t, Locale);
}
size_t
strftime_tz(s, maxsize, format, t, locale)
char * const s;
const size_t maxsize;
const char * const format;
const struct tm * const t;
const struct strftime_locale *locale;
{
char * p;
int warn;
tzset();
#ifdef LOCALE_HOME
localebuf.mon[0] = 0;
#endif /* defined LOCALE_HOME */
warn = IN_NONE;
p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
#ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, locale);
#if 0 /* ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
(void) fprintf(stderr, "\n");
if (format == NULL)
@ -182,12 +190,13 @@ static char *getformat(int modifier, char *normal, char *underscore,
}
static char *
_fmt(format, t, pt, ptlim, warnp)
_fmt(format, t, pt, ptlim, warnp, locale)
const char * format;
const struct tm * const t;
char * pt;
const char * const ptlim;
int * warnp;
const struct strftime_locale* locale;
{
for ( ; *format; ++format) {
if (*format == '%') {
@ -200,26 +209,26 @@ label:
case 'A':
pt = _add((t->tm_wday < 0 ||
t->tm_wday >= DAYSPERWEEK) ?
"?" : Locale->weekday[t->tm_wday],
"?" : locale->weekday[t->tm_wday],
pt, ptlim, modifier);
continue;
case 'a':
pt = _add((t->tm_wday < 0 ||
t->tm_wday >= DAYSPERWEEK) ?
"?" : Locale->wday[t->tm_wday],
"?" : locale->wday[t->tm_wday],
pt, ptlim, modifier);
continue;
case 'B':
pt = _add((t->tm_mon < 0 ||
t->tm_mon >= MONSPERYEAR) ?
"?" : Locale->month[t->tm_mon],
"?" : locale->month[t->tm_mon],
pt, ptlim, modifier);
continue;
case 'b':
case 'h':
pt = _add((t->tm_mon < 0 ||
t->tm_mon >= MONSPERYEAR) ?
"?" : Locale->mon[t->tm_mon],
"?" : locale->mon[t->tm_mon],
pt, ptlim, modifier);
continue;
case 'C':
@ -237,7 +246,7 @@ label:
{
int warn2 = IN_SOME;
pt = _fmt(Locale->c_fmt, t, pt, ptlim, warnp);
pt = _fmt(locale->c_fmt, t, pt, ptlim, warnp, locale);
if (warn2 == IN_ALL)
warn2 = IN_THIS;
if (warn2 > *warnp)
@ -245,7 +254,7 @@ label:
}
continue;
case 'D':
pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, locale);
continue;
case 'd':
pt = _conv(t->tm_mday,
@ -279,7 +288,7 @@ label:
pt, ptlim);
continue;
case 'F':
pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, locale);
continue;
case 'H':
pt = _conv(t->tm_hour,
@ -356,21 +365,21 @@ label:
continue;
case 'p':
pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
Locale->pm :
Locale->am,
locale->pm :
locale->am,
pt, ptlim, modifier);
continue;
case 'P':
pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
Locale->pm :
Locale->am,
locale->pm :
locale->am,
pt, ptlim, FORCE_LOWER_CASE);
continue;
case 'R':
pt = _fmt("%H:%M", t, pt, ptlim, warnp);
pt = _fmt("%H:%M", t, pt, ptlim, warnp, locale);
continue;
case 'r':
pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp, locale);
continue;
case 'S':
pt = _conv(t->tm_sec,
@ -382,21 +391,21 @@ label:
{
struct tm tm;
char buf[INT_STRLEN_MAXIMUM(
time_t) + 1];
time_t mkt;
time64_t) + 1];
time64_t mkt;
tm = *t;
mkt = mktime(&tm);
if (TYPE_SIGNED(time_t))
(void) sprintf(buf, "%ld",
(long) mkt);
else (void) sprintf(buf, "%lu",
(unsigned long) mkt);
mkt = mktime64(&tm);
if (TYPE_SIGNED(time64_t))
(void) sprintf(buf, "%lld",
(long long) mkt);
else (void) sprintf(buf, "%llu",
(unsigned long long) mkt);
pt = _add(buf, pt, ptlim, modifier);
}
continue;
case 'T':
pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, locale);
continue;
case 't':
pt = _add("\t", pt, ptlim, modifier);
@ -517,7 +526,7 @@ label:
** "date as dd-bbb-YYYY"
** (ado, 1993-05-24)
*/
pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, locale);
continue;
case 'W':
pt = _conv((t->tm_yday + DAYSPERWEEK -
@ -532,13 +541,13 @@ label:
pt = _conv(t->tm_wday, "%d", pt, ptlim);
continue;
case 'X':
pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
pt = _fmt(locale->X_fmt, t, pt, ptlim, warnp, locale);
continue;
case 'x':
{
int warn2 = IN_SOME;
pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
pt = _fmt(locale->x_fmt, t, pt, ptlim, &warn2, locale);
if (warn2 == IN_ALL)
warn2 = IN_THIS;
if (warn2 > *warnp)
@ -627,8 +636,8 @@ label:
}
continue;
case '+':
pt = _fmt(Locale->date_fmt, t, pt, ptlim,
warnp);
pt = _fmt(locale->date_fmt, t, pt, ptlim,
warnp, locale);
continue;
case '%':
/*
@ -656,7 +665,7 @@ const char * const ptlim;
{
char buf[INT_STRLEN_MAXIMUM(int) + 1];
(void) sprintf(buf, format, n);
(void) snprintf(buf, sizeof(buf), format, n);
return _add(buf, pt, ptlim, 0);
}
@ -749,124 +758,3 @@ int modifier;
pt, ptlim);
return pt;
}
#ifdef LOCALE_HOME
static struct lc_time_T *
_loc P((void))
{
static const char locale_home[] = LOCALE_HOME;
static const char lc_time[] = "LC_TIME";
static char * locale_buf;
int fd;
int oldsun; /* "...ain't got nothin' to do..." */
char * lbuf;
char * name;
char * p;
const char ** ap;
const char * plim;
char filename[FILENAME_MAX];
struct stat st;
size_t namesize;
size_t bufsize;
/*
** Use localebuf.mon[0] to signal whether locale is already set up.
*/
if (localebuf.mon[0])
return &localebuf;
name = setlocale(LC_TIME, (char *) NULL);
if (name == NULL || *name == '\0')
goto no_locale;
/*
** If the locale name is the same as our cache, use the cache.
*/
lbuf = locale_buf;
if (lbuf != NULL && strcmp(name, lbuf) == 0) {
p = lbuf;
for (ap = (const char **) &localebuf;
ap < (const char **) (&localebuf + 1);
++ap)
*ap = p += strlen(p) + 1;
return &localebuf;
}
/*
** Slurp the locale file into the cache.
*/
namesize = strlen(name) + 1;
if (sizeof filename <
((sizeof locale_home) + namesize + (sizeof lc_time)))
goto no_locale;
oldsun = 0;
(void) sprintf(filename, "%s/%s/%s", locale_home, name, lc_time);
fd = open(filename, O_RDONLY);
if (fd < 0) {
/*
** Old Sun systems have a different naming and data convention.
*/
oldsun = 1;
(void) sprintf(filename, "%s/%s/%s", locale_home,
lc_time, name);
fd = open(filename, O_RDONLY);
if (fd < 0)
goto no_locale;
}
if (fstat(fd, &st) != 0)
goto bad_locale;
if (st.st_size <= 0)
goto bad_locale;
bufsize = namesize + st.st_size;
locale_buf = NULL;
lbuf = (lbuf == NULL) ? malloc(bufsize) : realloc(lbuf, bufsize);
if (lbuf == NULL)
goto bad_locale;
(void) strcpy(lbuf, name);
p = lbuf + namesize;
plim = p + st.st_size;
if (read(fd, p, (size_t) st.st_size) != st.st_size)
goto bad_lbuf;
if (close(fd) != 0)
goto bad_lbuf;
/*
** Parse the locale file into localebuf.
*/
if (plim[-1] != '\n')
goto bad_lbuf;
for (ap = (const char **) &localebuf;
ap < (const char **) (&localebuf + 1);
++ap) {
if (p == plim)
goto bad_lbuf;
*ap = p;
while (*p != '\n')
++p;
*p++ = '\0';
}
if (oldsun) {
/*
** SunOS 4 used an obsolescent format; see localdtconv(3).
** c_fmt had the ``short format for dates and times together''
** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
** date_fmt had the ``long format for dates''
** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
** Discard the latter in favor of the former.
*/
localebuf.date_fmt = localebuf.c_fmt;
}
/*
** Record the successful parse in the cache.
*/
locale_buf = lbuf;
return &localebuf;
bad_lbuf:
free(lbuf);
bad_locale:
(void) close(fd);
no_locale:
localebuf = C_time_locale;
locale_buf = NULL;
return &localebuf;
}
#endif /* defined LOCALE_HOME */

View File

@ -90,21 +90,21 @@ static const struct {
static int _conv_num(const unsigned char **, int *, int, int);
static char *_strptime(const char *, const char *, struct tm *, int);
static unsigned char *_strptime(const unsigned char *, const char *, struct tm *, int);
char *
strptime(const char *buf, const char *fmt, struct tm *tm)
{
return(_strptime(buf, fmt, tm, 1));
return (char*)(_strptime((const unsigned char*)buf, fmt, tm, 1));
}
static char *
_strptime(const char *buf, const char *fmt, struct tm *tm, int initialize)
static unsigned char *
_strptime(const unsigned char *buf, const char *fmt, struct tm *tm, int initialize)
{
unsigned char c;
const unsigned char *bp;
size_t len;
size_t len = 0;
int alt_format, i;
static int century, relyear;
@ -207,12 +207,12 @@ literal:
for (i = 0; i < 7; i++) {
/* Full name. */
len = strlen(_ctloc(day[i]));
if (strncasecmp(_ctloc(day[i]), bp, len) == 0)
if (strncasecmp(_ctloc(day[i]), (const char*)bp, len) == 0)
break;
/* Abbreviated name. */
len = strlen(_ctloc(abday[i]));
if (strncasecmp(_ctloc(abday[i]), bp, len) == 0)
if (strncasecmp(_ctloc(abday[i]), (const char*)bp, len) == 0)
break;
}
@ -231,12 +231,12 @@ literal:
for (i = 0; i < 12; i++) {
/* Full name. */
len = strlen(_ctloc(mon[i]));
if (strncasecmp(_ctloc(mon[i]), bp, len) == 0)
if (strncasecmp(_ctloc(mon[i]), (const char*)bp, len) == 0)
break;
/* Abbreviated name. */
len = strlen(_ctloc(abmon[i]));
if (strncasecmp(_ctloc(abmon[i]), bp, len) == 0)
if (strncasecmp(_ctloc(abmon[i]), (const char*)bp, len) == 0)
break;
}
@ -305,7 +305,7 @@ literal:
_LEGAL_ALT(0);
/* AM? */
len = strlen(_ctloc(am_pm[0]));
if (strncasecmp(_ctloc(am_pm[0]), bp, len) == 0) {
if (strncasecmp(_ctloc(am_pm[0]), (const char*)bp, len) == 0) {
if (tm->tm_hour > 12) /* i.e., 13:00 AM ?! */
return (NULL);
else if (tm->tm_hour == 12)
@ -316,7 +316,7 @@ literal:
}
/* PM? */
len = strlen(_ctloc(am_pm[1]));
if (strncasecmp(_ctloc(am_pm[1]), bp, len) == 0) {
if (strncasecmp(_ctloc(am_pm[1]), (const char*)bp, len) == 0) {
if (tm->tm_hour > 12) /* i.e., 13:00 PM ?! */
return (NULL);
else if (tm->tm_hour < 12)
@ -402,7 +402,7 @@ literal:
}
}
return ((char *)bp);
return (unsigned char*)bp;
}