📄 timezone.java
字号:
tz = new SimpleTimeZone (10500 * 3600, "Australia/Lord_Howe", Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600, Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600, 500 * 3600); timezones0.put("Australia/Lord_Howe", tz); tz = new SimpleTimeZone (11000 * 3600, "Asia/Magadan", Calendar.MARCH, -1, Calendar.SUNDAY, 3000 * 3600, Calendar.OCTOBER, -1, Calendar.SUNDAY, 3000 * 3600); timezones0.put("Asia/Magadan", tz); tz = new SimpleTimeZone(11000 * 3600, "SST"); timezones0.put("SST", tz); timezones0.put("Pacific/Efate", tz); timezones0.put("Pacific/Guadalcanal", tz); timezones0.put("Pacific/Kosrae", tz); timezones0.put("Pacific/Noumea", tz); timezones0.put("Pacific/Ponape", tz); tz = new SimpleTimeZone(11500 * 3600, "Pacific/Norfolk"); timezones0.put("Pacific/Norfolk", tz); tz = new SimpleTimeZone (12000 * 3600, "NST", Calendar.OCTOBER, 1, Calendar.SUNDAY, 3000 * 3600, Calendar.MARCH, 3, Calendar.SUNDAY, 3000 * 3600); timezones0.put("NST", tz); timezones0.put("Antarctica/McMurdo", tz); timezones0.put("Antarctica/South_Pole", tz); timezones0.put("Pacific/Auckland", tz); tz = new SimpleTimeZone (12000 * 3600, "Asia/Anadyr", Calendar.MARCH, -1, Calendar.SUNDAY, 3000 * 3600, Calendar.OCTOBER, -1, Calendar.SUNDAY, 3000 * 3600); timezones0.put("Asia/Anadyr", tz); timezones0.put("Asia/Kamchatka", tz); tz = new SimpleTimeZone(12000 * 3600, "Pacific/Fiji"); timezones0.put("Pacific/Fiji", tz); timezones0.put("Pacific/Funafuti", tz); timezones0.put("Pacific/Kwajalein", tz); timezones0.put("Pacific/Majuro", tz); timezones0.put("Pacific/Nauru", tz); timezones0.put("Pacific/Tarawa", tz); timezones0.put("Pacific/Wake", tz); timezones0.put("Pacific/Wallis", tz); tz = new SimpleTimeZone (12750 * 3600, "Pacific/Chatham", Calendar.OCTOBER, 1, Calendar.SUNDAY, 3750 * 3600, Calendar.MARCH, 3, Calendar.SUNDAY, 3750 * 3600); timezones0.put("Pacific/Chatham", tz); tz = new SimpleTimeZone(13000 * 3600, "Pacific/Enderbury"); timezones0.put("Pacific/Enderbury", tz); timezones0.put("Pacific/Tongatapu", tz); tz = new SimpleTimeZone(14000 * 3600, "Pacific/Kiritimati"); timezones0.put("Pacific/Kiritimati", tz); } return timezones0; } /** * Maps a time zone name (with optional GMT offset and daylight time * zone name) to one of the known time zones. This method called * with the result of <code>System.getProperty("user.timezone")</code> * or <code>getDefaultTimeZoneId()</code>. Note that giving one of * the standard tz data names from ftp://elsie.nci.nih.gov/pub/ is * preferred. * The time zone name can be given as follows: * <code>(standard zone name)[(GMT offset)[(DST zone name)[DST offset]]] * </code> * <p> * If only a (standard zone name) is given (no numbers in the * String) then it gets mapped directly to the TimeZone with that * name, if that fails null is returned. * <p> * Alternately, a POSIX-style TZ string can be given, defining the time zone: * <code>std offset dst offset,date/time,date/time</code> * See the glibc manual, or the man page for <code>tzset</code> for details * of this format. * <p> * A GMT offset is the offset to add to the local time to get GMT. * If a (GMT offset) is included (either in seconds or hours) then * an attempt is made to find a TimeZone name matching both the name * and the offset (that doesn't observe daylight time, if the * timezone observes daylight time then you must include a daylight * time zone name after the offset), if that fails then a TimeZone * with the given GMT offset is returned (whether or not the * TimeZone observes daylight time is ignored), if that also fails * the GMT TimeZone is returned. * <p> * If the String ends with (GMT offset)(daylight time zone name) * then an attempt is made to find a TimeZone with the given name and * GMT offset that also observes (the daylight time zone name is not * currently used in any other way), if that fails a TimeZone with * the given GMT offset that observes daylight time is returned, if * that also fails the GMT TimeZone is returned. * <p> * Examples: In Chicago, the time zone id could be "CST6CDT", but * the preferred name would be "America/Chicago". In Indianapolis * (which does not have Daylight Savings Time) the string could be * "EST5", but the preferred name would be "America/Indianapolis". * The standard time zone name for The Netherlands is "Europe/Amsterdam", * but can also be given as "CET-1CEST". */ static TimeZone getDefaultTimeZone(String sysTimeZoneId) { String stdName = null; String dstName; int stdOffs; int dstOffs; try { int idLength = sysTimeZoneId.length(); int index = 0; int prevIndex; char c; // get std do c = sysTimeZoneId.charAt(index++); while (c != '+' && c != '-' && c != ',' && c != ':' && ! Character.isDigit(c) && c != '\0' && index < idLength); if (index >= idLength) return (TimeZone)timezones().get(sysTimeZoneId); stdName = sysTimeZoneId.substring(0, --index); prevIndex = index; // get the std offset do c = sysTimeZoneId.charAt(index++); while ((c == '-' || c == '+' || c == ':' || Character.isDigit(c)) && index < idLength); if (index < idLength) index--; { // convert the dst string to a millis number String offset = sysTimeZoneId.substring(prevIndex, index); prevIndex = index; if (offset.charAt(0) == '+' || offset.charAt(0) == '-') stdOffs = parseTime(offset.substring(1)); else stdOffs = parseTime(offset); if (offset.charAt(0) == '-') stdOffs = -stdOffs; // TZ timezone offsets are positive when WEST of the meridian. stdOffs = -stdOffs; } // Done yet? (Format: std offset) if (index >= idLength) { // Do we have an existing timezone with that name and offset? TimeZone tz = (TimeZone) timezones().get(stdName); if (tz != null) if (tz.getRawOffset() == stdOffs) return tz; // Custom then. return new SimpleTimeZone(stdOffs, stdName); } // get dst do c = sysTimeZoneId.charAt(index++); while (c != '+' && c != '-' && c != ',' && c != ':' && ! Character.isDigit(c) && c != '\0' && index < idLength); // Done yet? (Format: std offset dst) if (index >= idLength) { // Do we have an existing timezone with that name and offset // which has DST? TimeZone tz = (TimeZone) timezones().get(stdName); if (tz != null) if (tz.getRawOffset() == stdOffs && tz.useDaylightTime()) return tz; // Custom then. return new SimpleTimeZone(stdOffs, stdName); } // get the dst offset dstName = sysTimeZoneId.substring(prevIndex, --index); prevIndex = index; do c = sysTimeZoneId.charAt(index++); while ((c == '-' || c == '+' || c == ':' || Character.isDigit(c)) && index < idLength); if (index < idLength) index--; { // convert the dst string to a millis number String offset = sysTimeZoneId.substring(prevIndex, index); prevIndex = index; if (offset.charAt(0) == '+' || offset.charAt(0) == '-') dstOffs = parseTime(offset.substring(1)); else dstOffs = parseTime(offset); if (offset.charAt(0) == '-') dstOffs = -dstOffs; // TZ timezone offsets are positive when WEST of the meridian. dstOffs = -dstOffs; } // Done yet? (Format: std offset dst offset) // FIXME: We don't support DST without a rule given. Should we? if (index >= idLength) { // Time Zone existing with same name, dst and offsets? TimeZone tz = (TimeZone) timezones().get(stdName); if (tz != null) if (tz.getRawOffset() == stdOffs && tz.useDaylightTime() && tz.getDSTSavings() == (dstOffs - stdOffs)) return tz; return new SimpleTimeZone(stdOffs, stdName); } // get the DST rule if (sysTimeZoneId.charAt(index) == ',' || sysTimeZoneId.charAt(index) == ';') { index++; int offs = index; while (sysTimeZoneId.charAt(index) != ',' && sysTimeZoneId.charAt(index) != ';') index++; String startTime = sysTimeZoneId.substring(offs, index); index++; String endTime = sysTimeZoneId.substring(index); index = startTime.indexOf('/'); int startMillis; int endMillis; String startDate; String endDate; if (index != -1) { startDate = startTime.substring(0, index); startMillis = parseTime(startTime.substring(index + 1)); } else { startDate = startTime; // if time isn't given, default to 2:00:00 AM. startMillis = 2 * 60 * 60 * 1000; } index = endTime.indexOf('/'); if (index != -1) { endDate = endTime.substring(0, index); endMillis = parseTime(endTime.substring(index + 1)); } else { endDate = endTime; // if time isn't given, default to 2:00:00 AM. endMillis = 2 * 60 * 60 * 1000; } int[] start = getDateParams(startDate); int[] end = getDateParams(endDate); return new SimpleTimeZone(stdOffs, stdName, start[0], start[1], start[2], startMillis, end[0], end[1], end[2], endMillis, (dstOffs - stdOffs)); } } // FIXME: Produce a warning here? catch (IndexOutOfBoundsException _) { } catch (NumberFormatException _) { } return null; } /** * Parses and returns the params for a POSIX TZ date field, * in the format int[]{ month, day, dayOfWeek }, following the * SimpleTimeZone constructor rules. */ private static int[] getDateParams(String date) { int[] dayCount = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; int month; if (date.charAt(0) == 'M' || date.charAt(0) == 'm') { int day; // Month, week of month, day of week month = Integer.parseInt(date.substring(1, date.indexOf('.'))); int week = Integer.parseInt(date.substring(date.indexOf('.') + 1, date.lastIndexOf('.'))); int dayOfWeek = Integer.parseInt(date.substring(date.lastIndexOf('.') + 1)); if (week == 5) day = -1; // last day of month is -1 in java, 5 in TZ else // first day of week starting on or after. day = (week - 1) * 7 + 1; dayOfWeek++; // Java day of week is one-based, Sunday is first day. month--; // Java month is zero-based. return new int[] { month, day, dayOfWeek }; } // julian day, either zero-based 0<=n<=365 (incl feb 29) // or one-based 1<=n<=365 (no feb 29) int julianDay; // Julian day, if (date.charAt(0) != 'J' || date.charAt(0) != 'j') { julianDay = Integer.parseInt(date.substring(1)); julianDay++; // make 1-based // Adjust day count to include feb 29. dayCount = new int[] { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }; } else // 1-based julian day julianDay = Integer.parseInt(date); int i = 11; while (i > 0) if (dayCount[i] < julianDay) break; else i--; julianDay -= dayCount[i]; month = i; return new int[] { month, julianDay, 0 }; } /** * Parses a time field hh[:mm[:ss]], returning the result * in milliseconds. No leading sign. */ private static int parseTime(String time) { int millis = 0; int i = 0; while (i < time.length()) if (time.charAt(i) == ':') break; else i++; millis = 60 * 60 * 1000 * Integer.parseInt(time.substring(0, i)); if (i >= time.length()) return millis; int iprev = ++i; while (i < time.length()) if (time.charAt(i) == ':') break; else i++; if (i >= time.length()) return millis; millis += 60 * 1000 * Integer.parseInt(time.substring(iprev, i)); millis += 1000 * Integer.parseInt(time.substring(++i)); return millis; } /** * Gets the time zone offset, for current date, modified in case of * daylight savings. This is the offset to add to UTC to get the local * time. * @param era the era of the given date * @param year the year of the given date * @param month the month of the given date, 0 for January. * @param day the day of month * @param dayOfWeek the day of week * @param milliseconds the millis in the day (in local standard time) * @return the time zone offset in milliseconds.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -