Fix to 58007. Add Date.prototype.toDateString, toTimeString methods, by commoning existing date_format helper function with toString. ECMA-262 ed. 3 conformance.

r=brendan@mozilla.org
sr=jband@mozilla.org
This commit is contained in:
mccabe%netscape.com 2000-11-01 23:57:37 +00:00
Родитель 6dde92b825
Коммит 980b771c8d
1 изменённых файлов: 70 добавлений и 18 удалений

Просмотреть файл

@ -1491,9 +1491,13 @@ new_explode(jsdouble timeval, PRMJTime *split, JSBool findEquivalent)
split->tm_isdst = (DaylightSavingTA(timeval) != 0); split->tm_isdst = (DaylightSavingTA(timeval) != 0);
} }
typedef enum formatspec {
FORMATSPEC_FULL, FORMATSPEC_DATE, FORMATSPEC_TIME
} formatspec;
/* helper function */ /* helper function */
static JSBool static JSBool
date_format(JSContext *cx, jsdouble date, jsval *rval) date_format(JSContext *cx, jsdouble date, formatspec format, jsval *rval)
{ {
char buf[100]; char buf[100];
JSString *str; JSString *str;
@ -1527,7 +1531,7 @@ date_format(JSContext *cx, jsdouble date, jsval *rval)
/* get a timezone string from the OS to include as a /* get a timezone string from the OS to include as a
comment. */ comment. */
new_explode(date, &split, JS_TRUE); new_explode(date, &split, JS_TRUE);
PRMJ_FormatTime(tzbuf, sizeof tzbuf, "(%Z) ", &split); PRMJ_FormatTime(tzbuf, sizeof tzbuf, "(%Z)", &split);
/* Decide whether to use the resulting timezone string. /* Decide whether to use the resulting timezone string.
* *
@ -1552,19 +1556,45 @@ date_format(JSContext *cx, jsdouble date, jsval *rval)
if (tzbuf[0] != '(' || tzbuf[1] == ')') if (tzbuf[0] != '(' || tzbuf[1] == ')')
usetz = JS_FALSE; usetz = JS_FALSE;
/* Avoid dependence on PRMJ_FormatTimeUSEnglish, because it if (format == FORMATSPEC_FULL) {
* requires a PRMJTime... which only has 16-bit years. Sub-ECMA. /*
*/ * Avoid dependence on PRMJ_FormatTimeUSEnglish, because it
JS_snprintf(buf, sizeof buf, "%s %s %.2d %.2d:%.2d:%.2d GMT%+.4d %s%.4d", * requires a PRMJTime... which only has 16-bit years. Sub-ECMA.
days[WeekDay(local)], */
months[MonthFromTime(local)], /* Tue Oct 31 09:41:40 GMT-0800 (PST) 2000 */
DateFromTime(local), JS_snprintf(buf, sizeof buf,
HourFromTime(local), "%s %s %.2d %.2d:%.2d:%.2d GMT%+.4d %s%s%.4d",
MinFromTime(local), days[WeekDay(local)],
SecFromTime(local), months[MonthFromTime(local)],
offset, DateFromTime(local),
usetz ? tzbuf : "", HourFromTime(local),
YearFromTime(local)); MinFromTime(local),
SecFromTime(local),
offset,
usetz ? tzbuf : "",
usetz ? " " : "",
YearFromTime(local));
} else if (format == FORMATSPEC_DATE) {
/* Tue Oct 31 2000 */
JS_snprintf(buf, sizeof buf,
"%s %s %.2d %.4d",
days[WeekDay(local)],
months[MonthFromTime(local)],
DateFromTime(local),
YearFromTime(local));
} else if (format == FORMATSPEC_TIME) {
/* 09:41:40 GMT-0800 (PST) */
JS_snprintf(buf, sizeof buf,
"%.2d:%.2d:%.2d GMT%+.4d%s%s",
HourFromTime(local),
MinFromTime(local),
SecFromTime(local),
offset,
usetz ? " " : "",
usetz ? tzbuf : "");
} else {
return JS_FALSE;
}
} }
str = JS_NewStringCopyZ(cx, buf); str = JS_NewStringCopyZ(cx, buf);
@ -1597,7 +1627,7 @@ date_toLocaleHelper(JSContext *cx, JSObject *obj, uintN argc,
/* If it failed, default to toString. */ /* If it failed, default to toString. */
if (result_len == 0) if (result_len == 0)
return date_format(cx, *date, rval); return date_format(cx, *date, FORMATSPEC_FULL, rval);
} }
str = JS_NewStringCopyZ(cx, buf); str = JS_NewStringCopyZ(cx, buf);
@ -1648,6 +1678,26 @@ date_toLocaleTimeString(JSContext *cx, JSObject *obj, uintN argc,
return date_toLocaleHelper(cx, obj, argc, argv, rval, "%X"); return date_toLocaleHelper(cx, obj, argc, argv, rval, "%X");
} }
static JSBool
date_toTimeString(JSContext *cx, JSObject *obj, uintN argc,
jsval *argv, jsval *rval)
{
jsdouble *date = date_getProlog(cx, obj, argv);
if (!date)
return JS_FALSE;
return date_format(cx, *date, FORMATSPEC_TIME, rval);
}
static JSBool
date_toDateString(JSContext *cx, JSObject *obj, uintN argc,
jsval *argv, jsval *rval)
{
jsdouble *date = date_getProlog(cx, obj, argv);
if (!date)
return JS_FALSE;
return date_format(cx, *date, FORMATSPEC_DATE, rval);
}
#if JS_HAS_TOSOURCE #if JS_HAS_TOSOURCE
#include <string.h> #include <string.h>
#include "jsdtoa.h" #include "jsdtoa.h"
@ -1693,7 +1743,7 @@ date_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsdouble *date = date_getProlog(cx, obj, argv); jsdouble *date = date_getProlog(cx, obj, argv);
if (!date) if (!date)
return JS_FALSE; return JS_FALSE;
return date_format(cx, *date, rval); return date_format(cx, *date, FORMATSPEC_FULL, rval);
} }
#if JS_HAS_VALUEOF_HINT #if JS_HAS_VALUEOF_HINT
@ -1779,6 +1829,8 @@ static JSFunctionSpec date_methods[] = {
{js_toLocaleString_str, date_toLocaleString, 0,0,0 }, {js_toLocaleString_str, date_toLocaleString, 0,0,0 },
{"toLocaleDateString", date_toLocaleDateString,0,0,0 }, {"toLocaleDateString", date_toLocaleDateString,0,0,0 },
{"toLocaleTimeString", date_toLocaleTimeString,0,0,0 }, {"toLocaleTimeString", date_toLocaleTimeString,0,0,0 },
{"toDateString", date_toDateString, 0,0,0 },
{"toTimeString", date_toTimeString, 0,0,0 },
#if JS_HAS_TOSOURCE #if JS_HAS_TOSOURCE
{js_toSource_str, date_toSource, 0,0,0 }, {js_toSource_str, date_toSource, 0,0,0 },
#endif #endif
@ -1819,7 +1871,7 @@ Date(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
JSLL_DIV(ms, us, us2ms); JSLL_DIV(ms, us, us2ms);
JSLL_L2D(msec_time, ms); JSLL_L2D(msec_time, ms);
return date_format(cx, msec_time, rval); return date_format(cx, msec_time, FORMATSPEC_FULL, rval);
} }
/* Date called as constructor */ /* Date called as constructor */