⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsdate.c

📁 java script test programing source code
💻 C
📖 第 1 页 / 共 5 页
字号:
    intN result = (intN) fmod(floor(t / msPerSecond), SecondsPerMinute);    if (result < 0)        result += (intN)SecondsPerMinute;    return result;}static intNmsFromTime(jsdouble t){    intN result = (intN) fmod(t, msPerSecond);    if (result < 0)        result += (intN)msPerSecond;    return result;}#define TIMECLIP(d) ((JSDOUBLE_IS_FINITE(d) \                      && !((d < 0 ? -d : d) > HalfTimeDomain)) \                     ? js_DoubleToInteger(d + (+0.)) : *cx->runtime->jsNaN)/** * end of ECMA 'support' functions *//* * Other Support routines and definitions */JSClass js_DateClass = {    js_Date_str,    JSCLASS_HAS_PRIVATE | JSCLASS_HAS_CACHED_PROTO(JSProto_Date),    JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,    JS_EnumerateStub, JS_ResolveStub,   JS_ConvertStub,   JS_FinalizeStub,    JSCLASS_NO_OPTIONAL_MEMBERS};/* for use by date_parse */static const char* wtb[] = {    "am", "pm",    "monday", "tuesday", "wednesday", "thursday", "friday",    "saturday", "sunday",    "january", "february", "march", "april", "may", "june",    "july", "august", "september", "october", "november", "december",    "gmt", "ut", "utc",    "est", "edt",    "cst", "cdt",    "mst", "mdt",    "pst", "pdt"    /* time zone table needs to be expanded */};static int ttb[] = {    -1, -2, 0, 0, 0, 0, 0, 0, 0,       /* AM/PM */    2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,    10000 + 0, 10000 + 0, 10000 + 0,   /* GMT/UT/UTC */    10000 + 5 * 60, 10000 + 4 * 60,    /* EST/EDT */    10000 + 6 * 60, 10000 + 5 * 60,    /* CST/CDT */    10000 + 7 * 60, 10000 + 6 * 60,    /* MST/MDT */    10000 + 8 * 60, 10000 + 7 * 60     /* PST/PDT */};/* helper for date_parse */static JSBooldate_regionMatches(const char* s1, int s1off, const jschar* s2, int s2off,                   int count, int ignoreCase){    JSBool result = JS_FALSE;    /* return true if matches, otherwise, false */    while (count > 0 && s1[s1off] && s2[s2off]) {        if (ignoreCase) {            if (JS_TOLOWER((jschar)s1[s1off]) != JS_TOLOWER(s2[s2off])) {                break;            }        } else {            if ((jschar)s1[s1off] != s2[s2off]) {                break;            }        }        s1off++;        s2off++;        count--;    }    if (count == 0) {        result = JS_TRUE;    }    return result;}/* find UTC time from given date... no 1900 correction! */static jsdoubledate_msecFromDate(jsdouble year, jsdouble mon, jsdouble mday, jsdouble hour,                  jsdouble min, jsdouble sec, jsdouble msec){    jsdouble day;    jsdouble msec_time;    jsdouble result;    day = MakeDay(year, mon, mday);    msec_time = MakeTime(hour, min, sec, msec);    result = MakeDate(day, msec_time);    return result;}/* * See ECMA 15.9.4.[3-10]; *//* XXX this function must be above date_parseString to avoid a   horrid bug in the Win16 1.52 compiler */#define MAXARGS        7static JSBooldate_UTC(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){    jsdouble array[MAXARGS];    uintN loop;    jsdouble d;    for (loop = 0; loop < MAXARGS; loop++) {        if (loop < argc) {            if (!js_ValueToNumber(cx, argv[loop], &d))                return JS_FALSE;            /* return NaN if any arg is NaN */            if (!JSDOUBLE_IS_FINITE(d)) {                return js_NewNumberValue(cx, d, rval);            }            array[loop] = floor(d);        } else {            array[loop] = 0;        }    }    /* adjust 2-digit years into the 20th century */    if (array[0] >= 0 && array[0] <= 99)        array[0] += 1900;    /* if we got a 0 for 'date' (which is out of range)     * pretend it's a 1.  (So Date.UTC(1972, 5) works) */    if (array[2] < 1)        array[2] = 1;    d = date_msecFromDate(array[0], array[1], array[2],                              array[3], array[4], array[5], array[6]);    d = TIMECLIP(d);    return js_NewNumberValue(cx, d, rval);}static JSBooldate_parseString(JSString *str, jsdouble *result){    jsdouble msec;    const jschar *s = JSSTRING_CHARS(str);    size_t limit = JSSTRING_LENGTH(str);    size_t i = 0;    int year = -1;    int mon = -1;    int mday = -1;    int hour = -1;    int min = -1;    int sec = -1;    int c = -1;    int n = -1;    jsdouble tzoffset = -1;  /* was an int, overflowed on win16!!! */    int prevc = 0;    JSBool seenplusminus = JS_FALSE;    int temp;    JSBool seenmonthname = JS_FALSE;    if (limit == 0)        goto syntax;    while (i < limit) {        c = s[i];        i++;        if (c <= ' ' || c == ',' || c == '-') {            if (c == '-' && '0' <= s[i] && s[i] <= '9') {              prevc = c;            }            continue;        }        if (c == '(') { /* comments) */            int depth = 1;            while (i < limit) {                c = s[i];                i++;                if (c == '(') depth++;                else if (c == ')')                    if (--depth <= 0)                        break;            }            continue;        }        if ('0' <= c && c <= '9') {            n = c - '0';            while (i < limit && '0' <= (c = s[i]) && c <= '9') {                n = n * 10 + c - '0';                i++;            }            /* allow TZA before the year, so             * 'Wed Nov 05 21:49:11 GMT-0800 1997'             * works */            /* uses of seenplusminus allow : in TZA, so Java             * no-timezone style of GMT+4:30 works             */            if ((prevc == '+' || prevc == '-')/*  && year>=0 */) {                /* make ':' case below change tzoffset */                seenplusminus = JS_TRUE;                /* offset */                if (n < 24)                    n = n * 60; /* EG. "GMT-3" */                else                    n = n % 100 + n / 100 * 60; /* eg "GMT-0430" */                if (prevc == '+')       /* plus means east of GMT */                    n = -n;                if (tzoffset != 0 && tzoffset != -1)                    goto syntax;                tzoffset = n;            } else if (prevc == '/' && mon >= 0 && mday >= 0 && year < 0) {                if (c <= ' ' || c == ',' || c == '/' || i >= limit)                    year = n;                else                    goto syntax;            } else if (c == ':') {                if (hour < 0)                    hour = /*byte*/ n;                else if (min < 0)                    min = /*byte*/ n;                else                    goto syntax;            } else if (c == '/') {                /* until it is determined that mon is the actual                   month, keep it as 1-based rather than 0-based */                if (mon < 0)                    mon = /*byte*/ n;                else if (mday < 0)                    mday = /*byte*/ n;                else                    goto syntax;            } else if (i < limit && c != ',' && c > ' ' && c != '-' && c != '(') {                goto syntax;            } else if (seenplusminus && n < 60) {  /* handle GMT-3:30 */                if (tzoffset < 0)                    tzoffset -= n;                else                    tzoffset += n;            } else if (hour >= 0 && min < 0) {                min = /*byte*/ n;            } else if (prevc == ':' && min >= 0 && sec < 0) {                sec = /*byte*/ n;            } else if (mon < 0) {                mon = /*byte*/n;            } else if (mon >= 0 && mday < 0) {                mday = /*byte*/ n;            } else if (mon >= 0 && mday >= 0 && year < 0) {                year = n;            } else {                goto syntax;            }            prevc = 0;        } else if (c == '/' || c == ':' || c == '+' || c == '-') {            prevc = c;        } else {            size_t st = i - 1;            int k;            while (i < limit) {                c = s[i];                if (!(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')))                    break;                i++;            }            if (i <= st + 1)                goto syntax;            for (k = (sizeof(wtb)/sizeof(char*)); --k >= 0;)                if (date_regionMatches(wtb[k], 0, s, st, i-st, 1)) {                    int action = ttb[k];                    if (action != 0) {                        if (action < 0) {                            /*                             * AM/PM. Count 12:30 AM as 00:30, 12:30 PM as                             * 12:30, instead of blindly adding 12 if PM.                             */                            JS_ASSERT(action == -1 || action == -2);                            if (hour > 12 || hour < 0) {                                goto syntax;                            } else {                                if (action == -1 && hour == 12) { /* am */                                    hour = 0;                                } else if (action == -2 && hour != 12) { /* pm */                                    hour += 12;                                }                            }                        } else if (action <= 13) { /* month! */                            /* Adjust mon to be 1-based until the final values                               for mon, mday and year are adjusted below */                            if (seenmonthname) {                                goto syntax;                            }                            seenmonthname = JS_TRUE;                            temp = /*byte*/ (action - 2) + 1;                            if (mon < 0) {                                mon = temp;                            } else if (mday < 0) {                                mday = mon;                                mon = temp;                            } else if (year < 0) {                                year = mon;                                mon = temp;                            } else {                                goto syntax;                            }                        } else {                            tzoffset = action - 10000;                        }                    }                    break;                }            if (k < 0)                goto syntax;            prevc = 0;        }    }    if (year < 0 || mon < 0 || mday < 0)        goto syntax;    /*      Case 1. The input string contains an English month name.              The form of the string can be month f l, or f month l, or              f l month which each evaluate to the same date.              If f and l are both greater than or equal to 70, or              both less than 70, the date is invalid.              The year is taken to be the greater of the values f, l.              If the year is greater than or equal to 70 and less than 100,              it is considered to be the number of years after 1900.      Case 2. The input string is of the form "f/m/l" where f, m and l are              integers, e.g. 7/16/45.              Adjust the mon, mday and year values to achieve 100% MSIE              compatibility.              a. If 0 <= f < 70, f/m/l is interpreted as month/day/year.                 i.  If year < 100, it is the number of years after 1900                 ii. If year >= 100, it is the number of years after 0.              b. If 70 <= f < 100                 i.  If m < 70, f/m/l is interpreted as                     year/month/day where year is the number of years after                     1900.                 ii. If m >= 70, the date is invalid.              c. If f >= 100                 i.  If m < 70, f/m/l is interpreted as                     year/month/day where year is the number of years after 0.                 ii. If m >= 70, the date is invalid.    */    if (seenmonthname) {        if ((mday >= 70 && year >= 70) || (mday < 70 && year < 70)) {            goto syntax;        }        if (mday > year) {            temp = year;            year = mday;            mday = temp;        }        if (year >= 70 && year < 100) {            year += 1900;        }    } else if (mon < 70) { /* (a) month/day/year */        if (year < 100) {            year += 1900;        }    } else if (mon < 100) { /* (b) year/month/day */        if (mday < 70) {            temp = year;            year = mon + 1900;            mon = mday;            mday = temp;        } else {            goto syntax;        }    } else { /* (c) year/month/day */        if (mday < 70) {            temp = year;            year = mon;            mon = mday;            mday = temp;        } else {            goto syntax;        }    }    mon -= 1; /* convert month to 0-based */    if (sec < 0)        sec = 0;    if (min < 0)        min = 0;    if (hour < 0)        hour = 0;    if (tzoffset == -1) { /* no time zone specified, have to use local */        jsdouble msec_time;        msec_time = date_msecFromDate(year, mon, mday, hour, min, sec, 0);        *result = UTC(msec_time);        return JS_TRUE;    }    msec = date_msecFromDate(year, mon, mday, hour, min, sec, 0);    msec += tzoffset * msPerMinute;    *result = msec;    return JS_TRUE;syntax:    /* syntax error */    *result = 0;    return JS_FALSE;}static JSBooldate_parse(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){    JSString *str;    jsdouble result;    str = js_ValueToString(cx, argv[0]);    if (!str)        return JS_FALSE;    if (!date_parseString(str, &result)) {        *rval = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);        return JS_TRUE;    }    result = TIMECLIP(result);    return js_NewNumberValue(cx, result, rval);}static JSBooldate_now(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){    int64 us, ms, us2ms;    jsdouble msec_time;    us = PRMJ_Now();    JSLL_UI2L(us2ms, PRMJ_USEC_PER_MSEC);    JSLL_DIV(ms, us, us2ms);    JSLL_L2D(msec_time, ms);    return js_NewDoubleValue(cx, msec_time, rval);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -