use of java.time.zone.ZoneOffsetTransition in project jdk8u_jdk by JetBrains.
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);
}
use of java.time.zone.ZoneOffsetTransition in project jdk8u_jdk by JetBrains.
the class ZonedDateTime method ofLocal.
/**
* Obtains an instance of {@code ZonedDateTime} from a local date-time
* using the preferred offset if possible.
* <p>
* The local date-time is resolved to a single instant on the time-line.
* This is achieved by finding a valid offset from UTC/Greenwich for the local
* date-time as defined by the {@link ZoneRules rules} of the zone ID.
*<p>
* In most cases, there is only one valid offset for a local date-time.
* In the case of an overlap, where clocks are set back, there are two valid offsets.
* If the preferred offset is one of the valid offsets then it is used.
* Otherwise the earlier valid offset is used, typically corresponding to "summer".
* <p>
* In the case of a gap, where clocks jump forward, there is no valid offset.
* Instead, the local date-time is adjusted to be later by the length of the gap.
* For a typical one hour daylight savings change, the local date-time will be
* moved one hour later into the offset typically corresponding to "summer".
*
* @param localDateTime the local date-time, not null
* @param zone the time-zone, not null
* @param preferredOffset the zone offset, null if no preference
* @return the zoned date-time, not null
*/
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
Objects.requireNonNull(localDateTime, "localDateTime");
Objects.requireNonNull(zone, "zone");
if (zone instanceof ZoneOffset) {
return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
}
ZoneRules rules = zone.getRules();
List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
ZoneOffset offset;
if (validOffsets.size() == 1) {
offset = validOffsets.get(0);
} else if (validOffsets.size() == 0) {
ZoneOffsetTransition trans = rules.getTransition(localDateTime);
localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
offset = trans.getOffsetAfter();
} else {
if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
offset = preferredOffset;
} else {
// protect against bad ZoneRules
offset = Objects.requireNonNull(validOffsets.get(0), "offset");
}
}
return new ZonedDateTime(localDateTime, offset, zone);
}
use of java.time.zone.ZoneOffsetTransition in project jdk8u_jdk by JetBrains.
the class LocalDate method atStartOfDay.
/**
* Returns a zoned date-time from this date at the earliest valid time according
* to the rules in the time-zone.
* <p>
* Time-zone rules, such as daylight savings, mean that not every local date-time
* is valid for the specified zone, thus the local date-time may not be midnight.
* <p>
* In most cases, there is only one valid offset for a local date-time.
* In the case of an overlap, there are two valid offsets, and the earlier one is used,
* corresponding to the first occurrence of midnight on the date.
* In the case of a gap, the zoned date-time will represent the instant just after the gap.
* <p>
* If the zone ID is a {@link ZoneOffset}, then the result always has a time of midnight.
* <p>
* To convert to a specific time in a given time-zone call {@link #atTime(LocalTime)}
* followed by {@link LocalDateTime#atZone(ZoneId)}.
*
* @param zone the zone ID to use, not null
* @return the zoned date-time formed from this date and the earliest valid time for the zone, not null
*/
public ZonedDateTime atStartOfDay(ZoneId zone) {
Objects.requireNonNull(zone, "zone");
// need to handle case where there is a gap from 11:30 to 00:30
// standard ZDT factory would result in 01:00 rather than 00:30
LocalDateTime ldt = atTime(LocalTime.MIDNIGHT);
if (zone instanceof ZoneOffset == false) {
ZoneRules rules = zone.getRules();
ZoneOffsetTransition trans = rules.getTransition(ldt);
if (trans != null && trans.isGap()) {
ldt = trans.getDateTimeAfter();
}
}
return ZonedDateTime.of(ldt, zone);
}
use of java.time.zone.ZoneOffsetTransition in project jdk8u_jdk by JetBrains.
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));
}
use of java.time.zone.ZoneOffsetTransition in project jdk8u_jdk by JetBrains.
the class TCKZoneRules method test_Apia_jumpForwardOverInternationalDateLine_P12_to_M12.
public void test_Apia_jumpForwardOverInternationalDateLine_P12_to_M12() {
// transition occurred at 1879-07-04T00:00+12:33:04
ZoneRules test = pacificApia();
Instant instantBefore = LocalDate.of(1879, 7, 2).atStartOfDay(ZoneOffset.UTC).toInstant();
ZoneOffsetTransition trans = test.nextTransition(instantBefore);
assertEquals(trans.getDateTimeBefore(), LocalDateTime.of(1879, 7, 5, 0, 0));
assertEquals(trans.getDateTimeAfter(), LocalDateTime.of(1879, 7, 4, 0, 0));
assertEquals(trans.isGap(), false);
assertEquals(trans.isOverlap(), true);
assertEquals(trans.isValidOffset(ZoneOffset.ofHoursMinutesSeconds(+12, 33, 4)), true);
assertEquals(trans.isValidOffset(ZoneOffset.ofHoursMinutesSeconds(-11, -26, -56)), true);
assertEquals(trans.getDuration(), Duration.ofHours(-24));
assertEquals(trans.getInstant(), LocalDateTime.of(1879, 7, 4, 0, 0).toInstant(ZoneOffset.ofHoursMinutesSeconds(-11, -26, -56)));
ZonedDateTime zdt = ZonedDateTime.of(1879, 7, 4, 23, 0, 0, 0, ZoneId.of("Pacific/Apia"));
assertEquals(zdt.plusHours(2).toLocalDateTime(), LocalDateTime.of(1879, 7, 4, 1, 0, 0));
}
Aggregations