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

📄 nativedate.java

📁 java中比较著名的js引擎当属mozilla开源的rhino
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                }                continue;            }            if (c == '(') { /* comments) */                int depth = 1;                while (i < limit) {                    c = s.charAt(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.charAt(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 = 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)                        return ScriptRuntime.NaN;                    tzoffset = n;                } else if (n >= 70  ||                           (prevc == '/' && mon >= 0 && mday >= 0                            && year < 0))                {                    if (year >= 0)                        return ScriptRuntime.NaN;                    else if (c <= ' ' || c == ',' || c == '/' || i >= limit)                        year = n < 100 ? n + 1900 : n;                    else                        return ScriptRuntime.NaN;                } else if (c == ':') {                    if (hour < 0)                        hour = /*byte*/ n;                    else if (min < 0)                        min = /*byte*/ n;                    else                        return ScriptRuntime.NaN;                } else if (c == '/') {                    if (mon < 0)                        mon = /*byte*/ n-1;                    else if (mday < 0)                        mday = /*byte*/ n;                    else                        return ScriptRuntime.NaN;                } else if (i < limit && c != ',' && c > ' ' && c != '-') {                    return ScriptRuntime.NaN;                } 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 (min >= 0 && sec < 0) {                    sec = /*byte*/ n;                } else if (mday < 0) {                    mday = /*byte*/ n;                } else {                    return ScriptRuntime.NaN;                }                prevc = 0;            } else if (c == '/' || c == ':' || c == '+' || c == '-') {                prevc = c;            } else {                int st = i - 1;                while (i < limit) {                    c = s.charAt(i);                    if (!(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')))                        break;                    i++;                }                int letterCount = i - st;                if (letterCount < 2)                    return ScriptRuntime.NaN;               /*                * Use ported code from jsdate.c rather than the locale-specific                * date-parsing code from Java, to keep js and rhino consistent.                * Is this the right strategy?                */                String 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;";                int index = 0;                for (int wtbOffset = 0; ;) {                    int wtbNext = wtb.indexOf(';', wtbOffset);                    if (wtbNext < 0)                        return ScriptRuntime.NaN;                    if (wtb.regionMatches(true, wtbOffset, s, st, letterCount))                        break;                    wtbOffset = wtbNext + 1;                    ++index;                }                if (index < 2) {                    /*                     * AM/PM. Count 12:30 AM as 00:30, 12:30 PM as                     * 12:30, instead of blindly adding 12 if PM.                     */                    if (hour > 12 || hour < 0) {                        return ScriptRuntime.NaN;                    } else if (index == 0) {                        // AM                        if (hour == 12)                            hour = 0;                    } else {                        // PM                        if (hour != 12)                            hour += 12;                    }                } else if ((index -= 2) < 7) {                    // ignore week days                } else if ((index -= 7) < 12) {                    // month                    if (mon < 0) {                        mon = index;                    } else {                        return ScriptRuntime.NaN;                    }                } else {                    index -= 12;                    // timezones                    switch (index) {                      case 0 /* gmt */: tzoffset = 0; break;                      case 1 /* ut */:  tzoffset = 0; break;                      case 2 /* utc */: tzoffset = 0; break;                      case 3 /* est */: tzoffset = 5 * 60; break;                      case 4 /* edt */: tzoffset = 4 * 60; break;                      case 5 /* cst */: tzoffset = 6 * 60; break;                      case 6 /* cdt */: tzoffset = 5 * 60; break;                      case 7 /* mst */: tzoffset = 7 * 60; break;                      case 8 /* mdt */: tzoffset = 6 * 60; break;                      case 9 /* pst */: tzoffset = 8 * 60; break;                      case 10 /* pdt */:tzoffset = 7 * 60; break;                      default: Kit.codeBug();                    }                }            }        }        if (year < 0 || mon < 0 || mday < 0)            return ScriptRuntime.NaN;        if (sec < 0)            sec = 0;        if (min < 0)            min = 0;        if (hour < 0)            hour = 0;        double msec = date_msecFromDate(year, mon, mday, hour, min, sec, 0);        if (tzoffset == -1) { /* no time zone specified, have to use local */            return internalUTC(msec);        } else {            return msec + tzoffset * msPerMinute;        }    }    private static String date_format(double t, int methodId)    {        StringBuffer result = new StringBuffer(60);        double local = LocalTime(t);        /* Tue Oct 31 09:41:40 GMT-0800 (PST) 2000 */        /* Tue Oct 31 2000 */        /* 09:41:40 GMT-0800 (PST) */        if (methodId != Id_toTimeString) {            appendWeekDayName(result, WeekDay(local));            result.append(' ');            appendMonthName(result, MonthFromTime(local));            result.append(' ');            append0PaddedUint(result, DateFromTime(local), 2);            result.append(' ');            int year = YearFromTime(local);            if (year < 0) {                result.append('-');                year = -year;            }            append0PaddedUint(result, year, 4);            if (methodId != Id_toDateString)                result.append(' ');        }        if (methodId != Id_toDateString) {            append0PaddedUint(result, HourFromTime(local), 2);            result.append(':');            append0PaddedUint(result, MinFromTime(local), 2);            result.append(':');            append0PaddedUint(result, SecFromTime(local), 2);            // offset from GMT in minutes.  The offset includes daylight            // savings, if it applies.            int minutes = (int) Math.floor((LocalTZA + DaylightSavingTA(t))                                           / msPerMinute);            // map 510 minutes to 0830 hours            int offset = (minutes / 60) * 100 + minutes % 60;            if (offset > 0) {                result.append(" GMT+");            } else {                result.append(" GMT-");                offset = -offset;            }            append0PaddedUint(result, offset, 4);            if (timeZoneFormatter == null)                timeZoneFormatter = new java.text.SimpleDateFormat("zzz");            // Find an equivalent year before getting the timezone            // comment.  See DaylightSavingTA.            if (t < 0.0 || t > 2145916800000.0) {                int equiv = EquivalentYear(YearFromTime(local));                double day = MakeDay(equiv, MonthFromTime(t), DateFromTime(t));                t = MakeDate(day, TimeWithinDay(t));             }            result.append(" (");            java.util.Date date = new Date((long) t);            result.append(timeZoneFormatter.format(date));            result.append(')');        }        return result.toString();    }    /* the javascript constructor */    private static Object jsConstructor(Object[] args)    {        NativeDate obj = new NativeDate();        // if called as a constructor with no args,        // return a new Date with the current time.        if (args.length == 0) {            obj.date = now();            return obj;        }        // if called with just one arg -        if (args.length == 1) {            Object arg0 = args[0];            if (arg0 instanceof Scriptable)                arg0 = ((Scriptable) arg0).getDefaultValue(null);            double date;            if (arg0 instanceof String) {                // it's a string; parse it.                date = date_parseString((String)arg0);            } else {                // if it's not a string, use it as a millisecond date                date = ScriptRuntime.toNumber(arg0);            }            obj.date = TimeClip(date);            return obj;        }        // multiple arguments; year, month, day etc.        double array[] = new double[MAXARGS];        int loop;        double d;        for (loop = 0; loop < MAXARGS; loop++) {            if (loop < args.length) {                d = ScriptRuntime.toNumber(args[loop]);                if (d != d || Double.isInfinite(d)) {                    obj.date = ScriptRuntime.NaN;                    return obj;                }                array[loop] = ScriptRuntime.toInteger(args[loop]);            } 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 */        if (array[2] < 1)            array[2] = 1;        double day = MakeDay(array[0], array[1], array[2]);        double time = MakeTime(array[3], array[4], array[5], array[6]);        time = MakeDate(day, time);        time = internalUTC(time);        obj.date = TimeClip(time);        return obj;    }    private static String toLocale_helper(double t, int methodId)    {        java.text.DateFormat formatter;        switch (methodId) {          case Id_toLocaleString:            if (localeDateTimeFormatter == null) {                localeDateTimeFormatter                    = DateFormat.getDateTimeInstance(DateFormat.LONG,                                                     DateFormat.LONG);            }            formatter = localeDateTimeFormatter;            break;          case Id_toLocaleTimeString:            if (localeTimeFormatter == null) {                localeTimeFormatter                    = DateFormat.getTimeInstance(DateFormat.LONG);            }            formatter = localeTimeFormatter;            break;          case Id_toLocaleDateString:            if (localeDateFormatter == null) {                localeDateFormatter                    = DateFormat.getDateInstance(DateFormat.LONG);            }            formatter = localeDateFormatter;            break;          default: formatter = null; // unreachable        }        return formatter.format(new Date((long) t));    }    private static String js_toUTCString(double date)    {        StringBuffer result = new StringBuffer(60);        appendWeekDayName(result, WeekDay(date));        result.append(", ");        append0PaddedUint(result, DateFromTime(date), 2);        result.append(' ');        appendMonthName(result, MonthFromTime(date));        result.append(' ');        int year = YearFromTime(date);        if (year < 0) {            result.append('-'); year = -year;        }        append0PaddedUint(result, year, 4);        result.append(' ');        append0PaddedUint(result, HourFromTime(date), 2);        result.append(':');        append0PaddedUint(result, MinFromTime(date), 2);        result.append(':');        append0PaddedUint(result, SecFromTime(date), 2);        result.append(" GMT");        return result.toString();    }    private static void append0PaddedUint(StringBuffer sb, int i, int minWidth)    {        if (i < 0) Kit.codeBug();        int scale = 1;        --minWidth;        if (i >= 10) {            if (i < 1000 * 1000 * 1000) {                for (;;) {                    int newScale = scale * 10;                    if (i < newScale) { break; }                    --minWidth;                    scale = newScale;                }            } else {                // Separated case not to check against 10 * 10^9 overflow                minWidth -= 9;                scale = 1000 * 1000 * 1000;            }        }        while (minWidth > 0) {            sb.append('0');            --minWidth;        }        while (scale != 1) {            sb.append((char)('0' + (i / scale)));            i %= scale;            scale /= 10;        }        sb.append((char)('0' + i));    }    private static void appendMonthName(StringBuffer sb, int index)    {        // Take advantage of the fact that all month abbreviations        // have the same length to minimize amount of strings runtime has        // to keep in memory        String months = "Jan"+"Feb"+"Mar"+"Apr"+"May"+"Jun"                       +"Jul"+"Aug"+"Sep"+"Oct"+"Nov"+"Dec";

⌨️ 快捷键说明

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