Search in sources :

Example 36 with ZoneId

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

the class TCKZoneIdSerialization method test_deserialization_lenient_characters.

@Test
public void test_deserialization_lenient_characters() throws Exception {
    // an ID can be loaded without validation during deserialization
    String id = "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-";
    ZoneId deser = deserialize(id);
    // getId, equals, hashCode, toString and normalized are OK
    assertEquals(deser.getId(), id);
    assertEquals(deser.toString(), id);
    assertEquals(deser, deser);
    assertEquals(deser.hashCode(), deser.hashCode());
    assertEquals(deser.normalized(), deser);
    // getting the rules is not
    try {
        deser.getRules();
        fail();
    } catch (ZoneRulesException ex) {
    // expected
    }
}
Also used : ZoneRulesException(java.time.zone.ZoneRulesException) ZoneId(java.time.ZoneId) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 37 with ZoneId

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

the class TCKZoneIdSerialization method deserialize.

private ZoneId deserialize(String id) throws Exception {
    String serClass = ZoneId.class.getPackage().getName() + ".Ser";
    Class<?> serCls = Class.forName(serClass);
    Field field = serCls.getDeclaredField("serialVersionUID");
    field.setAccessible(true);
    long serVer = (Long) field.get(null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DataOutputStream dos = new DataOutputStream(baos)) {
        dos.writeShort(ObjectStreamConstants.STREAM_MAGIC);
        dos.writeShort(ObjectStreamConstants.STREAM_VERSION);
        dos.writeByte(ObjectStreamConstants.TC_OBJECT);
        dos.writeByte(ObjectStreamConstants.TC_CLASSDESC);
        dos.writeUTF(serClass);
        dos.writeLong(serVer);
        dos.writeByte(ObjectStreamConstants.SC_EXTERNALIZABLE | ObjectStreamConstants.SC_BLOCK_DATA);
        // number of fields
        dos.writeShort(0);
        // end of classdesc
        dos.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
        // no superclasses
        dos.writeByte(ObjectStreamConstants.TC_NULL);
        dos.writeByte(ObjectStreamConstants.TC_BLOCKDATA);
        // length of data (1 byte + 2 bytes UTF length + 32 bytes UTF)
        dos.writeByte(1 + 2 + id.length());
        // ZoneId
        dos.writeByte(7);
        dos.writeUTF(id);
        // end of blockdata
        dos.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
    }
    ZoneId deser = null;
    try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
        deser = (ZoneId) ois.readObject();
    }
    return deser;
}
Also used : Field(java.lang.reflect.Field) ZoneId(java.time.ZoneId)

Example 38 with ZoneId

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

the class TCKZoneIdPrinterParser method test_parseSuccess_caseInsensitive.

@Test(dataProvider = "parseSuccess")
public void test_parseSuccess_caseInsensitive(String text, int expectedIndex, int expectedErrorIndex, ZoneId expected) {
    builder.parseCaseInsensitive().appendZoneId();
    String lcText = text.toLowerCase(Locale.ENGLISH);
    TemporalAccessor parsed = builder.toFormatter().parseUnresolved(lcText, pos);
    assertEquals(pos.getErrorIndex(), expectedErrorIndex, "Incorrect error index parsing: " + lcText);
    assertEquals(pos.getIndex(), expectedIndex, "Incorrect index parsing: " + lcText);
    if (expected != null) {
        ZoneId zid = parsed.query(TemporalQueries.zoneId());
        assertEquals(parsed.query(TemporalQueries.zoneId()), expected, "Incorrect zoneId parsing: " + lcText);
        assertEquals(parsed.query(TemporalQueries.offset()), null, "Incorrect offset parsing: " + lcText);
        assertEquals(parsed.query(TemporalQueries.zone()), expected, "Incorrect zone parsing: " + lcText);
    } else {
        assertEquals(parsed, null);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ZoneId(java.time.ZoneId) Test(org.testng.annotations.Test)

Example 39 with ZoneId

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

the class TestZoneId method test_NewYork.

//-----------------------------------------------------------------------
// America/New_York
//-----------------------------------------------------------------------
public void test_NewYork() {
    ZoneId test = ZoneId.of("America/New_York");
    assertEquals(test.getId(), "America/New_York");
    assertEquals(test.getRules().isFixedOffset(), false);
}
Also used : ZoneId(java.time.ZoneId)

Example 40 with ZoneId

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

the class TestZoneId method test_London_getOffset_toDST.

public void test_London_getOffset_toDST() {
    ZoneId test = ZoneId.of("Europe/London");
    assertEquals(test.getRules().getOffset(createInstant(2008, 3, 24, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
    assertEquals(test.getRules().getOffset(createInstant(2008, 3, 25, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
    assertEquals(test.getRules().getOffset(createInstant(2008, 3, 26, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
    assertEquals(test.getRules().getOffset(createInstant(2008, 3, 27, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
    assertEquals(test.getRules().getOffset(createInstant(2008, 3, 28, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
    assertEquals(test.getRules().getOffset(createInstant(2008, 3, 29, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
    assertEquals(test.getRules().getOffset(createInstant(2008, 3, 30, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
    assertEquals(test.getRules().getOffset(createInstant(2008, 3, 31, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
    // cutover at 01:00Z
    assertEquals(test.getRules().getOffset(createInstant(2008, 3, 30, 0, 59, 59, 999999999, ZoneOffset.UTC)), ZoneOffset.ofHours(0));
    assertEquals(test.getRules().getOffset(createInstant(2008, 3, 30, 1, 0, 0, 0, ZoneOffset.UTC)), ZoneOffset.ofHours(1));
}
Also used : ZoneId(java.time.ZoneId)

Aggregations

ZoneId (java.time.ZoneId)88 Test (org.testng.annotations.Test)38 LocalDateTime (java.time.LocalDateTime)13 ZonedDateTime (java.time.ZonedDateTime)13 Test (org.junit.Test)8 Clock (java.time.Clock)7 ZoneOffset (java.time.ZoneOffset)7 ZoneOffsetTransition (java.time.zone.ZoneOffsetTransition)6 Chronology (java.time.chrono.Chronology)5 IsoChronology (java.time.chrono.IsoChronology)5 AbstractTCKTest (tck.java.time.AbstractTCKTest)5 Instant (java.time.Instant)4 HijrahChronology (java.time.chrono.HijrahChronology)4 JapaneseChronology (java.time.chrono.JapaneseChronology)4 MinguoChronology (java.time.chrono.MinguoChronology)4 ThaiBuddhistChronology (java.time.chrono.ThaiBuddhistChronology)4 LocalTime (java.time.LocalTime)3 DateTimeFormatter (java.time.format.DateTimeFormatter)3 HashMap (java.util.HashMap)3 Field (java.lang.reflect.Field)2