zoneinfo.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 699 行 · 第 1/2 页

JAVA
699
字号
/* * @(#)ZoneInfo.java	1.9 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation.  *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt).  *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA  *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions.  */package sun.util.calendar;import java.lang.ref.SoftReference;import java.util.ArrayList;import java.util.Date;import java.util.GregorianCalendar;import java.util.HashMap;import java.util.SimpleTimeZone;import java.util.TimeZone;/** * <code>ZoneInfo</code> is an implementation subclass of {@link * java.util.TimeZone TimeZone} that represents GMT offsets and * daylight saving time transitions of a time zone. * <p> * The daylight saving time transitions are described in the {@link * #transitions transitions} table consisting of a chronological * sequence of transitions of GMT offset and/or daylight saving time * changes. Since all transitions are represented in UTC, in theory, * <code>ZoneInfo</code> can be used with any calendar systems except * for the {@link #getOffset(int,int,int,int,int,int) getOffset} * method that takes Gregorian calendar date fields. * <p> * This table covers transitions from 1900 until 2037 (as of version * 1.4), Before 1900, it assumes that there was no daylight saving * time and the <code>getOffset</code> methods always return the * {@link #getRawOffset} value. No Local Mean Time is supported. If a * specified date is beyond the transition table and this time zone is * supposed to observe daylight saving time in 2037, it delegates * operations to a {@link java.util.SimpleTimeZone SimpleTimeZone} * object created using the daylight saving time schedule as of 2037. * <p> * The date items, transitions, GMT offset(s), etc. are read from a database * file. See {@link ZoneInfoFile} for details. * @see java.util.SimpleTimeZone * @since 1.4 */public class ZoneInfo extends TimeZone {    private static final long OFFSET_MASK = 0x0fL;    private static final long DST_MASK = 0xf0L;    private static final int DST_NSHIFT = 4;    // this bit field is reserved for abbreviation support    private static final long ABBR_MASK = 0xf00L;    private static final int TRANSITION_NSHIFT = 12;    /**     * The raw GMT offset in milliseconds between this zone and GMT.     * Negative offsets are to the west of Greenwich.  To obtain local     * <em>standard</em> time, add the offset to GMT time.     * @serial     */    private int rawOffset;    /**     * Difference in milliseconds from the original GMT offset in case     * the raw offset value has been modified by calling {@link     * #setRawOffset}. The initial value is 0.     * @serial     */    private int rawOffsetDiff = 0;    /**     * A CRC32 value of all pairs of transition time (in milliseconds     * in <code>long</code>) in local time and its GMT offset (in     * seconds in <code>int</code>) in the chronological order. Byte     * values of each <code>long</code> and <code>int</code> are taken     * in the big endian order (i.e., MSB to LSB).     * @serial     */    private int checksum;    /**     * The amount of time in milliseconds saved during daylight saving     * time. If <code>useDaylight</code> is false, this value is 0.     * @serial     */    private int dstSavings;    /**     * This array describes transitions of GMT offsets of this time     * zone, including both raw offset changes and daylight saving     * time changes.     * A long integer consists of four bit fields.     * <ul>     * <li>The most significant 52-bit field represents transition     * time in milliseconds from Gregorian January 1 1970, 00:00:00     * GMT.</li>     * <li>The next 4-bit field is reserved and must be 0.</li>     * <li>The next 4-bit field is an index value to {@link #offsets     * offsets[]} for the amount of daylight saving at the     * transition. If this value is zero, it means that no daylight     * saving, not the index value zero.</li>     * <li>The least significant 4-bit field is an index value to     * {@link #offsets offsets[]} for <em>total</em> GMT offset at the     * transition.</li>     * </ul>     * If this time zone doesn't observe daylight saving time and has     * never changed any GMT offsets in the past, this value is null.     * @serial     */    private long[] transitions;    /**     * This array holds all unique offset values in     * milliseconds. Index values to this array are stored in the     * transitions array elements.     * @serial     */    private int[] offsets;    /**     * SimpleTimeZone parameter values. It has to have either 8 for     * {@link java.util.SimpleTimeZone#SimpleTimeZone(int, String,     * int, int , int , int , int , int , int , int , int) the     * 11-argument SimpleTimeZone constructor} or 10 for {@link     * java.util.SimpleTimeZone#SimpleTimeZone(int, String, int, int,     * int , int , int , int , int , int , int, int, int) the     * 13-argument SimpleTimeZone constructor} parameters.     * @serial     */    private int[] simpleTimeZoneParams;    /**     * True if the raw GMT offset value would change after the time     * zone data has been generated; false, otherwise. The default     * value is false.     * @serial     */    private boolean willGMTOffsetChange = false;    private static final long serialVersionUID = 2653134537216586139L;    /**     * A constructor.     */    public ZoneInfo() {    }    /**     * A Constructor for CustomID.     */    public ZoneInfo(String ID, int rawOffset) {        this(ID, rawOffset, 0, 0, null, null, null, false);    }    /**     * Constructs a ZoneInfo instance.     *     * @param ID time zone name     * @param rawOffset GMT offset in milliseconds     * @param dstSavings daylight saving value in milliseconds or 0     * (zero) if this time zone doesn't observe Daylight Saving Time.     * @param checksum CRC32 value with all transitions table entry     * values     * @param transitions transition table     * @param offsets offset value table     * @param simpleTimeZoneParams parameter values for constructing     * SimpleTimeZone     * @param willGMTOffsetChange the value of willGMTOffsetChange     */    ZoneInfo(String ID,	     int rawOffset,	     int dstSavings,	     int checksum,	     long[] transitions,	     int[] offsets,	     int[] simpleTimeZoneParams,	     boolean willGMTOffsetChange) {	setID(ID);	this.rawOffset = rawOffset;	this.dstSavings = dstSavings;	this.checksum = checksum;	this.transitions = transitions;	this.offsets = offsets;	this.simpleTimeZoneParams = simpleTimeZoneParams;	this.willGMTOffsetChange = willGMTOffsetChange;    }    /**     * Returns the difference in milliseconds between local time and UTC     * of given time, taking into account both the raw offset and the     * effect of daylight savings.     *     * @param date the milliseconds in UTC     * @return the milliseconds to add to UTC to get local wall time     */    public int getOffset(long date) {	return getOffsets(date, null, false);    }    public int getOffsets(long utc, int[] offsets) {	return getOffsets(utc, offsets, false);    }    public int getOffsetsByWall(long wall, int[] offsets) {	return getOffsets(wall, offsets, true);    }    private int getOffsets(long date, int[] offsets, boolean isWall) {	// if dst is never observed, there is no transition.	if (transitions == null) {	    int offset = getLastRawOffset();	    if (offsets != null) {		offsets[0] = offset;		offsets[1] = 0;	    }	    return offset;	}	date -= rawOffsetDiff;	int index = getTransitionIndex(date, isWall);	// prior to the transition table, returns the raw offset.	// should support LMT.	if (index < 0) {	    int offset = getLastRawOffset();	    if (offsets != null) {		offsets[0] = offset;		offsets[1] = 0;	    }	    return offset;	}	if (index < transitions.length) {	    long val = transitions[index];	    int offset = this.offsets[(int)(val & OFFSET_MASK)] + rawOffsetDiff;	    if (offsets != null) {		int dst = (int)((val >>> DST_NSHIFT) & 0xfL);		int save = (dst == 0) ? 0 : this.offsets[dst];		offsets[0] = offset - save;		offsets[1] = save;	    }	    return offset;	}	// beyond the transitions, delegate to SimpleTimeZone if there	// is a rule; otherwise, return rawOffset.	SimpleTimeZone tz = getLastRule();	if (tz != null) {	    int rawoffset = tz.getRawOffset();	    long msec = date;	    if (isWall) {		msec -= rawOffset;	    }	    int dstoffset = tz.inDaylightTime(new Date(msec)) ? tz.getDSTSavings() : 0;	    if (offsets != null) {		offsets[0] = rawoffset;		offsets[1] = dstoffset;	    }	    return rawoffset + dstoffset;	}	int offset = getLastRawOffset();	if (offsets != null) {	    offsets[0] = offset;	    offsets[1] = 0;	}	return offset;    }    private final int getTransitionIndex(long date, boolean isWall) {	int low = 0;	int high = transitions.length - 1;	while (low <= high) {	    int mid = (low + high) / 2;	    long val = transitions[mid];	    long midVal = val >> TRANSITION_NSHIFT;	    if (isWall) {		midVal += offsets[(int)(val & OFFSET_MASK)]; // wall time	    }	    if (midVal < date) {		low = mid + 1;	    } else if (midVal > date) {		high = mid - 1;	    } else {		return mid;	    }	}	// if beyond the transitions, returns that index.	if (low >= transitions.length) {	    return low;	}	return low - 1;    }   /**     * Returns the difference in milliseconds between local time and     * UTC, taking into account both the raw offset and the effect of     * daylight savings, for the specified date and time.  This method     * assumes that the start and end month are distinct.  This method     * assumes a Gregorian calendar for calculations.     * <p>     * <em>Note: In general, clients should use     * {@link Calendar#ZONE_OFFSET Calendar.get(ZONE_OFFSET)} +     * {@link Calendar#DST_OFFSET Calendar.get(DST_OFFSET)}     * instead of calling this method.</em>     *     * @param era       The era of the given date. The value must be either     *                  GregorianCalendar.AD or GregorianCalendar.BC.     * @param year      The year in the given date.     * @param month     The month in the given date. Month is 0-based. e.g.,     *                  0 for January.     * @param day       The day-in-month of the given date.     * @param dayOfWeek The day-of-week of the given date.     * @param millis    The milliseconds in day in <em>standard</em> local time.     * @return The milliseconds to add to UTC to get local time.     */    public int getOffset(int era, int year, int month, int day,			 int dayOfWeek, int milliseconds) {	if (milliseconds < 0 || milliseconds >= Gregorian.ONE_DAY) {	    throw new IllegalArgumentException();	}	if (era == GregorianCalendar.BC) { // BC	    year = 1 - year;	} else if (era != GregorianCalendar.AD) {	    throw new IllegalArgumentException();	}	CalendarDate date = new CalendarDate(year, month, day);

⌨️ 快捷键说明

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