📄 jsdate.c
字号:
}static JSBooldate_toLocaleDateString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ /* Use '%#x' for windows, because '%x' is * backward-compatible and non-y2k with msvc; '%#x' requests that a * full year be used in the result string. */ return date_toLocaleHelper(cx, obj, argc, argv, rval,#if defined(_WIN32) && !defined(__MWERKS__) "%#x"#else "%x"#endif );}static JSBooldate_toLocaleTimeString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ return date_toLocaleHelper(cx, obj, argc, argv, rval, "%X");}static JSBooldate_toLocaleFormat(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ JSString *fmt; if (argc == 0) return date_toLocaleString(cx, obj, argc, argv, rval); fmt = JS_ValueToString(cx, argv[0]); if (!fmt) return JS_FALSE; return date_toLocaleHelper(cx, obj, argc, argv, rval, JS_GetStringBytes(fmt));}static JSBooldate_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 JSBooldate_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#include <string.h>#include "jsdtoa.h"static JSBooldate_toSource(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ jsdouble *date; char buf[DTOSTR_STANDARD_BUFFER_SIZE], *numStr, *bytes; JSString *str; date = date_getProlog(cx, obj, argv); if (!date) return JS_FALSE; numStr = JS_dtostr(buf, sizeof buf, DTOSTR_STANDARD, 0, *date); if (!numStr) { JS_ReportOutOfMemory(cx); return JS_FALSE; } bytes = JS_smprintf("(new %s(%s))", js_Date_str, numStr); if (!bytes) { JS_ReportOutOfMemory(cx); return JS_FALSE; } str = JS_NewString(cx, bytes, strlen(bytes)); if (!str) { free(bytes); return JS_FALSE; } *rval = STRING_TO_JSVAL(str); return JS_TRUE;}#endifstatic JSBooldate_toString(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_FULL, rval);}static JSBooldate_valueOf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ /* It is an error to call date_valueOf on a non-date object, but we don't * need to check for that explicitly here because every path calls * date_getProlog, which does the check. */ /* If called directly with no arguments, convert to a time number. */ if (argc == 0) return date_getTime(cx, obj, argc, argv, rval); /* Convert to number only if the hint was given, otherwise favor string. */ if (argc == 1) { JSString *str, *str2; str = js_ValueToString(cx, argv[0]); if (!str) return JS_FALSE; str2 = ATOM_TO_STRING(cx->runtime->atomState.typeAtoms[JSTYPE_NUMBER]); if (js_EqualStrings(str, str2)) return date_getTime(cx, obj, argc, argv, rval); } return date_toString(cx, obj, argc, argv, rval);}/* * creation and destruction */static JSFunctionSpec date_static_methods[] = { {"UTC", date_UTC, MAXARGS,0,0 }, {"parse", date_parse, 1,0,0 }, {"now", date_now, 0,0,0 }, {0,0,0,0,0}};static JSFunctionSpec date_methods[] = { {"getTime", date_getTime, 0,0,0 }, {"getTimezoneOffset", date_getTimezoneOffset, 0,0,0 }, {"getYear", date_getYear, 0,0,0 }, {"getFullYear", date_getFullYear, 0,0,0 }, {"getUTCFullYear", date_getUTCFullYear, 0,0,0 }, {"getMonth", date_getMonth, 0,0,0 }, {"getUTCMonth", date_getUTCMonth, 0,0,0 }, {"getDate", date_getDate, 0,0,0 }, {"getUTCDate", date_getUTCDate, 0,0,0 }, {"getDay", date_getDay, 0,0,0 }, {"getUTCDay", date_getUTCDay, 0,0,0 }, {"getHours", date_getHours, 0,0,0 }, {"getUTCHours", date_getUTCHours, 0,0,0 }, {"getMinutes", date_getMinutes, 0,0,0 }, {"getUTCMinutes", date_getUTCMinutes, 0,0,0 }, {"getSeconds", date_getUTCSeconds, 0,0,0 }, {"getUTCSeconds", date_getUTCSeconds, 0,0,0 }, {"getMilliseconds", date_getUTCMilliseconds,0,0,0 }, {"getUTCMilliseconds", date_getUTCMilliseconds,0,0,0 }, {"setTime", date_setTime, 1,0,0 }, {"setYear", date_setYear, 1,0,0 }, {"setFullYear", date_setFullYear, 3,0,0 }, {"setUTCFullYear", date_setUTCFullYear, 3,0,0 }, {"setMonth", date_setMonth, 2,0,0 }, {"setUTCMonth", date_setUTCMonth, 2,0,0 }, {"setDate", date_setDate, 1,0,0 }, {"setUTCDate", date_setUTCDate, 1,0,0 }, {"setHours", date_setHours, 4,0,0 }, {"setUTCHours", date_setUTCHours, 4,0,0 }, {"setMinutes", date_setMinutes, 3,0,0 }, {"setUTCMinutes", date_setUTCMinutes, 3,0,0 }, {"setSeconds", date_setSeconds, 2,0,0 }, {"setUTCSeconds", date_setUTCSeconds, 2,0,0 }, {"setMilliseconds", date_setMilliseconds, 1,0,0 }, {"setUTCMilliseconds", date_setUTCMilliseconds,1,0,0 }, {"toUTCString", date_toGMTString, 0,0,0 }, {js_toLocaleString_str, date_toLocaleString, 0,0,0 }, {"toLocaleDateString", date_toLocaleDateString,0,0,0 }, {"toLocaleTimeString", date_toLocaleTimeString,0,0,0 }, {"toLocaleFormat", date_toLocaleFormat, 1,0,0 }, {"toDateString", date_toDateString, 0,0,0 }, {"toTimeString", date_toTimeString, 0,0,0 },#if JS_HAS_TOSOURCE {js_toSource_str, date_toSource, 0,0,0 },#endif {js_toString_str, date_toString, 0,0,0 }, {js_valueOf_str, date_valueOf, 0,0,0 }, {0,0,0,0,0}};static jsdouble *date_constructor(JSContext *cx, JSObject* obj){ jsdouble *date; date = js_NewDouble(cx, 0.0, 0); if (!date) return NULL; OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, DOUBLE_TO_JSVAL(date)); return date;}static JSBoolDate(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){ jsdouble *date; JSString *str; jsdouble d; /* Date called as function. */ if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) { int64 us, ms, us2ms; jsdouble msec_time; /* NSPR 2.0 docs say 'We do not support PRMJ_NowMS and PRMJ_NowS', * so compute ms from PRMJ_Now. */ us = PRMJ_Now(); JSLL_UI2L(us2ms, PRMJ_USEC_PER_MSEC); JSLL_DIV(ms, us, us2ms); JSLL_L2D(msec_time, ms); return date_format(cx, msec_time, FORMATSPEC_FULL, rval); } /* Date called as constructor. */ if (argc == 0) { int64 us, ms, us2ms; jsdouble msec_time; date = date_constructor(cx, obj); if (!date) return JS_FALSE; us = PRMJ_Now(); JSLL_UI2L(us2ms, PRMJ_USEC_PER_MSEC); JSLL_DIV(ms, us, us2ms); JSLL_L2D(msec_time, ms); *date = msec_time; } else if (argc == 1) { if (!JSVAL_IS_STRING(argv[0])) { /* the argument is a millisecond number */ if (!js_ValueToNumber(cx, argv[0], &d)) return JS_FALSE; date = date_constructor(cx, obj); if (!date) return JS_FALSE; *date = TIMECLIP(d); } else { /* the argument is a string; parse it. */ date = date_constructor(cx, obj); if (!date) return JS_FALSE; str = js_ValueToString(cx, argv[0]); if (!str) return JS_FALSE; if (!date_parseString(str, date)) *date = *cx->runtime->jsNaN; *date = TIMECLIP(*date); } } else { jsdouble array[MAXARGS]; uintN loop; jsdouble double_arg; jsdouble day; jsdouble msec_time; for (loop = 0; loop < MAXARGS; loop++) { if (loop < argc) { if (!js_ValueToNumber(cx, argv[loop], &double_arg)) return JS_FALSE; /* if any arg is NaN, make a NaN date object and return */ if (!JSDOUBLE_IS_FINITE(double_arg)) { date = date_constructor(cx, obj); if (!date) return JS_FALSE; *date = *cx->runtime->jsNaN; return JS_TRUE; } array[loop] = js_DoubleToInteger(double_arg); } else { if (loop == 2) { array[loop] = 1; /* Default the date argument to 1. */ } else { array[loop] = 0; } } } date = date_constructor(cx, obj); if (!date) return JS_FALSE; /* adjust 2-digit years into the 20th century */ if (array[0] >= 0 && array[0] <= 99) array[0] += 1900; day = MakeDay(array[0], array[1], array[2]); msec_time = MakeTime(array[3], array[4], array[5], array[6]); msec_time = MakeDate(day, msec_time); msec_time = UTC(msec_time); *date = TIMECLIP(msec_time); } return JS_TRUE;}JSObject *js_InitDateClass(JSContext *cx, JSObject *obj){ JSObject *proto; jsdouble *proto_date; /* set static LocalTZA */ LocalTZA = -(PRMJ_LocalGMTDifference() * msPerSecond); proto = JS_InitClass(cx, obj, NULL, &js_DateClass, Date, MAXARGS, NULL, date_methods, NULL, date_static_methods); if (!proto) return NULL; /* Alias toUTCString with toGMTString. (ECMA B.2.6) */ if (!JS_AliasProperty(cx, proto, "toUTCString", "toGMTString")) return NULL; /* Set the value of the Date.prototype date to NaN */ proto_date = date_constructor(cx, proto); if (!proto_date) return NULL; *proto_date = *cx->runtime->jsNaN; return proto;}JS_FRIEND_API(JSObject *)js_NewDateObjectMsec(JSContext *cx, jsdouble msec_time){ JSObject *obj; jsdouble *date; obj = js_NewObject(cx, &js_DateClass, NULL, NULL); if (!obj) return NULL; date = date_constructor(cx, obj); if (!date) return NULL; *date = msec_time; return obj;}JS_FRIEND_API(JSObject *)js_NewDateObject(JSContext* cx, int year, int mon, int mday, int hour, int min, int sec){ JSObject *obj; jsdouble msec_time; msec_time = date_msecFromDate(year, mon, mday, hour, min, sec, 0); obj = js_NewDateObjectMsec(cx, UTC(msec_time)); return obj;}JS_FRIEND_API(JSBool)js_DateIsValid(JSContext *cx, JSObject* obj){ jsdouble *date = date_getProlog(cx, obj, NULL); if (!date || JSDOUBLE_IS_NaN(*date)) return JS_FALSE; else return JS_TRUE;}JS_FRIEND_API(int)js_DateGetYear(JSContext *cx, JSObject* obj){ jsdouble *date = date_getProlog(cx, obj, NULL); /* Preserve legacy API behavior of returning 0 for invalid dates. */ if (!date || JSDOUBLE_IS_NaN(*date)) return 0; return (int) YearFromTime(LocalTime(*date));}JS_FRIEND_API(int)js_DateGetMonth(JSContext *cx, JSObject* obj){ jsdouble *date = date_getProlog(cx, obj, NULL); if (!date || JSDOUBLE_IS_NaN(*date)) return 0; return (int) MonthFromTime(LocalTime(*date));}JS_FRIEND_API(int)js_DateGetDate(JSContext *cx, JSObject* obj){ jsdouble *date = date_getProlog(cx, obj, NULL); if (!date || JSDOUBLE_IS_NaN(*date)) return 0; return (int) DateFromTime(LocalTime(*date));}JS_FRIEND_API(int)js_DateGetHours(JSContext *cx, JSObject* obj){ jsdouble *date = date_getProlog(cx, obj, NULL); if (!date || JSDOUBLE_IS_NaN(*date)) return 0; return (int) HourFromTime(LocalTime(*date));}JS_FRIEND_API(int)js_DateGetMinutes(JSContext *cx, JSObject* obj){ jsdouble *date = date_getProlog(cx, obj, NULL); if (!date || JSDOUBLE_IS_NaN(*date)) return 0; return (int) MinFromTime(LocalTime(*date));}JS_FRIEND_API(int)js_DateGetSeconds(JSContext *cx, JSObject* obj){ jsdouble *date = date_getProlog(cx, obj, NULL); if (!date || JSDOUBLE_IS_NaN(*date)) re
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -