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

📄 time.java

📁 tinyos最新版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                for (int i=0; i<12; i++)                    shortmonths[i] = names[i];            }        }        catch (ArrayIndexOutOfBoundsException e) {            throwValueError("month out of range (1-12)");        }        return shortmonths[month0to11];    }    private static String _padint(int i, int target) {        String s = Integer.toString(i);        int sz = s.length();        if (target <= sz)            // no truncation            return s;        if (target == sz+1)            return "0"+s;        if (target == sz+2)            return "00"+s;        else {            char[] c = new char[target-sz];            while (target > sz) {                c[target-sz] = '0';                target--;            }            return new String(c) + s;        }    }    private static String _twodigit(int i) {        return _padint(i, 2);    }    private static String _truncyear(int year) {        String yearstr = _padint(year, 4);        return yearstr.substring(yearstr.length()-2, yearstr.length());    }    public static String asctime() {        return asctime(localtime());    }    public static String asctime(PyTuple tup) {        checkLocale();        int day = item(tup, 6);        int mon = item(tup, 1);        return _shortday(day) + " " + _shortmonth(mon) + " " +            _twodigit(item(tup, 2)) + " " +            _twodigit(item(tup, 3)) + ":" +            _twodigit(item(tup, 4)) + ":" +            _twodigit(item(tup, 5)) + " " +            item(tup, 0);    }    public static void sleep(double secs) {        try {            java.lang.Thread.sleep((long)(secs * 1000));        }        catch (java.lang.InterruptedException e) {            throw new PyException(Py.KeyboardInterrupt, "interrupted sleep");        }    }    // set by classDictInit()    public static int timezone;    public static int altzone = -1;    public static int daylight;    public static PyTuple tzname = null;    // TBD: should we accept 2 digit years?  should we make this attribute    // writable but ignore its value?    public static final int accept2dyear = 0;    public static String strftime(String format) {        return strftime(format, localtime());    }    public static String strftime(String format, PyTuple tup) {        checkLocale();        String s = "";        int lastc = 0;        int j;        String[] syms;        GregorianCalendar cal = null;        while (lastc < format.length()) {            int i = format.indexOf("%", lastc);            if (i < 0) {                // the end of the format string                s = s + format.substring(lastc);                break;            }            if (i == format.length() - 1) {                // there's a bare % at the end of the string.  Python lets                // this go by just sticking a % at the end of the result                // string                s = s + "%";                break;            }            s = s + format.substring(lastc, i);            i++;            switch (format.charAt(i)) {            case 'a':                // abbrev weekday                j = item(tup, 6);                s = s + _shortday(j);                break;            case 'A':                // full weekday                // see _shortday()                syms = datesyms.getWeekdays();                j = item(tup, 6);                if (0 <= j && j < 6)                    s = s + syms[j+2];                else if (j== 6)                    s = s + syms[1];                else                    throwValueError("day of week out of range (0 - 6)");                break;            case 'b':                // abbrev month                j = item(tup, 1);                s = s + _shortmonth(j);                break;            case 'B':                // full month                syms = datesyms.getMonths();                j = item(tup, 1);                s = s + syms[j];                break;            case 'c':                // locale's date and time repr (essentially asctime()?)                s = s + asctime(tup);                break;            case 'd':                // day of month (01-31)                s = s + _twodigit(item(tup, 2));                break;            case 'H':                // hour (00-23)                s = s + _twodigit(item(tup, 3));                break;            case 'I':                // hour (01-12)                j = item(tup, 3) % 12;                if (j == 0)                    j = 12;                  // midnight or noon                s = s + _twodigit(j);                break;            case 'j':                // day of year (001-366)                s = _padint(item(tup, 7), 3);                break;            case 'm':                // month (01-12)                s = s + _twodigit(item(tup, 1) + 1);                break;            case 'M':                // minute (00-59)                s = s + _twodigit(item(tup, 4));                break;            case 'p':                // AM/PM                j = item(tup, 3);                syms = datesyms.getAmPmStrings();                if (0 <= j && j < 12)                    s = s + syms[0];                else if (12 <= j && j < 24)                    s = s + syms[1];                else                    throwValueError("hour out of range (0-23)");                break;            case 'S':                // seconds (00-61)                s = s + _twodigit(item(tup, 5));                break;            case 'U':                // week of year (sunday is first day) (00-53).  all days in                // new year preceding first sunday are considered to be in                // week 0                if (cal == null)                    cal = _tupletocal(tup);                cal.setFirstDayOfWeek(cal.SUNDAY);                cal.setMinimalDaysInFirstWeek(7);                j = cal.get(cal.WEEK_OF_YEAR);                if (cal.get(cal.MONTH) == cal.JANUARY && j >= 52)                    j = 0;                s = s + _twodigit(j);                break;            case 'w':                // weekday as decimal (0=Sunday-6)                // tuple format has monday=0                j = (item(tup, 6) + 1) % 7;                s = s + _twodigit(j);                break;            case 'W':                // week of year (monday is first day) (00-53).  all days in                // new year preceding first sunday are considered to be in                // week 0                if (cal == null)                    cal = _tupletocal(tup);                cal.setFirstDayOfWeek(cal.MONDAY);                cal.setMinimalDaysInFirstWeek(7);                j = cal.get(cal.WEEK_OF_YEAR);                if (cal.get(cal.MONTH) == cal.JANUARY && j >= 52)                    j = 0;                s = s + _twodigit(j);                break;            case 'x':                // TBD: A note about %x and %X.  Python's time.strftime()                // by default uses the "C" locale, which is changed by                // using the setlocale() function.  In Java, the default                // locale is set by user.language and user.region                // properties and is "en_US" by default, at least around                // here!  Locale "en_US" differs from locale "C" in the way                // it represents dates and times.  Eventually we might want                // to craft a "C" locale for Java and set JPython to use                // this by default, but that's too much work right now.                //                // For now, we hard code %x and %X to return values                // formatted in the "C" locale, i.e. the default way                // CPython does it.  E.g.:                //     %x == mm/dd/yy                //     %X == HH:mm:SS                //                s = s + _twodigit(item(tup, 1) + 1) + "/" +                    _twodigit(item(tup, 2)) + "/" +                    _truncyear(item(tup, 0));                break;            case 'X':                // See comment for %x above                s = s + _twodigit(item(tup, 3)) + ":" +                    _twodigit(item(tup, 4)) + ":" +                    _twodigit(item(tup, 5));                break;            case 'Y':                // year w/ century                s = s + _padint(item(tup, 0), 4);                break;            case 'y':                // year w/o century (00-99)                s = s + _truncyear(item(tup, 0));                break;            case 'Z':                // timezone name                if (cal == null)                    cal = _tupletocal(tup);                s = s + getDisplayName(cal.getTimeZone(),                        // in daylight savings time?  true if == 1 -1                        // means the information was not available;                        // treat this as if not in dst                        item(tup, 8) > 0, 0);                break;            case '%':                // %                s = s + "%";                break;            default:                // TBD: should this raise a ValueError?                s = s + "%" + format.charAt(i);                i++;                break;            }            lastc = i+1;            i++;        }        return s;    }    private static void checkLocale() {        if (!Locale.getDefault().equals(currentLocale)) {            currentLocale = Locale.getDefault();            datesyms = new DateFormatSymbols(currentLocale);            shortdays = null;            shortmonths = null;        }    }    private static String getDisplayName(TimeZone tz, boolean dst,                                         int style)    {        String version = System.getProperty("java.version");        if (version.compareTo("1.2") >= 0) {            try {                Method m = tz.getClass().getMethod("getDisplayName",                            new Class[] { Boolean.TYPE, Integer.TYPE });                return (String) m.invoke(tz, new Object[] {                            new Boolean(dst), new Integer(style) });            } catch (Exception exc) { }        }        return tz.getID();    }    private static int getDSTSavings(TimeZone tz) {        String version = System.getProperty("java.version");        if (version.compareTo("1.2") >= 0) {            try {                Method m = tz.getClass().getMethod("getDSTSavings", null);                return ((Integer) m.invoke(tz, null)).intValue();             } catch (Exception exc) { }        }        return 0;    }}

⌨️ 快捷键说明

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