Search in sources :

Example 26 with ZoneOffset

use of java.time.ZoneOffset in project jdk8u_jdk by JetBrains.

the class ZoneRules method previousTransition.

/**
     * Gets the previous transition before the specified instant.
     * <p>
     * This returns details of the previous transition after the specified instant.
     * For example, if the instant represents a point where "summer" daylight saving time
     * applies, then the method will return the transition from the previous "winter" time.
     *
     * @param instant  the instant to get the previous transition after, not null, but null
     *  may be ignored if the rules have a single offset for all instants
     * @return the previous transition after the specified instant, null if this is before the first transition
     */
public ZoneOffsetTransition previousTransition(Instant instant) {
    if (savingsInstantTransitions.length == 0) {
        return null;
    }
    long epochSec = instant.getEpochSecond();
    if (instant.getNano() > 0 && epochSec < Long.MAX_VALUE) {
        // allow rest of method to only use seconds
        epochSec += 1;
    }
    // check if using last rules
    long lastHistoric = savingsInstantTransitions[savingsInstantTransitions.length - 1];
    if (lastRules.length > 0 && epochSec > lastHistoric) {
        // search year the instant is in
        ZoneOffset lastHistoricOffset = wallOffsets[wallOffsets.length - 1];
        int year = findYear(epochSec, lastHistoricOffset);
        ZoneOffsetTransition[] transArray = findTransitionArray(year);
        for (int i = transArray.length - 1; i >= 0; i--) {
            if (epochSec > transArray[i].toEpochSecond()) {
                return transArray[i];
            }
        }
        // use last from preceding year
        int lastHistoricYear = findYear(lastHistoric, lastHistoricOffset);
        if (--year > lastHistoricYear) {
            transArray = findTransitionArray(year);
            return transArray[transArray.length - 1];
        }
    // drop through
    }
    // using historic rules
    int index = Arrays.binarySearch(savingsInstantTransitions, epochSec);
    if (index < 0) {
        index = -index - 1;
    }
    if (index <= 0) {
        return null;
    }
    return new ZoneOffsetTransition(savingsInstantTransitions[index - 1], wallOffsets[index - 1], wallOffsets[index]);
}
Also used : ZoneOffset(java.time.ZoneOffset)

Example 27 with ZoneOffset

use of java.time.ZoneOffset in project jdk8u_jdk by JetBrains.

the class Parsed method resolveInstantFields.

//-----------------------------------------------------------------------
private void resolveInstantFields() {
    // resolve parsed instant seconds to date and time if zone available
    if (fieldValues.containsKey(INSTANT_SECONDS)) {
        if (zone != null) {
            resolveInstantFields0(zone);
        } else {
            Long offsetSecs = fieldValues.get(OFFSET_SECONDS);
            if (offsetSecs != null) {
                ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSecs.intValue());
                resolveInstantFields0(offset);
            }
        }
    }
}
Also used : ZoneOffset(java.time.ZoneOffset)

Example 28 with ZoneOffset

use of java.time.ZoneOffset in project jdk8u_jdk by JetBrains.

the class Parsed method resolveInstant.

private void resolveInstant() {
    // add instant seconds if we have date, time and zone
    if (date != null && time != null) {
        if (zone != null) {
            long instant = date.atTime(time).atZone(zone).getLong(ChronoField.INSTANT_SECONDS);
            fieldValues.put(INSTANT_SECONDS, instant);
        } else {
            Long offsetSecs = fieldValues.get(OFFSET_SECONDS);
            if (offsetSecs != null) {
                ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSecs.intValue());
                long instant = date.atTime(time).atZone(offset).getLong(ChronoField.INSTANT_SECONDS);
                fieldValues.put(INSTANT_SECONDS, instant);
            }
        }
    }
}
Also used : ZoneOffset(java.time.ZoneOffset)

Example 29 with ZoneOffset

use of java.time.ZoneOffset in project jdk8u_jdk by JetBrains.

the class ZoneRules method getOffsetInfo.

private Object getOffsetInfo(LocalDateTime dt) {
    if (savingsInstantTransitions.length == 0) {
        return standardOffsets[0];
    }
    // check if using last rules
    if (lastRules.length > 0 && dt.isAfter(savingsLocalTransitions[savingsLocalTransitions.length - 1])) {
        ZoneOffsetTransition[] transArray = findTransitionArray(dt.getYear());
        Object info = null;
        for (ZoneOffsetTransition trans : transArray) {
            info = findOffsetInfo(dt, trans);
            if (info instanceof ZoneOffsetTransition || info.equals(trans.getOffsetBefore())) {
                return info;
            }
        }
        return info;
    }
    // using historic rules
    int index = Arrays.binarySearch(savingsLocalTransitions, dt);
    if (index == -1) {
        // before first transition
        return wallOffsets[0];
    }
    if (index < 0) {
        // switch negative insert position to start of matched range
        index = -index - 2;
    } else if (index < savingsLocalTransitions.length - 1 && savingsLocalTransitions[index].equals(savingsLocalTransitions[index + 1])) {
        // handle overlap immediately following gap
        index++;
    }
    if ((index & 1) == 0) {
        // gap or overlap
        LocalDateTime dtBefore = savingsLocalTransitions[index];
        LocalDateTime dtAfter = savingsLocalTransitions[index + 1];
        ZoneOffset offsetBefore = wallOffsets[index / 2];
        ZoneOffset offsetAfter = wallOffsets[index / 2 + 1];
        if (offsetAfter.getTotalSeconds() > offsetBefore.getTotalSeconds()) {
            // gap
            return new ZoneOffsetTransition(dtBefore, offsetBefore, offsetAfter);
        } else {
            // overlap
            return new ZoneOffsetTransition(dtAfter, offsetBefore, offsetAfter);
        }
    } else {
        // normal (neither gap or overlap)
        return wallOffsets[index / 2 + 1];
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) ZoneOffset(java.time.ZoneOffset)

Example 30 with ZoneOffset

use of java.time.ZoneOffset in project jdk8u_jdk by JetBrains.

the class ChronoZonedDateTimeImpl method readExternal.

static ChronoZonedDateTime<?> readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    ChronoLocalDateTime<?> dateTime = (ChronoLocalDateTime<?>) in.readObject();
    ZoneOffset offset = (ZoneOffset) in.readObject();
    ZoneId zone = (ZoneId) in.readObject();
    return dateTime.atZone(offset).withZoneSameLocal(zone);
// TODO: ZDT uses ofLenient()
}
Also used : ZoneId(java.time.ZoneId) ZoneOffset(java.time.ZoneOffset)

Aggregations

ZoneOffset (java.time.ZoneOffset)62 Test (org.testng.annotations.Test)35 LocalDateTime (java.time.LocalDateTime)9 ZoneRules (java.time.zone.ZoneRules)9 ZoneId (java.time.ZoneId)8 ZonedDateTime (java.time.ZonedDateTime)7 Instant (java.time.Instant)6 DateTimeFormatter (java.time.format.DateTimeFormatter)5 OffsetDateTime (java.time.OffsetDateTime)4 ZoneOffsetTransition (java.time.zone.ZoneOffsetTransition)4 Clock (java.time.Clock)3 LocalTime (java.time.LocalTime)2 ChronoField (java.time.temporal.ChronoField)2 Cache (com.github.benmanes.caffeine.cache.Cache)1 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 DateTimeException (java.time.DateTimeException)1 DayOfWeek (java.time.DayOfWeek)1 Duration (java.time.Duration)1 LocalDate (java.time.LocalDate)1 Month (java.time.Month)1