Fixes for getting the timezone string fot toString, and for asking for

a localized string from the os for toLocaleString.  The time struct
used to interface to the os time-formatting functions only takes a
16-bit year, so we map to an equivalent year (for getting the timezone
string) or clamp for years outside that range.
This commit is contained in:
mccabe 1998-04-30 00:27:43 +00:00
parent b7ca0cd7fd
commit 6398680e7f
2 changed files with 58 additions and 10 deletions

View File

@ -1391,11 +1391,35 @@ date_toGMTString(JSContext *cx, JSObject *obj, uintN argc,
return JS_TRUE;
}
/* for Date.toLocaleString; interface to PRTime date struct. */
/* for Date.toLocaleString; interface to PRTime 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, PRTime *split)
new_explode(jsdouble time, PRTime *split, JSBool findEquivalent)
{
jsint year = YearFromTime(time);
int16 adjustedYear;
/* If the year doesn't fit in a PRTime, 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.
*/
int 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, PRTime *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
@ -1427,7 +1451,7 @@ date_toLocaleString(JSContext *cx, JSObject *obj, uintN argc,
PR_snprintf(buf, sizeof buf, js_NaN_date_str);
} else {
jsdouble local = LocalTime(*date);
new_explode(local, &split);
new_explode(local, &split, JS_FALSE);
/* let PRTime format it. Use '%#c' for windows, because '%c' is
* backward-compatible and non-y2k with msvc; '%#c' requests that a
@ -1482,7 +1506,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);
PR_FormatTime(tzbuf, sizeof tzbuf, "(%Z) ", &split);
/* Avoid dependence on PR_FormatTimeUSEnglish, because it

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.
*/
int 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
@ -1427,7 +1451,7 @@ date_toLocaleString(JSContext *cx, JSObject *obj, uintN argc,
PR_snprintf(buf, sizeof buf, js_NaN_date_str);
} else {
jsdouble local = LocalTime(*date);
new_explode(local, &split);
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
@ -1482,7 +1506,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