Search in sources :

Example 6 with ZoneRulesException

use of java.time.zone.ZoneRulesException in project j2objc by google.

the class ZoneRegion method ofId.

/**
 * Obtains an instance of {@code ZoneId} from an identifier.
 *
 * @param zoneId  the time-zone ID, not null
 * @param checkAvailable  whether to check if the zone ID is available
 * @return the zone ID, not null
 * @throws DateTimeException if the ID format is invalid
 * @throws ZoneRulesException if checking availability and the ID cannot be found
 */
static ZoneRegion ofId(String zoneId, boolean checkAvailable) {
    Objects.requireNonNull(zoneId, "zoneId");
    checkName(zoneId);
    ZoneRules rules = null;
    try {
        // always attempt load for better behavior after deserialization
        rules = ZoneRulesProvider.getRules(zoneId, true);
    } catch (ZoneRulesException ex) {
        if (checkAvailable) {
            throw ex;
        }
    }
    return new ZoneRegion(zoneId, rules);
}
Also used : ZoneRulesException(java.time.zone.ZoneRulesException) ZoneRules(java.time.zone.ZoneRules)

Example 7 with ZoneRulesException

use of java.time.zone.ZoneRulesException in project j2objc by google.

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.junit.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 8 with ZoneRulesException

use of java.time.zone.ZoneRulesException in project ballerina by ballerina-lang.

the class Utils method createTimeZone.

public static BStruct createTimeZone(StructInfo timezoneStructInfo, String zoneIdValue) {
    String zoneIdName;
    try {
        ZoneId zoneId = ZoneId.of(zoneIdValue);
        zoneIdName = zoneId.toString();
        // Get offset in seconds
        TimeZone tz = TimeZone.getTimeZone(zoneId);
        int offsetInMills = tz.getOffset(new Date().getTime());
        int offset = offsetInMills / 1000;
        return BLangVMStructs.createBStruct(timezoneStructInfo, zoneIdName, offset);
    } catch (ZoneRulesException e) {
        throw new BallerinaException("invalid timezone id: " + zoneIdValue);
    }
}
Also used : TimeZone(java.util.TimeZone) ZoneRulesException(java.time.zone.ZoneRulesException) ZoneId(java.time.ZoneId) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) Date(java.util.Date)

Example 9 with ZoneRulesException

use of java.time.zone.ZoneRulesException in project teammates by TEAMMATES.

the class TzdbResourceZoneRulesProvider method provideRules.

@Override
protected ZoneRules provideRules(String zoneId, boolean forCaching) {
    // forCaching flag is ignored because this is not a dynamic provider
    Object obj = regionToRules.get(zoneId);
    if (obj == null) {
        throw new ZoneRulesException("Unknown time-zone ID: " + zoneId);
    }
    try {
        if (obj instanceof byte[]) {
            byte[] bytes = (byte[]) obj;
            DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
            obj = serRead(dis);
            regionToRules.put(zoneId, obj);
        }
        return (ZoneRules) obj;
    } catch (Exception ex) {
        throw new ZoneRulesException("Invalid binary time-zone data: TZDB:" + zoneId + ", version: " + versionId, ex);
    }
}
Also used : ZoneRulesException(java.time.zone.ZoneRulesException) ByteArrayInputStream(java.io.ByteArrayInputStream) ZoneRules(java.time.zone.ZoneRules) DataInputStream(java.io.DataInputStream) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ZoneRulesException(java.time.zone.ZoneRulesException)

Example 10 with ZoneRulesException

use of java.time.zone.ZoneRulesException in project neo4j by neo4j.

the class DateTimeValue method parse.

private static DateTimeValue parse(Matcher matcher, Supplier<ZoneId> defaultZone) {
    LocalDateTime local = LocalDateTime.of(parseDate(matcher), optTime(matcher));
    String zoneName = matcher.group("zoneName");
    ZoneOffset offset = parseOffset(matcher);
    ZoneId zone;
    if (zoneName != null) {
        zone = parseZoneName(zoneName);
        if (offset != null) {
            try {
                if (!zone.getRules().isValidOffset(local, offset)) {
                    throw new InvalidArgumentException("Timezone and offset do not match: " + matcher.group());
                }
            } catch (ZoneRulesException e) {
                throw new TemporalParseException(e.getMessage(), e);
            }
        }
    } else if (offset != null) {
        zone = offset;
    } else {
        zone = defaultZone.get();
    }
    return new DateTimeValue(ZonedDateTime.ofLocal(local, zone, offset));
}
Also used : LocalDateTime(java.time.LocalDateTime) InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) ZoneRulesException(java.time.zone.ZoneRulesException) ZoneId(java.time.ZoneId) TemporalParseException(org.neo4j.exceptions.TemporalParseException) ZoneOffset(java.time.ZoneOffset)

Aggregations

ZoneRulesException (java.time.zone.ZoneRulesException)10 ZoneId (java.time.ZoneId)4 ZoneRules (java.time.zone.ZoneRules)4 Test (org.junit.Test)3 AbstractTCKTest (tck.java.time.AbstractTCKTest)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 StreamCorruptedException (java.io.StreamCorruptedException)1 LocalDateTime (java.time.LocalDateTime)1 ZoneOffset (java.time.ZoneOffset)1 Date (java.util.Date)1 TimeZone (java.util.TimeZone)1 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)1 InvalidArgumentException (org.neo4j.exceptions.InvalidArgumentException)1 TemporalParseException (org.neo4j.exceptions.TemporalParseException)1 Test (org.testng.annotations.Test)1