Search in sources :

Example 1 with ZoneInfo

use of sun.util.calendar.ZoneInfo in project jdk8u_jdk by JetBrains.

the class JapaneseImperialCalendar method computeFields.

/**
     * This computeFields implements the conversion from UTC
     * (millisecond offset from the Epoch) to calendar
     * field values. fieldMask specifies which fields to change the
     * setting state to COMPUTED, although all fields are set to
     * the correct values. This is required to fix 4685354.
     *
     * @param fieldMask a bit mask to specify which fields to change
     * the setting state.
     * @param tzMask a bit mask to specify which time zone offset
     * fields to be used for time calculations
     * @return a new field mask that indicates what field values have
     * actually been set.
     */
private int computeFields(int fieldMask, int tzMask) {
    int zoneOffset = 0;
    TimeZone tz = getZone();
    if (zoneOffsets == null) {
        zoneOffsets = new int[2];
    }
    if (tzMask != (ZONE_OFFSET_MASK | DST_OFFSET_MASK)) {
        if (tz instanceof ZoneInfo) {
            zoneOffset = ((ZoneInfo) tz).getOffsets(time, zoneOffsets);
        } else {
            zoneOffset = tz.getOffset(time);
            zoneOffsets[0] = tz.getRawOffset();
            zoneOffsets[1] = zoneOffset - zoneOffsets[0];
        }
    }
    if (tzMask != 0) {
        if (isFieldSet(tzMask, ZONE_OFFSET)) {
            zoneOffsets[0] = internalGet(ZONE_OFFSET);
        }
        if (isFieldSet(tzMask, DST_OFFSET)) {
            zoneOffsets[1] = internalGet(DST_OFFSET);
        }
        zoneOffset = zoneOffsets[0] + zoneOffsets[1];
    }
    // By computing time and zoneOffset separately, we can take
    // the wider range of time+zoneOffset than the previous
    // implementation.
    long fixedDate = zoneOffset / ONE_DAY;
    int timeOfDay = zoneOffset % (int) ONE_DAY;
    fixedDate += time / ONE_DAY;
    timeOfDay += (int) (time % ONE_DAY);
    if (timeOfDay >= ONE_DAY) {
        timeOfDay -= ONE_DAY;
        ++fixedDate;
    } else {
        while (timeOfDay < 0) {
            timeOfDay += ONE_DAY;
            --fixedDate;
        }
    }
    fixedDate += EPOCH_OFFSET;
    // See if we can use jdate to avoid date calculation.
    if (fixedDate != cachedFixedDate || fixedDate < 0) {
        jcal.getCalendarDateFromFixedDate(jdate, fixedDate);
        cachedFixedDate = fixedDate;
    }
    int era = getEraIndex(jdate);
    int year = jdate.getYear();
    // Always set the ERA and YEAR values.
    internalSet(ERA, era);
    internalSet(YEAR, year);
    int mask = fieldMask | (ERA_MASK | YEAR_MASK);
    // 0-based
    int month = jdate.getMonth() - 1;
    int dayOfMonth = jdate.getDayOfMonth();
    // Set the basic date fields.
    if ((fieldMask & (MONTH_MASK | DAY_OF_MONTH_MASK | DAY_OF_WEEK_MASK)) != 0) {
        internalSet(MONTH, month);
        internalSet(DAY_OF_MONTH, dayOfMonth);
        internalSet(DAY_OF_WEEK, jdate.getDayOfWeek());
        mask |= MONTH_MASK | DAY_OF_MONTH_MASK | DAY_OF_WEEK_MASK;
    }
    if ((fieldMask & (HOUR_OF_DAY_MASK | AM_PM_MASK | HOUR_MASK | MINUTE_MASK | SECOND_MASK | MILLISECOND_MASK)) != 0) {
        if (timeOfDay != 0) {
            int hours = timeOfDay / ONE_HOUR;
            internalSet(HOUR_OF_DAY, hours);
            // Assume AM == 0
            internalSet(AM_PM, hours / 12);
            internalSet(HOUR, hours % 12);
            int r = timeOfDay % ONE_HOUR;
            internalSet(MINUTE, r / ONE_MINUTE);
            r %= ONE_MINUTE;
            internalSet(SECOND, r / ONE_SECOND);
            internalSet(MILLISECOND, r % ONE_SECOND);
        } else {
            internalSet(HOUR_OF_DAY, 0);
            internalSet(AM_PM, AM);
            internalSet(HOUR, 0);
            internalSet(MINUTE, 0);
            internalSet(SECOND, 0);
            internalSet(MILLISECOND, 0);
        }
        mask |= (HOUR_OF_DAY_MASK | AM_PM_MASK | HOUR_MASK | MINUTE_MASK | SECOND_MASK | MILLISECOND_MASK);
    }
    if ((fieldMask & (ZONE_OFFSET_MASK | DST_OFFSET_MASK)) != 0) {
        internalSet(ZONE_OFFSET, zoneOffsets[0]);
        internalSet(DST_OFFSET, zoneOffsets[1]);
        mask |= (ZONE_OFFSET_MASK | DST_OFFSET_MASK);
    }
    if ((fieldMask & (DAY_OF_YEAR_MASK | WEEK_OF_YEAR_MASK | WEEK_OF_MONTH_MASK | DAY_OF_WEEK_IN_MONTH_MASK)) != 0) {
        int normalizedYear = jdate.getNormalizedYear();
        // If it's a year of an era transition, we need to handle
        // irregular year boundaries.
        boolean transitionYear = isTransitionYear(jdate.getNormalizedYear());
        int dayOfYear;
        long fixedDateJan1;
        if (transitionYear) {
            fixedDateJan1 = getFixedDateJan1(jdate, fixedDate);
            dayOfYear = (int) (fixedDate - fixedDateJan1) + 1;
        } else if (normalizedYear == MIN_VALUES[YEAR]) {
            CalendarDate dx = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
            fixedDateJan1 = jcal.getFixedDate(dx);
            dayOfYear = (int) (fixedDate - fixedDateJan1) + 1;
        } else {
            dayOfYear = (int) jcal.getDayOfYear(jdate);
            fixedDateJan1 = fixedDate - dayOfYear + 1;
        }
        long fixedDateMonth1 = transitionYear ? getFixedDateMonth1(jdate, fixedDate) : fixedDate - dayOfMonth + 1;
        internalSet(DAY_OF_YEAR, dayOfYear);
        internalSet(DAY_OF_WEEK_IN_MONTH, (dayOfMonth - 1) / 7 + 1);
        int weekOfYear = getWeekNumber(fixedDateJan1, fixedDate);
        // ISO8601-style. This creates problems, though.
        if (weekOfYear == 0) {
            // If the date belongs to the last week of the
            // previous year, use the week number of "12/31" of
            // the "previous" year. Again, if the previous year is
            // a transition year, we need to take care of it.
            // Usually the previous day of the first day of a year
            // is December 31, which is not always true in the
            // Japanese imperial calendar system.
            long fixedDec31 = fixedDateJan1 - 1;
            long prevJan1;
            LocalGregorianCalendar.Date d = getCalendarDate(fixedDec31);
            if (!(transitionYear || isTransitionYear(d.getNormalizedYear()))) {
                prevJan1 = fixedDateJan1 - 365;
                if (d.isLeapYear()) {
                    --prevJan1;
                }
            } else if (transitionYear) {
                if (jdate.getYear() == 1) {
                    // future.
                    if (era > HEISEI) {
                        CalendarDate pd = eras[era - 1].getSinceDate();
                        if (normalizedYear == pd.getYear()) {
                            d.setMonth(pd.getMonth()).setDayOfMonth(pd.getDayOfMonth());
                        }
                    } else {
                        d.setMonth(LocalGregorianCalendar.JANUARY).setDayOfMonth(1);
                    }
                    jcal.normalize(d);
                    prevJan1 = jcal.getFixedDate(d);
                } else {
                    prevJan1 = fixedDateJan1 - 365;
                    if (d.isLeapYear()) {
                        --prevJan1;
                    }
                }
            } else {
                CalendarDate cd = eras[getEraIndex(jdate)].getSinceDate();
                d.setMonth(cd.getMonth()).setDayOfMonth(cd.getDayOfMonth());
                jcal.normalize(d);
                prevJan1 = jcal.getFixedDate(d);
            }
            weekOfYear = getWeekNumber(prevJan1, fixedDec31);
        } else {
            if (!transitionYear) {
                // Regular years
                if (weekOfYear >= 52) {
                    long nextJan1 = fixedDateJan1 + 365;
                    if (jdate.isLeapYear()) {
                        nextJan1++;
                    }
                    long nextJan1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6, getFirstDayOfWeek());
                    int ndays = (int) (nextJan1st - nextJan1);
                    if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
                        // The first days forms a week in which the date is included.
                        weekOfYear = 1;
                    }
                }
            } else {
                LocalGregorianCalendar.Date d = (LocalGregorianCalendar.Date) jdate.clone();
                long nextJan1;
                if (jdate.getYear() == 1) {
                    d.addYear(+1);
                    d.setMonth(LocalGregorianCalendar.JANUARY).setDayOfMonth(1);
                    nextJan1 = jcal.getFixedDate(d);
                } else {
                    int nextEraIndex = getEraIndex(d) + 1;
                    CalendarDate cd = eras[nextEraIndex].getSinceDate();
                    d.setEra(eras[nextEraIndex]);
                    d.setDate(1, cd.getMonth(), cd.getDayOfMonth());
                    jcal.normalize(d);
                    nextJan1 = jcal.getFixedDate(d);
                }
                long nextJan1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6, getFirstDayOfWeek());
                int ndays = (int) (nextJan1st - nextJan1);
                if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
                    // The first days forms a week in which the date is included.
                    weekOfYear = 1;
                }
            }
        }
        internalSet(WEEK_OF_YEAR, weekOfYear);
        internalSet(WEEK_OF_MONTH, getWeekNumber(fixedDateMonth1, fixedDate));
        mask |= (DAY_OF_YEAR_MASK | WEEK_OF_YEAR_MASK | WEEK_OF_MONTH_MASK | DAY_OF_WEEK_IN_MONTH_MASK);
    }
    return mask;
}
Also used : CalendarDate(sun.util.calendar.CalendarDate) LocalGregorianCalendar(sun.util.calendar.LocalGregorianCalendar) ZoneInfo(sun.util.calendar.ZoneInfo) CalendarDate(sun.util.calendar.CalendarDate)

Example 2 with ZoneInfo

use of sun.util.calendar.ZoneInfo in project jdk8u_jdk by JetBrains.

the class GregorianCalendar method computeFields.

/**
     * This computeFields implements the conversion from UTC
     * (millisecond offset from the Epoch) to calendar
     * field values. fieldMask specifies which fields to change the
     * setting state to COMPUTED, although all fields are set to
     * the correct values. This is required to fix 4685354.
     *
     * @param fieldMask a bit mask to specify which fields to change
     * the setting state.
     * @param tzMask a bit mask to specify which time zone offset
     * fields to be used for time calculations
     * @return a new field mask that indicates what field values have
     * actually been set.
     */
private int computeFields(int fieldMask, int tzMask) {
    int zoneOffset = 0;
    TimeZone tz = getZone();
    if (zoneOffsets == null) {
        zoneOffsets = new int[2];
    }
    if (tzMask != (ZONE_OFFSET_MASK | DST_OFFSET_MASK)) {
        if (tz instanceof ZoneInfo) {
            zoneOffset = ((ZoneInfo) tz).getOffsets(time, zoneOffsets);
        } else {
            zoneOffset = tz.getOffset(time);
            zoneOffsets[0] = tz.getRawOffset();
            zoneOffsets[1] = zoneOffset - zoneOffsets[0];
        }
    }
    if (tzMask != 0) {
        if (isFieldSet(tzMask, ZONE_OFFSET)) {
            zoneOffsets[0] = internalGet(ZONE_OFFSET);
        }
        if (isFieldSet(tzMask, DST_OFFSET)) {
            zoneOffsets[1] = internalGet(DST_OFFSET);
        }
        zoneOffset = zoneOffsets[0] + zoneOffsets[1];
    }
    // By computing time and zoneOffset separately, we can take
    // the wider range of time+zoneOffset than the previous
    // implementation.
    long fixedDate = zoneOffset / ONE_DAY;
    int timeOfDay = zoneOffset % (int) ONE_DAY;
    fixedDate += time / ONE_DAY;
    timeOfDay += (int) (time % ONE_DAY);
    if (timeOfDay >= ONE_DAY) {
        timeOfDay -= ONE_DAY;
        ++fixedDate;
    } else {
        while (timeOfDay < 0) {
            timeOfDay += ONE_DAY;
            --fixedDate;
        }
    }
    fixedDate += EPOCH_OFFSET;
    int era = CE;
    int year;
    if (fixedDate >= gregorianCutoverDate) {
        // Handle Gregorian dates.
        assert cachedFixedDate == Long.MIN_VALUE || gdate.isNormalized() : "cache control: not normalized";
        assert cachedFixedDate == Long.MIN_VALUE || gcal.getFixedDate(gdate.getNormalizedYear(), gdate.getMonth(), gdate.getDayOfMonth(), gdate) == cachedFixedDate : "cache control: inconsictency" + ", cachedFixedDate=" + cachedFixedDate + ", computed=" + gcal.getFixedDate(gdate.getNormalizedYear(), gdate.getMonth(), gdate.getDayOfMonth(), gdate) + ", date=" + gdate;
        // See if we can use gdate to avoid date calculation.
        if (fixedDate != cachedFixedDate) {
            gcal.getCalendarDateFromFixedDate(gdate, fixedDate);
            cachedFixedDate = fixedDate;
        }
        year = gdate.getYear();
        if (year <= 0) {
            year = 1 - year;
            era = BCE;
        }
        calsys = gcal;
        cdate = gdate;
        assert cdate.getDayOfWeek() > 0 : "dow=" + cdate.getDayOfWeek() + ", date=" + cdate;
    } else {
        // Handle Julian calendar dates.
        calsys = getJulianCalendarSystem();
        cdate = (BaseCalendar.Date) jcal.newCalendarDate(getZone());
        jcal.getCalendarDateFromFixedDate(cdate, fixedDate);
        Era e = cdate.getEra();
        if (e == jeras[0]) {
            era = BCE;
        }
        year = cdate.getYear();
    }
    // Always set the ERA and YEAR values.
    internalSet(ERA, era);
    internalSet(YEAR, year);
    int mask = fieldMask | (ERA_MASK | YEAR_MASK);
    // 0-based
    int month = cdate.getMonth() - 1;
    int dayOfMonth = cdate.getDayOfMonth();
    // Set the basic date fields.
    if ((fieldMask & (MONTH_MASK | DAY_OF_MONTH_MASK | DAY_OF_WEEK_MASK)) != 0) {
        internalSet(MONTH, month);
        internalSet(DAY_OF_MONTH, dayOfMonth);
        internalSet(DAY_OF_WEEK, cdate.getDayOfWeek());
        mask |= MONTH_MASK | DAY_OF_MONTH_MASK | DAY_OF_WEEK_MASK;
    }
    if ((fieldMask & (HOUR_OF_DAY_MASK | AM_PM_MASK | HOUR_MASK | MINUTE_MASK | SECOND_MASK | MILLISECOND_MASK)) != 0) {
        if (timeOfDay != 0) {
            int hours = timeOfDay / ONE_HOUR;
            internalSet(HOUR_OF_DAY, hours);
            // Assume AM == 0
            internalSet(AM_PM, hours / 12);
            internalSet(HOUR, hours % 12);
            int r = timeOfDay % ONE_HOUR;
            internalSet(MINUTE, r / ONE_MINUTE);
            r %= ONE_MINUTE;
            internalSet(SECOND, r / ONE_SECOND);
            internalSet(MILLISECOND, r % ONE_SECOND);
        } else {
            internalSet(HOUR_OF_DAY, 0);
            internalSet(AM_PM, AM);
            internalSet(HOUR, 0);
            internalSet(MINUTE, 0);
            internalSet(SECOND, 0);
            internalSet(MILLISECOND, 0);
        }
        mask |= (HOUR_OF_DAY_MASK | AM_PM_MASK | HOUR_MASK | MINUTE_MASK | SECOND_MASK | MILLISECOND_MASK);
    }
    if ((fieldMask & (ZONE_OFFSET_MASK | DST_OFFSET_MASK)) != 0) {
        internalSet(ZONE_OFFSET, zoneOffsets[0]);
        internalSet(DST_OFFSET, zoneOffsets[1]);
        mask |= (ZONE_OFFSET_MASK | DST_OFFSET_MASK);
    }
    if ((fieldMask & (DAY_OF_YEAR_MASK | WEEK_OF_YEAR_MASK | WEEK_OF_MONTH_MASK | DAY_OF_WEEK_IN_MONTH_MASK)) != 0) {
        int normalizedYear = cdate.getNormalizedYear();
        long fixedDateJan1 = calsys.getFixedDate(normalizedYear, 1, 1, cdate);
        int dayOfYear = (int) (fixedDate - fixedDateJan1) + 1;
        long fixedDateMonth1 = fixedDate - dayOfMonth + 1;
        int cutoverGap = 0;
        int cutoverYear = (calsys == gcal) ? gregorianCutoverYear : gregorianCutoverYearJulian;
        int relativeDayOfMonth = dayOfMonth - 1;
        // If we are in the cutover year, we need some special handling.
        if (normalizedYear == cutoverYear) {
            // Need to take care of the "missing" days.
            if (gregorianCutoverYearJulian <= gregorianCutoverYear) {
                // We need to find out where we are. The cutover
                // gap could even be more than one year.  (One
                // year difference in ~48667 years.)
                fixedDateJan1 = getFixedDateJan1(cdate, fixedDate);
                if (fixedDate >= gregorianCutoverDate) {
                    fixedDateMonth1 = getFixedDateMonth1(cdate, fixedDate);
                }
            }
            int realDayOfYear = (int) (fixedDate - fixedDateJan1) + 1;
            cutoverGap = dayOfYear - realDayOfYear;
            dayOfYear = realDayOfYear;
            relativeDayOfMonth = (int) (fixedDate - fixedDateMonth1);
        }
        internalSet(DAY_OF_YEAR, dayOfYear);
        internalSet(DAY_OF_WEEK_IN_MONTH, relativeDayOfMonth / 7 + 1);
        int weekOfYear = getWeekNumber(fixedDateJan1, fixedDate);
        // ISO8601-style. This creates problems, though.
        if (weekOfYear == 0) {
            // If the date belongs to the last week of the
            // previous year, use the week number of "12/31" of
            // the "previous" year. Again, if the previous year is
            // the Gregorian cutover year, we need to take care of
            // it.  Usually the previous day of January 1 is
            // December 31, which is not always true in
            // GregorianCalendar.
            long fixedDec31 = fixedDateJan1 - 1;
            long prevJan1 = fixedDateJan1 - 365;
            if (normalizedYear > (cutoverYear + 1)) {
                if (CalendarUtils.isGregorianLeapYear(normalizedYear - 1)) {
                    --prevJan1;
                }
            } else if (normalizedYear <= gregorianCutoverYearJulian) {
                if (CalendarUtils.isJulianLeapYear(normalizedYear - 1)) {
                    --prevJan1;
                }
            } else {
                BaseCalendar calForJan1 = calsys;
                //int prevYear = normalizedYear - 1;
                int prevYear = getCalendarDate(fixedDec31).getNormalizedYear();
                if (prevYear == gregorianCutoverYear) {
                    calForJan1 = getCutoverCalendarSystem();
                    if (calForJan1 == jcal) {
                        prevJan1 = calForJan1.getFixedDate(prevYear, BaseCalendar.JANUARY, 1, null);
                    } else {
                        prevJan1 = gregorianCutoverDate;
                        calForJan1 = gcal;
                    }
                } else if (prevYear <= gregorianCutoverYearJulian) {
                    calForJan1 = getJulianCalendarSystem();
                    prevJan1 = calForJan1.getFixedDate(prevYear, BaseCalendar.JANUARY, 1, null);
                }
            }
            weekOfYear = getWeekNumber(prevJan1, fixedDec31);
        } else {
            if (normalizedYear > gregorianCutoverYear || normalizedYear < (gregorianCutoverYearJulian - 1)) {
                // Regular years
                if (weekOfYear >= 52) {
                    long nextJan1 = fixedDateJan1 + 365;
                    if (cdate.isLeapYear()) {
                        nextJan1++;
                    }
                    long nextJan1st = BaseCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6, getFirstDayOfWeek());
                    int ndays = (int) (nextJan1st - nextJan1);
                    if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
                        // The first days forms a week in which the date is included.
                        weekOfYear = 1;
                    }
                }
            } else {
                BaseCalendar calForJan1 = calsys;
                int nextYear = normalizedYear + 1;
                if (nextYear == (gregorianCutoverYearJulian + 1) && nextYear < gregorianCutoverYear) {
                    // In case the gap is more than one year.
                    nextYear = gregorianCutoverYear;
                }
                if (nextYear == gregorianCutoverYear) {
                    calForJan1 = getCutoverCalendarSystem();
                }
                long nextJan1;
                if (nextYear > gregorianCutoverYear || gregorianCutoverYearJulian == gregorianCutoverYear || nextYear == gregorianCutoverYearJulian) {
                    nextJan1 = calForJan1.getFixedDate(nextYear, BaseCalendar.JANUARY, 1, null);
                } else {
                    nextJan1 = gregorianCutoverDate;
                    calForJan1 = gcal;
                }
                long nextJan1st = BaseCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6, getFirstDayOfWeek());
                int ndays = (int) (nextJan1st - nextJan1);
                if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
                    // The first days forms a week in which the date is included.
                    weekOfYear = 1;
                }
            }
        }
        internalSet(WEEK_OF_YEAR, weekOfYear);
        internalSet(WEEK_OF_MONTH, getWeekNumber(fixedDateMonth1, fixedDate));
        mask |= (DAY_OF_YEAR_MASK | WEEK_OF_YEAR_MASK | WEEK_OF_MONTH_MASK | DAY_OF_WEEK_IN_MONTH_MASK);
    }
    return mask;
}
Also used : BaseCalendar(sun.util.calendar.BaseCalendar) Era(sun.util.calendar.Era) ZoneInfo(sun.util.calendar.ZoneInfo)

Example 3 with ZoneInfo

use of sun.util.calendar.ZoneInfo in project jdk8u_jdk by JetBrains.

the class TimeZone method parseCustomTimeZone.

/**
     * Parses a custom time zone identifier and returns a corresponding zone.
     * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
     *
     * @param id a string of the <a href="#CustomID">custom ID form</a>.
     * @return a newly created TimeZone with the given offset and
     * no daylight saving time, or null if the id cannot be parsed.
     */
private static final TimeZone parseCustomTimeZone(String id) {
    int length;
    // start with "GMT".
    if ((length = id.length()) < (GMT_ID_LENGTH + 2) || id.indexOf(GMT_ID) != 0) {
        return null;
    }
    ZoneInfo zi;
    // First, we try to find it in the cache with the given
    // id. Even the id is not normalized, the returned ZoneInfo
    // should have its normalized id.
    zi = ZoneInfoFile.getZoneInfo(id);
    if (zi != null) {
        return zi;
    }
    int index = GMT_ID_LENGTH;
    boolean negative = false;
    char c = id.charAt(index++);
    if (c == '-') {
        negative = true;
    } else if (c != '+') {
        return null;
    }
    int hours = 0;
    int num = 0;
    int countDelim = 0;
    int len = 0;
    while (index < length) {
        c = id.charAt(index++);
        if (c == ':') {
            if (countDelim > 0) {
                return null;
            }
            if (len > 2) {
                return null;
            }
            hours = num;
            countDelim++;
            num = 0;
            len = 0;
            continue;
        }
        if (c < '0' || c > '9') {
            return null;
        }
        num = num * 10 + (c - '0');
        len++;
    }
    if (index != length) {
        return null;
    }
    if (countDelim == 0) {
        if (len <= 2) {
            hours = num;
            num = 0;
        } else {
            hours = num / 100;
            num %= 100;
        }
    } else {
        if (len != 2) {
            return null;
        }
    }
    if (hours > 23 || num > 59) {
        return null;
    }
    int gmtOffset = (hours * 60 + num) * 60 * 1000;
    if (gmtOffset == 0) {
        zi = ZoneInfoFile.getZoneInfo(GMT_ID);
        if (negative) {
            zi.setID("GMT-00:00");
        } else {
            zi.setID("GMT+00:00");
        }
    } else {
        zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset);
    }
    return zi;
}
Also used : ZoneInfo(sun.util.calendar.ZoneInfo)

Example 4 with ZoneInfo

use of sun.util.calendar.ZoneInfo in project jdk8u_jdk by JetBrains.

the class Calendar method readObject.

/**
     * Reconstitutes this object from a stream (i.e., deserialize it).
     */
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
    final ObjectInputStream input = stream;
    input.defaultReadObject();
    stamp = new int[FIELD_COUNT];
    // streamed out anymore.  We expect 'time' to be correct.
    if (serialVersionOnStream >= 2) {
        isTimeSet = true;
        if (fields == null) {
            fields = new int[FIELD_COUNT];
        }
        if (isSet == null) {
            isSet = new boolean[FIELD_COUNT];
        }
    } else if (serialVersionOnStream >= 0) {
        for (int i = 0; i < FIELD_COUNT; ++i) {
            stamp[i] = isSet[i] ? COMPUTED : UNSET;
        }
    }
    serialVersionOnStream = currentSerialVersion;
    // If there's a ZoneInfo object, use it for zone.
    ZoneInfo zi = null;
    try {
        zi = AccessController.doPrivileged(new PrivilegedExceptionAction<ZoneInfo>() {

            @Override
            public ZoneInfo run() throws Exception {
                return (ZoneInfo) input.readObject();
            }
        }, CalendarAccessControlContext.INSTANCE);
    } catch (PrivilegedActionException pae) {
        Exception e = pae.getException();
        if (!(e instanceof OptionalDataException)) {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else if (e instanceof IOException) {
                throw (IOException) e;
            } else if (e instanceof ClassNotFoundException) {
                throw (ClassNotFoundException) e;
            }
            throw new RuntimeException(e);
        }
    }
    if (zi != null) {
        zone = zi;
    }
    // implementation as much as possible.
    if (zone instanceof SimpleTimeZone) {
        String id = zone.getID();
        TimeZone tz = TimeZone.getTimeZone(id);
        if (tz != null && tz.hasSameRules(zone) && tz.getID().equals(id)) {
            zone = tz;
        }
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) OptionalDataException(java.io.OptionalDataException) ZoneInfo(sun.util.calendar.ZoneInfo) PrivilegedActionException(java.security.PrivilegedActionException) OptionalDataException(java.io.OptionalDataException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ZoneInfo (sun.util.calendar.ZoneInfo)4 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 OptionalDataException (java.io.OptionalDataException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 BaseCalendar (sun.util.calendar.BaseCalendar)1 CalendarDate (sun.util.calendar.CalendarDate)1 Era (sun.util.calendar.Era)1 LocalGregorianCalendar (sun.util.calendar.LocalGregorianCalendar)1