Code example for TimeZone

Methods: getDSTSavings, getDisplayName, getOffset, getRawOffset, useDaylightTime

0
        recycledTime.set(referenceTime);
        return recycledTime.hour;
    } 
 
    public int getNowOffsetMillis() { 
        return mTz.getOffset(System.currentTimeMillis());
    } 
 
    /* 
     * The method is synchronized because there's one mSB, which is used by 
     * mFormatter, per instance. If there are multiple callers for 
     * getGmtDisplayName, the output may be mangled. 
     */ 
    public synchronized CharSequence getGmtDisplayName(Context context) {
        // TODO Note: The local time is shown in current time (current GMT 
        // offset) which may be different from the time specified by 
        // mTimeMillis 
 
        final long nowMinute = System.currentTimeMillis() / DateUtils.MINUTE_IN_MILLIS;
        final long now = nowMinute * DateUtils.MINUTE_IN_MILLIS;
        final int gmtOffset = mTz.getOffset(now);
        int cacheKey;
 
        boolean hasFutureDST = mTz.useDaylightTime();
        if (hasFutureDST) {
            cacheKey = (int) (gmtOffset + 36 * DateUtils.HOUR_IN_MILLIS);
        } else { 
            cacheKey = (int) (gmtOffset - 36 * DateUtils.HOUR_IN_MILLIS);
        } 
 
        CharSequence displayName = null;
        if (mGmtDisplayNameUpdateTime != nowMinute) {
            mGmtDisplayNameUpdateTime = nowMinute;
            mGmtDisplayNameCache.clear();
        } else { 
            displayName = mGmtDisplayNameCache.get(cacheKey);
        } 
 
        if (displayName == null) {
            mSB.setLength(0);
            int flags = DateUtils.FORMAT_ABBREV_ALL;
            flags |= DateUtils.FORMAT_SHOW_TIME;
            if (TimeZoneInfo.is24HourFormat) {
                flags |= DateUtils.FORMAT_24HOUR;
            } 
 
            // mFormatter writes to mSB 
            DateUtils.formatDateRange(context, mFormatter, now, now, flags, mTzId);
            mSB.append("  ");
            int gmtStart = mSB.length();
            TimeZonePickerUtils.appendGmtOffset(mSB, gmtOffset);
            int gmtEnd = mSB.length();
 
            int symbolStart = 0;
            int symbolEnd = 0;
            if (hasFutureDST) {
                mSB.append(' ');
                symbolStart = mSB.length();
                mSB.append(TimeZonePickerUtils.getDstSymbol()); // Sun symbol
                symbolEnd = mSB.length();
            } 
 
            // Set the gray colors. 
            Spannable spannableText = mSpannableFactory.newSpannable(mSB);
            spannableText.setSpan(new ForegroundColorSpan(GMT_TEXT_COLOR),
                    gmtStart, gmtEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
 
            if (hasFutureDST) {
                spannableText.setSpan(new ForegroundColorSpan(DST_SYMBOL_COLOR),
                        symbolStart, symbolEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } 
            displayName = spannableText;
            mGmtDisplayNameCache.put(cacheKey, displayName);
        } 
        return displayName;
    } 
 
    private static int[] getTransitions(TimeZone tz, long time)
            throws IllegalAccessException, NoSuchFieldException { 
        Class<?> zoneInfoClass = tz.getClass();
        Field mTransitionsField = zoneInfoClass.getDeclaredField("mTransitions");
        mTransitionsField.setAccessible(true);
        int[] objTransitions = (int[]) mTransitionsField.get(tz);
        int[] transitions = null;
        if (objTransitions.length != 0) {
            transitions = new int[NUM_OF_TRANSITIONS];
            int numOfTransitions = 0;
            for (int i = 0; i < objTransitions.length; ++i) {
                if (objTransitions[i] < time) {
                    continue; 
                } 
                transitions[numOfTransitions++] = objTransitions[i];
                if (numOfTransitions == NUM_OF_TRANSITIONS) {
                    break; 
                } 
            } 
        } 
        return transitions;
    } 
 
    public boolean hasSameRules(TimeZoneInfo tzi) {
        // this.mTz.hasSameRules(tzi.mTz) 
 
        return this.mRawoffset == tzi.mRawoffset
                && Arrays.equals(this.mTransitions, tzi.mTransitions);
    } 
 
    @Override 
    public String toString() {
        StringBuilder sb = new StringBuilder();
 
        final String country = this.mCountry;
        final TimeZone tz = this.mTz;
 
        sb.append(mTzId);
        sb.append(SEPARATOR);
        sb.append(tz.getDisplayName(false /* daylightTime */, TimeZone.LONG));
        sb.append(SEPARATOR);
        sb.append(tz.getDisplayName(false /* daylightTime */, TimeZone.SHORT));
        sb.append(SEPARATOR);
        if (tz.useDaylightTime()) {
            sb.append(tz.getDisplayName(true, TimeZone.LONG));
            sb.append(SEPARATOR);
            sb.append(tz.getDisplayName(true, TimeZone.SHORT));
        } else { 
            sb.append(SEPARATOR);
        } 
        sb.append(SEPARATOR);
        sb.append(tz.getRawOffset() / 3600000f);
        sb.append(SEPARATOR);
        sb.append(tz.getDSTSavings() / 3600000f);
        sb.append(SEPARATOR);
        sb.append(country);
        sb.append(SEPARATOR);
 
        // 1-1-2013 noon GMT