Search in sources :

Example 66 with ZoneOffset

use of java.time.ZoneOffset in project j2objc by google.

the class ChronoZonedDateTimeImpl method ofInstant.

/**
 * Obtains an instance from an instant using the specified time-zone.
 *
 * @param chrono  the chronology, not null
 * @param instant  the instant, not null
 * @param zone  the zone identifier, not null
 * @return the zoned date-time, not null
 */
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    // protect against bad ZoneRules
    Objects.requireNonNull(offset, "offset");
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>) chrono.localDateTime(ldt);
    return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
Also used : LocalDateTime(java.time.LocalDateTime) ZoneRules(java.time.zone.ZoneRules) ZoneOffset(java.time.ZoneOffset)

Example 67 with ZoneOffset

use of java.time.ZoneOffset in project j2objc by google.

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)

Example 68 with ZoneOffset

use of java.time.ZoneOffset in project j2objc by google.

the class ChronoZonedDateTimeImpl method ofBest.

// -----------------------------------------------------------------------
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    // protect against bad ZoneRules
    Objects.requireNonNull(offset, "offset");
    return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
Also used : LocalDateTime(java.time.LocalDateTime) ZoneRules(java.time.zone.ZoneRules) ZoneOffsetTransition(java.time.zone.ZoneOffsetTransition) ZoneOffset(java.time.ZoneOffset)

Example 69 with ZoneOffset

use of java.time.ZoneOffset in project j2objc by google.

the class TCKZoneRules method test_of.

// -----------------------------------------------------------------------
// of()
// -----------------------------------------------------------------------
public void test_of() {
    // used for standard offset
    ZoneOffset stdOffset1 = ZoneOffset.UTC;
    ZoneOffset stdOffset2 = ZoneOffset.ofHours(1);
    LocalDateTime time_of_stdOffsetTransition1 = LocalDateTime.of(2013, 1, 5, 1, 0);
    ZoneOffsetTransition stdOffsetTransition1 = ZoneOffsetTransition.of(time_of_stdOffsetTransition1, stdOffset1, stdOffset2);
    List<ZoneOffsetTransition> stdOffsetTransition_list = new ArrayList<ZoneOffsetTransition>();
    stdOffsetTransition_list.add(stdOffsetTransition1);
    // used for wall offset
    ZoneOffset wallOffset1 = ZoneOffset.ofHours(2);
    ZoneOffset wallOffset2 = ZoneOffset.ofHours(4);
    ZoneOffset wallOffset3 = ZoneOffset.ofHours(7);
    LocalDateTime time_of_wallOffsetTransition1 = LocalDateTime.of(2013, 2, 5, 1, 0);
    LocalDateTime time_of_wallOffsetTransition2 = LocalDateTime.of(2013, 3, 5, 1, 0);
    LocalDateTime time_of_wallOffsetTransition3 = LocalDateTime.of(2013, 10, 5, 1, 0);
    ZoneOffsetTransition wallOffsetTransition1 = ZoneOffsetTransition.of(time_of_wallOffsetTransition1, wallOffset1, wallOffset2);
    ZoneOffsetTransition wallOffsetTransition2 = ZoneOffsetTransition.of(time_of_wallOffsetTransition2, wallOffset2, wallOffset3);
    ZoneOffsetTransition wallOffsetTransition3 = ZoneOffsetTransition.of(time_of_wallOffsetTransition3, wallOffset3, wallOffset1);
    List<ZoneOffsetTransition> wallOffsetTransition_list = new ArrayList<ZoneOffsetTransition>();
    wallOffsetTransition_list.add(wallOffsetTransition1);
    wallOffsetTransition_list.add(wallOffsetTransition2);
    wallOffsetTransition_list.add(wallOffsetTransition3);
    // used for ZoneOffsetTransitionRule
    ZoneOffset ruleOffset = ZoneOffset.ofHours(3);
    ZoneOffsetTransitionRule.TimeDefinition timeDefinition = ZoneOffsetTransitionRule.TimeDefinition.valueOf("WALL");
    ZoneOffsetTransitionRule rule1 = ZoneOffsetTransitionRule.of(Month.FEBRUARY, 2, DayOfWeek.MONDAY, LocalTime.of(1, 0), false, timeDefinition, ZoneOffset.UTC, ZoneOffset.UTC, ruleOffset);
    List<ZoneOffsetTransitionRule> rule_list = new ArrayList<ZoneOffsetTransitionRule>();
    rule_list.add(rule1);
    // Begin verification
    ZoneRules zoneRule = ZoneRules.of(stdOffset1, wallOffset1, stdOffsetTransition_list, wallOffsetTransition_list, rule_list);
    OffsetDateTime before_time_of_stdOffsetTransition1 = OffsetDateTime.of(time_of_stdOffsetTransition1, stdOffset1).minusSeconds(1);
    OffsetDateTime after_time_of_stdOffsetTransition1 = OffsetDateTime.of(time_of_stdOffsetTransition1, stdOffset1).plusSeconds(1);
    ;
    assertEquals(zoneRule.getStandardOffset(before_time_of_stdOffsetTransition1.toInstant()), stdOffset1);
    assertEquals(zoneRule.getStandardOffset(after_time_of_stdOffsetTransition1.toInstant()), stdOffset2);
    OffsetDateTime before_time_of_wallOffsetTransition1 = OffsetDateTime.of(time_of_wallOffsetTransition1, wallOffset1).minusSeconds(1);
    OffsetDateTime after_time_of_wallOffsetTransition1 = OffsetDateTime.of(time_of_wallOffsetTransition1, wallOffset1).plusSeconds(1);
    assertEquals(zoneRule.nextTransition(before_time_of_wallOffsetTransition1.toInstant()), wallOffsetTransition1);
    assertEquals(zoneRule.nextTransition(after_time_of_wallOffsetTransition1.toInstant()), wallOffsetTransition2);
    OffsetDateTime before_time_of_wallOffsetTransition2 = OffsetDateTime.of(time_of_wallOffsetTransition2, wallOffset2).minusSeconds(1);
    OffsetDateTime after_time_of_wallOffsetTransition2 = OffsetDateTime.of(time_of_wallOffsetTransition2, wallOffset2).plusSeconds(1);
    assertEquals(zoneRule.nextTransition(before_time_of_wallOffsetTransition2.toInstant()), wallOffsetTransition2);
    assertEquals(zoneRule.nextTransition(after_time_of_wallOffsetTransition2.toInstant()), wallOffsetTransition3);
    OffsetDateTime before_time_of_wallOffsetTransition3 = OffsetDateTime.of(time_of_wallOffsetTransition3, wallOffset3).minusSeconds(1);
    OffsetDateTime after_time_of_wallOffsetTransition3 = OffsetDateTime.of(time_of_wallOffsetTransition3, wallOffset3).plusSeconds(1);
    assertEquals(zoneRule.nextTransition(before_time_of_wallOffsetTransition3.toInstant()), wallOffsetTransition3);
    assertEquals(zoneRule.nextTransition(after_time_of_wallOffsetTransition3.toInstant()), rule1.createTransition(2014));
}
Also used : LocalDateTime(java.time.LocalDateTime) ZoneOffsetTransitionRule(java.time.zone.ZoneOffsetTransitionRule) OffsetDateTime(java.time.OffsetDateTime) ArrayList(java.util.ArrayList) ZoneRules(java.time.zone.ZoneRules) ZoneOffsetTransition(java.time.zone.ZoneOffsetTransition) TimeDefinition(java.time.zone.ZoneOffsetTransitionRule.TimeDefinition) ZoneOffset(java.time.ZoneOffset)

Example 70 with ZoneOffset

use of java.time.ZoneOffset in project j2objc by google.

the class TCKZoneRules method test_NewYork_getOffset_fromDST.

public void test_NewYork_getOffset_fromDST() {
    ZoneRules test = americaNewYork();
    ZoneOffset offset = ZoneOffset.ofHours(-4);
    assertEquals(test.getOffset(createInstant(2008, 11, 1, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 11, 2, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 11, 3, offset)), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(createInstant(2008, 11, 4, offset)), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(createInstant(2008, 11, 5, offset)), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(createInstant(2008, 11, 6, offset)), ZoneOffset.ofHours(-5));
    assertEquals(test.getOffset(createInstant(2008, 11, 7, offset)), ZoneOffset.ofHours(-5));
    // cutover at 02:00 local
    assertEquals(test.getOffset(createInstant(2008, 11, 2, 1, 59, 59, 999999999, offset)), ZoneOffset.ofHours(-4));
    assertEquals(test.getOffset(createInstant(2008, 11, 2, 2, 0, 0, 0, offset)), ZoneOffset.ofHours(-5));
}
Also used : ZoneRules(java.time.zone.ZoneRules) ZoneOffset(java.time.ZoneOffset)

Aggregations

ZoneOffset (java.time.ZoneOffset)201 Test (org.junit.Test)51 LocalDateTime (java.time.LocalDateTime)36 ZoneId (java.time.ZoneId)36 Test (org.testng.annotations.Test)35 ZonedDateTime (java.time.ZonedDateTime)27 LocalTime (java.time.LocalTime)25 ZoneRules (java.time.zone.ZoneRules)23 OffsetDateTime (java.time.OffsetDateTime)20 Instant (java.time.Instant)17 LocalDate (java.time.LocalDate)16 TemporalAccessor (java.time.temporal.TemporalAccessor)11 ZoneOffsetTransition (java.time.zone.ZoneOffsetTransition)11 OffsetTime (java.time.OffsetTime)10 DateTimeFormatter (java.time.format.DateTimeFormatter)10 ArrayList (java.util.ArrayList)8 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)7 List (java.util.List)6 BigDecimal (java.math.BigDecimal)5 Clock (java.time.Clock)5