Search in sources :

Example 21 with ZoneOffset

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

the class ZoneRules method getDaylightSavings.

/**
     * Gets the amount of daylight savings in use for the specified instant in this zone.
     * <p>
     * This provides access to historic information on how the amount of daylight
     * savings has changed over time.
     * This is the difference between the standard offset and the actual offset.
     * Typically the amount is zero during winter and one hour during summer.
     * Time-zones are second-based, so the nanosecond part of the duration will be zero.
     * <p>
     * This default implementation calculates the duration from the
     * {@link #getOffset(java.time.Instant) actual} and
     * {@link #getStandardOffset(java.time.Instant) standard} offsets.
     *
     * @param instant  the instant to find the daylight savings for, not null, but null
     *  may be ignored if the rules have a single offset for all instants
     * @return the difference between the standard and actual offset, not null
     */
public Duration getDaylightSavings(Instant instant) {
    if (savingsInstantTransitions.length == 0) {
        return Duration.ZERO;
    }
    ZoneOffset standardOffset = getStandardOffset(instant);
    ZoneOffset actualOffset = getOffset(instant);
    return Duration.ofSeconds(actualOffset.getTotalSeconds() - standardOffset.getTotalSeconds());
}
Also used : ZoneOffset(java.time.ZoneOffset)

Example 22 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 23 with ZoneOffset

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

the class ZoneOffsetTransition method readExternal.

/**
     * Reads the state from the stream.
     *
     * @param in  the input stream, not null
     * @return the created object, not null
     * @throws IOException if an error occurs
     */
static ZoneOffsetTransition readExternal(DataInput in) throws IOException {
    long epochSecond = Ser.readEpochSec(in);
    ZoneOffset before = Ser.readOffset(in);
    ZoneOffset after = Ser.readOffset(in);
    if (before.equals(after)) {
        throw new IllegalArgumentException("Offsets must not be equal");
    }
    return new ZoneOffsetTransition(epochSecond, before, after);
}
Also used : ZoneOffset(java.time.ZoneOffset)

Example 24 with ZoneOffset

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

the class TestDateTimeFormatterBuilder method test_appendOffset_format.

@Test(dataProvider = "offsetPatterns")
public void test_appendOffset_format(String pattern, int h, int m, int s, String expected) throws Exception {
    builder.appendOffset(pattern, "Z");
    DateTimeFormatter f = builder.toFormatter();
    ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(h, m, s);
    assertEquals(f.format(offset), expected);
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) ZoneOffset(java.time.ZoneOffset) Test(org.testng.annotations.Test)

Example 25 with ZoneOffset

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

the class TCKOffsetDateTime method now_Clock_offsets.

@Test
public void now_Clock_offsets() {
    OffsetDateTime base = OffsetDateTime.of(1970, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC);
    for (int i = -9; i < 15; i++) {
        ZoneOffset offset = ZoneOffset.ofHours(i);
        Clock clock = Clock.fixed(base.toInstant(), offset);
        OffsetDateTime test = OffsetDateTime.now(clock);
        assertEquals(test.getHour(), (12 + i) % 24);
        assertEquals(test.getMinute(), 0);
        assertEquals(test.getSecond(), 0);
        assertEquals(test.getNano(), 0);
        assertEquals(test.getOffset(), offset);
    }
}
Also used : OffsetDateTime(java.time.OffsetDateTime) Clock(java.time.Clock) ZoneOffset(java.time.ZoneOffset) Test(org.testng.annotations.Test)

Aggregations

ZoneOffset (java.time.ZoneOffset)64 Test (org.testng.annotations.Test)35 LocalDateTime (java.time.LocalDateTime)10 ZoneId (java.time.ZoneId)10 ZoneRules (java.time.zone.ZoneRules)9 ZonedDateTime (java.time.ZonedDateTime)8 Instant (java.time.Instant)6 OffsetDateTime (java.time.OffsetDateTime)5 DateTimeFormatter (java.time.format.DateTimeFormatter)5 ZoneOffsetTransition (java.time.zone.ZoneOffsetTransition)4 Clock (java.time.Clock)3 LocalTime (java.time.LocalTime)3 Duration (java.time.Duration)2 LocalDate (java.time.LocalDate)2 OffsetTime (java.time.OffsetTime)2 ChronoField (java.time.temporal.ChronoField)2 TemporalAccessor (java.time.temporal.TemporalAccessor)2 List (java.util.List)2 Cache (com.github.benmanes.caffeine.cache.Cache)1 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1