Search in sources :

Example 36 with BaseCalendar

use of sun.util.calendar.BaseCalendar in project Bytecoder by mirkosertic.

the class GregorianCalendar method getCalendarDate.

/**
 * Returns a CalendarDate produced from the specified fixed date.
 *
 * @param fd the fixed date
 */
private BaseCalendar.Date getCalendarDate(long fd) {
    BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
    BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
    cal.getCalendarDateFromFixedDate(d, fd);
    return d;
}
Also used : BaseCalendar(sun.util.calendar.BaseCalendar) CalendarDate(sun.util.calendar.CalendarDate)

Example 37 with BaseCalendar

use of sun.util.calendar.BaseCalendar in project Bytecoder by mirkosertic.

the class GregorianCalendar method getActualMaximum.

/**
 * Returns the maximum value that this calendar field could have,
 * taking into consideration the given time value and the current
 * values of the
 * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
 * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
 * {@link #getGregorianChange() getGregorianChange} and
 * {@link Calendar#getTimeZone() getTimeZone} methods.
 * For example, if the date of this instance is February 1, 2004,
 * the actual maximum value of the <code>DAY_OF_MONTH</code> field
 * is 29 because 2004 is a leap year, and if the date of this
 * instance is February 1, 2005, it's 28.
 *
 * <p>This method calculates the maximum value of {@link
 * Calendar#WEEK_OF_YEAR WEEK_OF_YEAR} based on the {@link
 * Calendar#YEAR YEAR} (calendar year) value, not the <a
 * href="#week_year">week year</a>. Call {@link
 * #getWeeksInWeekYear()} to get the maximum value of {@code
 * WEEK_OF_YEAR} in the week year of this {@code GregorianCalendar}.
 *
 * @param field the calendar field
 * @return the maximum of the given field for the time value of
 * this <code>GregorianCalendar</code>
 * @see #getMinimum(int)
 * @see #getMaximum(int)
 * @see #getGreatestMinimum(int)
 * @see #getLeastMaximum(int)
 * @see #getActualMinimum(int)
 * @since 1.2
 */
@Override
public int getActualMaximum(int field) {
    final int fieldsForFixedMax = ERA_MASK | DAY_OF_WEEK_MASK | HOUR_MASK | AM_PM_MASK | HOUR_OF_DAY_MASK | MINUTE_MASK | SECOND_MASK | MILLISECOND_MASK | ZONE_OFFSET_MASK | DST_OFFSET_MASK;
    if ((fieldsForFixedMax & (1 << field)) != 0) {
        return getMaximum(field);
    }
    GregorianCalendar gc = getNormalizedCalendar();
    BaseCalendar.Date date = gc.cdate;
    BaseCalendar cal = gc.calsys;
    int normalizedYear = date.getNormalizedYear();
    int value = -1;
    switch(field) {
        case MONTH:
            {
                if (!gc.isCutoverYear(normalizedYear)) {
                    value = DECEMBER;
                    break;
                }
                // January 1 of the next year may or may not exist.
                long nextJan1;
                do {
                    nextJan1 = gcal.getFixedDate(++normalizedYear, BaseCalendar.JANUARY, 1, null);
                } while (nextJan1 < gregorianCutoverDate);
                BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
                cal.getCalendarDateFromFixedDate(d, nextJan1 - 1);
                value = d.getMonth() - 1;
            }
            break;
        case DAY_OF_MONTH:
            {
                value = cal.getMonthLength(date);
                if (!gc.isCutoverYear(normalizedYear) || date.getDayOfMonth() == value) {
                    break;
                }
                // Handle cutover year.
                long fd = gc.getCurrentFixedDate();
                if (fd >= gregorianCutoverDate) {
                    break;
                }
                int monthLength = gc.actualMonthLength();
                long monthEnd = gc.getFixedDateMonth1(gc.cdate, fd) + monthLength - 1;
                // Convert the fixed date to its calendar date.
                BaseCalendar.Date d = gc.getCalendarDate(monthEnd);
                value = d.getDayOfMonth();
            }
            break;
        case DAY_OF_YEAR:
            {
                if (!gc.isCutoverYear(normalizedYear)) {
                    value = cal.getYearLength(date);
                    break;
                }
                // Handle cutover year.
                long jan1;
                if (gregorianCutoverYear == gregorianCutoverYearJulian) {
                    BaseCalendar cocal = gc.getCutoverCalendarSystem();
                    jan1 = cocal.getFixedDate(normalizedYear, 1, 1, null);
                } else if (normalizedYear == gregorianCutoverYearJulian) {
                    jan1 = cal.getFixedDate(normalizedYear, 1, 1, null);
                } else {
                    jan1 = gregorianCutoverDate;
                }
                // January 1 of the next year may or may not exist.
                long nextJan1 = gcal.getFixedDate(++normalizedYear, 1, 1, null);
                if (nextJan1 < gregorianCutoverDate) {
                    nextJan1 = gregorianCutoverDate;
                }
                assert jan1 <= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(), date.getDayOfMonth(), date);
                assert nextJan1 >= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(), date.getDayOfMonth(), date);
                value = (int) (nextJan1 - jan1);
            }
            break;
        case WEEK_OF_YEAR:
            {
                if (!gc.isCutoverYear(normalizedYear)) {
                    // Get the day of week of January 1 of the year
                    CalendarDate d = cal.newCalendarDate(TimeZone.NO_TIMEZONE);
                    d.setDate(date.getYear(), BaseCalendar.JANUARY, 1);
                    int dayOfWeek = cal.getDayOfWeek(d);
                    // Normalize the day of week with the firstDayOfWeek value
                    dayOfWeek -= getFirstDayOfWeek();
                    if (dayOfWeek < 0) {
                        dayOfWeek += 7;
                    }
                    value = 52;
                    int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1;
                    if ((magic == 6) || (date.isLeapYear() && (magic == 5 || magic == 12))) {
                        value++;
                    }
                    break;
                }
                if (gc == this) {
                    gc = (GregorianCalendar) gc.clone();
                }
                int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);
                gc.set(DAY_OF_YEAR, maxDayOfYear);
                value = gc.get(WEEK_OF_YEAR);
                if (internalGet(YEAR) != gc.getWeekYear()) {
                    gc.set(DAY_OF_YEAR, maxDayOfYear - 7);
                    value = gc.get(WEEK_OF_YEAR);
                }
            }
            break;
        case WEEK_OF_MONTH:
            {
                if (!gc.isCutoverYear(normalizedYear)) {
                    CalendarDate d = cal.newCalendarDate(null);
                    d.setDate(date.getYear(), date.getMonth(), 1);
                    int dayOfWeek = cal.getDayOfWeek(d);
                    int monthLength = cal.getMonthLength(d);
                    dayOfWeek -= getFirstDayOfWeek();
                    if (dayOfWeek < 0) {
                        dayOfWeek += 7;
                    }
                    // # of days in the first week
                    int nDaysFirstWeek = 7 - dayOfWeek;
                    value = 3;
                    if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) {
                        value++;
                    }
                    monthLength -= nDaysFirstWeek + 7 * 3;
                    if (monthLength > 0) {
                        value++;
                        if (monthLength > 7) {
                            value++;
                        }
                    }
                    break;
                }
                // Cutover year handling
                if (gc == this) {
                    gc = (GregorianCalendar) gc.clone();
                }
                int y = gc.internalGet(YEAR);
                int m = gc.internalGet(MONTH);
                do {
                    value = gc.get(WEEK_OF_MONTH);
                    gc.add(WEEK_OF_MONTH, +1);
                } while (gc.get(YEAR) == y && gc.get(MONTH) == m);
            }
            break;
        case DAY_OF_WEEK_IN_MONTH:
            {
                // may be in the Gregorian cutover month
                int ndays, dow1;
                int dow = date.getDayOfWeek();
                if (!gc.isCutoverYear(normalizedYear)) {
                    BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
                    ndays = cal.getMonthLength(d);
                    d.setDayOfMonth(1);
                    cal.normalize(d);
                    dow1 = d.getDayOfWeek();
                } else {
                    // Let a cloned GregorianCalendar take care of the cutover cases.
                    if (gc == this) {
                        gc = (GregorianCalendar) clone();
                    }
                    ndays = gc.actualMonthLength();
                    gc.set(DAY_OF_MONTH, gc.getActualMinimum(DAY_OF_MONTH));
                    dow1 = gc.get(DAY_OF_WEEK);
                }
                int x = dow - dow1;
                if (x < 0) {
                    x += 7;
                }
                ndays -= x;
                value = (ndays + 6) / 7;
            }
            break;
        case YEAR:
            /* The year computation is no different, in principle, from the
             * others, however, the range of possible maxima is large.  In
             * addition, the way we know we've exceeded the range is different.
             * For these reasons, we use the special case code below to handle
             * this field.
             *
             * The actual maxima for YEAR depend on the type of calendar:
             *
             *     Gregorian = May 17, 292275056 BCE - Aug 17, 292278994 CE
             *     Julian    = Dec  2, 292269055 BCE - Jan  3, 292272993 CE
             *     Hybrid    = Dec  2, 292269055 BCE - Aug 17, 292278994 CE
             *
             * We know we've exceeded the maximum when either the month, date,
             * time, or era changes in response to setting the year.  We don't
             * check for month, date, and time here because the year and era are
             * sufficient to detect an invalid year setting.  NOTE: If code is
             * added to check the month and date in the future for some reason,
             * Feb 29 must be allowed to shift to Mar 1 when setting the year.
             */
            {
                if (gc == this) {
                    gc = (GregorianCalendar) clone();
                }
                // Calculate the millisecond offset from the beginning
                // of the year of this calendar and adjust the max
                // year value if we are beyond the limit in the max
                // year.
                long current = gc.getYearOffsetInMillis();
                if (gc.internalGetEra() == CE) {
                    gc.setTimeInMillis(Long.MAX_VALUE);
                    value = gc.get(YEAR);
                    long maxEnd = gc.getYearOffsetInMillis();
                    if (current > maxEnd) {
                        value--;
                    }
                } else {
                    CalendarSystem mincal = gc.getTimeInMillis() >= gregorianCutover ? gcal : getJulianCalendarSystem();
                    CalendarDate d = mincal.getCalendarDate(Long.MIN_VALUE, getZone());
                    long maxEnd = (cal.getDayOfYear(d) - 1) * 24 + d.getHours();
                    maxEnd *= 60;
                    maxEnd += d.getMinutes();
                    maxEnd *= 60;
                    maxEnd += d.getSeconds();
                    maxEnd *= 1000;
                    maxEnd += d.getMillis();
                    value = d.getYear();
                    if (value <= 0) {
                        assert mincal == gcal;
                        value = 1 - value;
                    }
                    if (current < maxEnd) {
                        value--;
                    }
                }
            }
            break;
        default:
            throw new ArrayIndexOutOfBoundsException(field);
    }
    return value;
}
Also used : CalendarDate(sun.util.calendar.CalendarDate) BaseCalendar(sun.util.calendar.BaseCalendar) CalendarSystem(sun.util.calendar.CalendarSystem) CalendarDate(sun.util.calendar.CalendarDate)

Example 38 with BaseCalendar

use of sun.util.calendar.BaseCalendar in project Bytecoder by mirkosertic.

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 39 with BaseCalendar

use of sun.util.calendar.BaseCalendar in project Bytecoder by mirkosertic.

the class SimpleTimeZone method getOffset.

/**
 * Returns the difference in milliseconds between local time and
 * UTC, taking into account both the raw offset and the effect of
 * daylight saving, for the specified date and time.  This method
 * assumes that the start and end month are distinct.  It also
 * uses a default {@link GregorianCalendar} object as its
 * underlying calendar, such as for determining leap years.  Do
 * not use the result of this method with a calendar other than a
 * default <code>GregorianCalendar</code>.
 *
 * <p><em>Note:  In general, clients should use
 * <code>Calendar.get(ZONE_OFFSET) + Calendar.get(DST_OFFSET)</code>
 * instead of calling this method.</em>
 *
 * @param era       The era of the given date.
 * @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.
 * @exception       IllegalArgumentException the <code>era</code>,
 *                  <code>month</code>, <code>day</code>, <code>dayOfWeek</code>,
 *                  or <code>millis</code> parameters are out of range
 */
public int getOffset(int era, int year, int month, int day, int dayOfWeek, int millis) {
    if (era != GregorianCalendar.AD && era != GregorianCalendar.BC) {
        throw new IllegalArgumentException("Illegal era " + era);
    }
    int y = year;
    if (era == GregorianCalendar.BC) {
        // adjust y with the GregorianCalendar-style year numbering.
        y = 1 - y;
    }
    // can't be supported by the Java time system.
    if (y >= 292278994) {
        y = 2800 + y % 2800;
    } else if (y <= -292269054) {
        // y %= 28 also produces an equivalent year, but positive
        // year numbers would be convenient to use the UNIX cal
        // command.
        y = (int) CalendarUtils.mod((long) y, 28);
    }
    // convert year to its 1-based month value
    int m = month + 1;
    // First, calculate time as a Gregorian date.
    BaseCalendar cal = gcal;
    BaseCalendar.Date cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
    cdate.setDate(y, m, day);
    // normalize cdate
    long time = cal.getTime(cdate);
    // UTC time
    time += millis - rawOffset;
    // style year numbering (..., -1, 0 (BCE 1), 1, 2, ...).
    if (time < GregorianCalendar.DEFAULT_GREGORIAN_CUTOVER) {
        cal = (BaseCalendar) CalendarSystem.forName("julian");
        cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
        cdate.setNormalizedDate(y, m, day);
        time = cal.getTime(cdate) + millis - rawOffset;
    }
    if ((cdate.getNormalizedYear() != y) || (cdate.getMonth() != m) || (cdate.getDayOfMonth() != day) || // compatibility.
    (dayOfWeek < Calendar.SUNDAY || dayOfWeek > Calendar.SATURDAY) || (millis < 0 || millis >= (24 * 60 * 60 * 1000))) {
        throw new IllegalArgumentException();
    }
    if (!useDaylight || year < startYear || era != GregorianCalendar.CE) {
        return rawOffset;
    }
    return getOffset(cal, cdate, y, time);
}
Also used : BaseCalendar(sun.util.calendar.BaseCalendar)

Example 40 with BaseCalendar

use of sun.util.calendar.BaseCalendar in project j2objc by google.

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)) {
        // J2ObjC modified: Use NativeTimeZone instead of ZoneInfo.
        if (tz instanceof NativeTimeZone) {
            zoneOffset = ((NativeTimeZone) tz).getOffsetsByUtcTime(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 : NativeTimeZone(com.google.j2objc.util.NativeTimeZone) BaseCalendar(sun.util.calendar.BaseCalendar) Era(sun.util.calendar.Era) NativeTimeZone(com.google.j2objc.util.NativeTimeZone)

Aggregations

BaseCalendar (sun.util.calendar.BaseCalendar)44 CalendarDate (sun.util.calendar.CalendarDate)20 CalendarSystem (sun.util.calendar.CalendarSystem)8 LocalDate (java.time.LocalDate)6 Era (sun.util.calendar.Era)4 ZoneInfo (sun.util.calendar.ZoneInfo)3 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)1