Search in sources :

Example 41 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 42 with ZoneOffset

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

the class TCKLocalDateTime method test_toEpochSecond_afterEpoch.

//-----------------------------------------------------------------------
// toEpochSecond()
//-----------------------------------------------------------------------
@Test
public void test_toEpochSecond_afterEpoch() {
    for (int i = -5; i < 5; i++) {
        ZoneOffset offset = ZoneOffset.ofHours(i);
        for (int j = 0; j < 100000; j++) {
            LocalDateTime a = LocalDateTime.of(1970, 1, 1, 0, 0).plusSeconds(j);
            assertEquals(a.toEpochSecond(offset), j - i * 3600);
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) ZoneOffset(java.time.ZoneOffset) Test(org.testng.annotations.Test)

Example 43 with ZoneOffset

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

the class TCKZoneOffset method test_factory_string_hours_minutes_seconds_colon.

@Test
public void test_factory_string_hours_minutes_seconds_colon() {
    for (int i = -17; i <= 17; i++) {
        for (int j = -59; j <= 59; j++) {
            for (int k = -59; k <= 59; k++) {
                if ((i < 0 && j <= 0 && k <= 0) || (i > 0 && j >= 0 && k >= 0) || (i == 0 && ((j < 0 && k <= 0) || (j > 0 && k >= 0) || j == 0))) {
                    String str = (i < 0 || j < 0 || k < 0 ? "-" : "+") + Integer.toString(Math.abs(i) + 100).substring(1) + ":" + Integer.toString(Math.abs(j) + 100).substring(1) + ":" + Integer.toString(Math.abs(k) + 100).substring(1);
                    ZoneOffset test = ZoneOffset.of(str);
                    doTestOffset(test, i, j, k);
                }
            }
        }
    }
    ZoneOffset test1 = ZoneOffset.of("-18:00:00");
    doTestOffset(test1, -18, 0, 0);
    ZoneOffset test2 = ZoneOffset.of("+18:00:00");
    doTestOffset(test2, 18, 0, 0);
}
Also used : ZoneOffset(java.time.ZoneOffset) Test(org.testng.annotations.Test)

Example 44 with ZoneOffset

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

the class TCKZoneOffset method test_adjustInto_dateOnly.

@Test(expectedExceptions = DateTimeException.class)
public void test_adjustInto_dateOnly() {
    ZoneOffset base = ZoneOffset.ofHoursMinutesSeconds(1, 1, 1);
    base.adjustInto((LocalDate.of(1909, 2, 2)));
}
Also used : ZoneOffset(java.time.ZoneOffset) Test(org.testng.annotations.Test)

Example 45 with ZoneOffset

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

the class TCKZoneOffset method test_adjustInto_OffsetDateTime.

@Test
public void test_adjustInto_OffsetDateTime() {
    ZoneOffset base = ZoneOffset.ofHoursMinutesSeconds(1, 1, 1);
    for (int i = -18; i <= 18; i++) {
        OffsetDateTime offsetDateTime_target = OffsetDateTime.of(LocalDate.of(1909, 2, 2), LocalTime.of(10, 10, 10), ZoneOffset.ofHours(i));
        OffsetDateTime offsetDateTime_result = (OffsetDateTime) base.adjustInto(offsetDateTime_target);
        assertEquals(base, offsetDateTime_result.getOffset());
        //Do not change offset of ZonedDateTime after adjustInto()
        ZonedDateTime zonedDateTime_target = offsetDateTime_target.toZonedDateTime();
        ZonedDateTime zonedDateTime_result = (ZonedDateTime) (base.adjustInto(zonedDateTime_target));
        assertEquals(zonedDateTime_target.getOffset(), zonedDateTime_result.getOffset());
    }
}
Also used : OffsetDateTime(java.time.OffsetDateTime) ZonedDateTime(java.time.ZonedDateTime) 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