Restoring changes that were backed out because of a one-line error in

prmjtime.c.
This commit is contained in:
mccabe 1998-05-01 00:22:06 +00:00
parent 4cc9f5cf4a
commit f8b9b7e131
4 changed files with 81 additions and 58 deletions

View File

@ -1391,11 +1391,35 @@ date_toGMTString(JSContext *cx, JSObject *obj, uintN argc,
return JS_TRUE;
}
/* for Date.toLocaleString; interface to PRMJTime date struct. */
/* for Date.toLocaleString; interface to PRMJTime date struct.
* If findEquivalent is true, then try to map the year to an equivalent year
* that's in range.
*/
static void
new_explode(jsdouble time, PRMJTime *split)
new_explode(jsdouble time, PRMJTime *split, JSBool findEquivalent)
{
jsint year = YearFromTime(time);
int16 adjustedYear;
/* If the year doesn't fit in a PRMJTime, find something to do about it. */
if (year > 32767 || year < -32768) {
if (findEquivalent) {
/* We're really just trying to get a timezone string; map the year
* to some equivalent year in the range 0 to 2800. Borrowed from
* A. D. Olsen.
*/
jsint cycles;
#define CYCLE_YEARS 2800L
cycles = (year >= 0) ? year / CYCLE_YEARS
: -1 - (-1 - year) / CYCLE_YEARS;
adjustedYear = year - cycles * CYCLE_YEARS;
} else {
/* Clamp it to the nearest representable year. */
adjustedYear = (year > 0) ? 32767 : - 32768;
}
} else {
adjustedYear = (int16)year;
}
split->tm_usec = (int32) msFromTime(time) * 1000;
split->tm_sec = (int8) SecFromTime(time);
@ -1404,7 +1428,7 @@ new_explode(jsdouble time, PRMJTime *split)
split->tm_mday = (int8) DateFromTime(time);
split->tm_mon = (int8) MonthFromTime(time);
split->tm_wday = (int8) WeekDay(time);
split->tm_year = (int16) year;
split->tm_year = (int16) adjustedYear;
split->tm_yday = (int16) DayWithinYear(time, year);
/* not sure how this affects things, but it doesn't seem
@ -1412,43 +1436,6 @@ new_explode(jsdouble time, PRMJTime *split)
split->tm_isdst = (DaylightSavingTA(time) != 0);
}
static JSBool
date_toLocaleString(JSContext *cx, JSObject *obj, uintN argc,
jsval *argv, jsval *rval)
{
char buf[100];
JSString *str;
PRMJTime split;
jsdouble *date = date_getProlog(cx, obj, argv);
if (!date)
return JS_FALSE;
if (!JSDOUBLE_IS_FINITE(*date)) {
PR_snprintf(buf, sizeof buf, js_NaN_date_str);
} else {
jsdouble local = LocalTime(*date);
new_explode(local, &split);
/* let PRMJTime format it. Use '%#c' for windows, because '%c' is
* backward-compatible and non-y2k with msvc; '%#c' requests that a
* full year be used in the result string.
*/
PRMJ_FormatTime(buf, sizeof buf,
#ifdef _WIN32
"%#c",
#else
"%c",
#endif
&split);
}
str = JS_NewStringCopyZ(cx, buf);
if (!str)
return JS_FALSE;
*rval = STRING_TO_JSVAL(str);
return JS_TRUE;
}
/* helper function */
static JSBool
date_format(JSContext *cx, jsdouble date, jsval *rval)
@ -1482,7 +1469,7 @@ date_format(JSContext *cx, jsdouble date, jsval *rval)
/* get a timezone string from the OS to include as a
comment. */
new_explode(date, &split);
new_explode(date, &split, JS_TRUE);
PRMJ_FormatTime(tzbuf, sizeof tzbuf, "(%Z) ", &split);
/* Avoid dependence on PRMJ_FormatTimeUSEnglish, because it
@ -1512,6 +1499,48 @@ date_format(JSContext *cx, jsdouble date, jsval *rval)
return JS_TRUE;
}
static JSBool
date_toLocaleString(JSContext *cx, JSObject *obj, uintN argc,
jsval *argv, jsval *rval)
{
char buf[100];
JSString *str;
PRMJTime split;
jsdouble *date = date_getProlog(cx, obj, argv);
if (!date)
return JS_FALSE;
if (!JSDOUBLE_IS_FINITE(*date)) {
PR_snprintf(buf, sizeof buf, js_NaN_date_str);
} else {
intN result_len;
jsdouble local = LocalTime(*date);
new_explode(local, &split, JS_FALSE);
/* let PRMJTime format it. Use '%#c' for windows, because '%c' is
* backward-compatible and non-y2k with msvc; '%#c' requests that a
* full year be used in the result string.
*/
result_len = PRMJ_FormatTime(buf, sizeof buf,
#ifdef _WIN32
"%#c",
#else
"%c",
#endif
&split);
/* If it failed, default to toString. */
if (result_len == 0)
return date_format(cx, *date, rval);
}
str = JS_NewStringCopyZ(cx, buf);
if (!str)
return JS_FALSE;
*rval = STRING_TO_JSVAL(str);
return JS_TRUE;
}
#if JS_HAS_TOSOURCE
#include "prdtoa.h"

View File

@ -6,16 +6,10 @@ s/include "prassert.h"/include "prlog.h"/g
s/include "prprintf.h"/include "prprf.h"/g
s/include "prtime.h"/include "prmjtime.h"/g
s/PRTime/PRMJTime/g
s/PR_ExplodeTime/PRMJ_ExplodeTime/g
s/PR_ComputeTime/PRMJ_ComputeTime/g
s/PR_ToLocal/PRMJ_ToLocal/g
s/PR_ToGMT/PRMJ_ToGMT/g
s/PR_DSTOffset/PRMJ_DSTOffset/g
s/PR_FormatTimeUSEnglish/PRMJ_FormatTimeUSEnglish/g
s/PR_FormatTime/PRMJ_FormatTime/g
s/PR_USEC_PER_SEC/PRMJ_USEC_PER_SEC/g
s/PR_USEC_PER_MSEC/PRMJ_USEC_PER_MSEC/g
s/PR_gmtime/PRMJ_gmtime/g
s/PR_Now/PRMJ_Now/g
s/PR_LocalGMTDifference/PRMJ_LocalGMTDifference/g

View File

@ -158,10 +158,6 @@ PRMJ_LocalGMTDifference()
#define G2037GMTMICROHI 0x00e45fab /* micro secs to 2037 high */
#define G2037GMTMICROLOW 0x7a238000 /* micro secs to 2037 low */
/* Convert from extended time to base time (time since Jan 1 1970) it
* truncates dates if time is before 1970 and after 2037.
*/
/* Convert from base time to extended time */
PR_IMPLEMENT(PRInt64)
PRMJ_ToExtendedTime(PRInt32 time)
@ -287,10 +283,10 @@ PRMJ_DSTOffset(PRInt64 time)
PRInt32 diff;
PRInt64 maxtimet;
struct tm tm;
PRMJTime prtm;
#if defined( XP_PC ) || defined( FREEBSD ) || defined ( HPUX9 )
struct tm *ptm;
#endif
PRMJTime prtm;
LL_UI2L(us2s, PRMJ_USEC_PER_SEC);
@ -372,12 +368,19 @@ PRMJ_FormatTime(char *buf, int buflen, char *fmt, PRMJTime *prtm)
* Still not sure if MKLINUX is necessary; this is borrowed from the NSPR20
* prtime.c. I'm leaving it out - My Linux does the right thing without it
* (and the wrong thing with it) even though it has the tm_gmtoff, tm_zone
* fields.
* fields. Linux seems to be happy so long as the tm struct is zeroed out.
* The #ifdef in nspr is:
* #if defined(SUNOS4) || defined(MKLINUX) || defined (__GLIBC >= 2)
*/
#if defined(SUNOS4)
if (mktime(&a) == -1) {
PR_snprintf(buf, buflen, "can't get timezone");
/* Seems to fail whenever the requested date is outside of the 32-bit
* UNIX epoch. We could proceed at this point (setting a.tm_zone to
* "") but then strftime returns a string with a 2-digit field of
* garbage for the year. So we return 0 and hope jsdate.c
* will fall back on toString.
*/
return 0;
}
#endif

View File

@ -49,11 +49,8 @@ struct PRMJTime {
};
/* Some handy constants */
#define PRMJ_MSEC_PER_SEC 1000
#define PRMJ_USEC_PER_SEC 1000000L
#define PRMJ_NSEC_PER_SEC 1000000000L
#define PRMJ_USEC_PER_MSEC 1000
#define PRMJ_NSEC_PER_MSEC 1000000L
#define PRMJ_USEC_PER_MSEC 1000L
/* Return the current local time in micro-seconds */
extern PR_IMPLEMENT(PRInt64)