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);
}
}
use of java.time.ZoneOffset in project jdk8u_jdk by JetBrains.
the class TestZoneId method test_NewYork_getOffset_fromDST.
public void test_NewYork_getOffset_fromDST() {
ZoneId test = ZoneId.of("America/New_York");
ZoneOffset offset = ZoneOffset.ofHours(-4);
assertEquals(test.getRules().getOffset(createInstant(2008, 11, 1, offset)), ZoneOffset.ofHours(-4));
assertEquals(test.getRules().getOffset(createInstant(2008, 11, 2, offset)), ZoneOffset.ofHours(-4));
assertEquals(test.getRules().getOffset(createInstant(2008, 11, 3, offset)), ZoneOffset.ofHours(-5));
assertEquals(test.getRules().getOffset(createInstant(2008, 11, 4, offset)), ZoneOffset.ofHours(-5));
assertEquals(test.getRules().getOffset(createInstant(2008, 11, 5, offset)), ZoneOffset.ofHours(-5));
assertEquals(test.getRules().getOffset(createInstant(2008, 11, 6, offset)), ZoneOffset.ofHours(-5));
assertEquals(test.getRules().getOffset(createInstant(2008, 11, 7, offset)), ZoneOffset.ofHours(-5));
// cutover at 02:00 local
assertEquals(test.getRules().getOffset(createInstant(2008, 11, 2, 1, 59, 59, 999999999, offset)), ZoneOffset.ofHours(-4));
assertEquals(test.getRules().getOffset(createInstant(2008, 11, 2, 2, 0, 0, 0, offset)), ZoneOffset.ofHours(-5));
}
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);
}
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);
}
use of java.time.ZoneOffset in project jdk8u_jdk by JetBrains.
the class DateTimePrintContext method adjust.
private static TemporalAccessor adjust(final TemporalAccessor temporal, DateTimeFormatter formatter) {
// normal case first (early return is an optimization)
Chronology overrideChrono = formatter.getChronology();
ZoneId overrideZone = formatter.getZone();
if (overrideChrono == null && overrideZone == null) {
return temporal;
}
// ensure minimal change (early return is an optimization)
Chronology temporalChrono = temporal.query(TemporalQueries.chronology());
ZoneId temporalZone = temporal.query(TemporalQueries.zoneId());
if (Objects.equals(overrideChrono, temporalChrono)) {
overrideChrono = null;
}
if (Objects.equals(overrideZone, temporalZone)) {
overrideZone = null;
}
if (overrideChrono == null && overrideZone == null) {
return temporal;
}
// make adjustment
final Chronology effectiveChrono = (overrideChrono != null ? overrideChrono : temporalChrono);
if (overrideZone != null) {
// if have zone and instant, calculation is simple, defaulting chrono if necessary
if (temporal.isSupported(INSTANT_SECONDS)) {
Chronology chrono = (effectiveChrono != null ? effectiveChrono : IsoChronology.INSTANCE);
return chrono.zonedDateTime(Instant.from(temporal), overrideZone);
}
// block changing zone on OffsetTime, and similar problem cases
if (overrideZone.normalized() instanceof ZoneOffset && temporal.isSupported(OFFSET_SECONDS) && temporal.get(OFFSET_SECONDS) != overrideZone.getRules().getOffset(Instant.EPOCH).getTotalSeconds()) {
throw new DateTimeException("Unable to apply override zone '" + overrideZone + "' because the temporal object being formatted has a different offset but" + " does not represent an instant: " + temporal);
}
}
final ZoneId effectiveZone = (overrideZone != null ? overrideZone : temporalZone);
final ChronoLocalDate effectiveDate;
if (overrideChrono != null) {
if (temporal.isSupported(EPOCH_DAY)) {
effectiveDate = effectiveChrono.date(temporal);
} else {
// check for date fields other than epoch-day, ignoring case of converting null to ISO
if (!(overrideChrono == IsoChronology.INSTANCE && temporalChrono == null)) {
for (ChronoField f : ChronoField.values()) {
if (f.isDateBased() && temporal.isSupported(f)) {
throw new DateTimeException("Unable to apply override chronology '" + overrideChrono + "' because the temporal object being formatted contains date fields but" + " does not represent a whole date: " + temporal);
}
}
}
effectiveDate = null;
}
} else {
effectiveDate = null;
}
// this better handles map-like underlying temporal instances
return new TemporalAccessor() {
@Override
public boolean isSupported(TemporalField field) {
if (effectiveDate != null && field.isDateBased()) {
return effectiveDate.isSupported(field);
}
return temporal.isSupported(field);
}
@Override
public ValueRange range(TemporalField field) {
if (effectiveDate != null && field.isDateBased()) {
return effectiveDate.range(field);
}
return temporal.range(field);
}
@Override
public long getLong(TemporalField field) {
if (effectiveDate != null && field.isDateBased()) {
return effectiveDate.getLong(field);
}
return temporal.getLong(field);
}
@SuppressWarnings("unchecked")
@Override
public <R> R query(TemporalQuery<R> query) {
if (query == TemporalQueries.chronology()) {
return (R) effectiveChrono;
}
if (query == TemporalQueries.zoneId()) {
return (R) effectiveZone;
}
if (query == TemporalQueries.precision()) {
return temporal.query(query);
}
return query.queryFrom(this);
}
};
}
Aggregations