@Override protected void execute(CommandEvent event) { String args = event.getArgs(); if(args.isEmpty()) { event.replyWarning("Please specify a timezone!"); return; } try { ZoneId.of(args); } catch(ZoneRulesException e) { event.replyError("Please specify a valid timezone!"); return; } db.setTimezone(event.getAuthor(), args); event.replySuccess("Successfully updated timezone!"); }
@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 } }
@Test public void shouldAllTimeZonesBeValid() { boolean allValid = true; for (TimeZone tz : TimeZone.values()) { try { tz.getZoneId().getId().equals(tz.getId()); } catch (ZoneRulesException e) { e.printStackTrace(); allValid = false; } } assertThat(allValid, org.hamcrest.Matchers.is(true)); }
/** * 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 DateTimeException if checking availability and the ID cannot be found */ static ZoneRegion ofId(String zoneId, boolean checkAvailable) { Jdk8Methods.requireNonNull(zoneId, "zoneId"); if (zoneId.length() < 2 || PATTERN.matcher(zoneId).matches() == false) { throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId); } ZoneRules rules = null; try { // always attempt load for better behavior after deserialization rules = ZoneRulesProvider.getRules(zoneId, true); } catch (ZoneRulesException ex) { // special case as removed from data file if (zoneId.equals("GMT0")) { rules = ZoneOffset.UTC.getRules(); } else if (checkAvailable) { throw ex; } } return new ZoneRegion(zoneId, rules); }
/** * 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 DateTimeException if checking availability and the ID cannot be found */ static ZoneRegion ofId(String zoneId, boolean checkAvailable) { Jdk7Methods.Objects_requireNonNull(zoneId, "zoneId"); if (zoneId.length() < 2 || zoneId.startsWith("UTC") || zoneId.startsWith("GMT") || (PATTERN.matcher(zoneId).matches() == false)) { throw new DateTimeException("ZoneId format is not a valid region format"); } ZoneRules rules = null; try { // always attempt load for better behavior after deserialization rules = ZoneRulesProvider.getRules(zoneId); } catch (ZoneRulesException ex) { if (checkAvailable) { throw ex; } } return new ZoneRegion(zoneId, rules); }
/** * 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); }
@Override protected ZoneRules provideRules(String zoneId, boolean forCaching) { if (zoneId.equals("FooLocation")) { return rules; } throw new ZoneRulesException("Invalid"); }
@Override protected ZoneRules provideRules(String zoneId, boolean forCaching) { count++; if (zoneId.equals("DynamicLocation")) { return (forCaching ? null : (count > 2 ? ALTERNATE : BASE)); } throw new ZoneRulesException("Invalid"); }
@Test(expectedExceptions = ZoneRulesException.class) public void test_systemDefault_unableToConvert_unknownId() { TimeZone current = TimeZone.getDefault(); try { TimeZone.setDefault(new SimpleTimeZone(127, "SomethingWeird")); ZoneId.systemDefault(); } finally { TimeZone.setDefault(current); } }
/** * Creates an instance. * * @throws ZoneRulesException if unable to load */ public TzdbZoneRulesProvider(List<Path> files) { try { load(files); } catch (Exception ex) { throw new ZoneRulesException("Unable to load TZDB time-zone rules", ex); } }
public ZoneRules getZoneRules(String zoneId) { Object obj = zones.get(zoneId); if (obj == null) { String zoneId0 = zoneId; if (links.containsKey(zoneId)) { zoneId = links.get(zoneId); obj = zones.get(zoneId); } if (obj == null) { // Timezone link can be located in 'backward' file and it // can refer to another link, so we need to check for // link one more time, before throwing an exception String zoneIdBack = zoneId; if (links.containsKey(zoneId)) { zoneId = links.get(zoneId); obj = zones.get(zoneId); } if (obj == null) { throw new ZoneRulesException("Unknown time-zone ID: " + zoneIdBack); } } } if (obj instanceof ZoneRules) { return (ZoneRules)obj; } try { ZoneRules zrules = buildRules(zoneId, (List<ZoneLine>)obj); zones.put(zoneId, zrules); return zrules; } catch (Exception ex) { throw new ZoneRulesException( "Invalid binary time-zone data: TZDB:" + zoneId, ex); } }
@Override protected TimeZone deserialize(String jsonValue, Unmarshaller unmarshaller, Type rtType) { try { final ZoneId zoneId = ZoneId.of(jsonValue); final ZonedDateTime zonedDateTime = LocalDateTime.now().atZone(zoneId); return new SimpleTimeZone(zonedDateTime.getOffset().getTotalSeconds() * 1000, zoneId.getId()); } catch (ZoneRulesException e) { throw new JsonbException(Messages.getMessage(MessageKeys.ZONE_PARSE_ERROR, jsonValue), e); } }
/** * Returns the specified time zone from the repository configuration or the default time zone if none specified. * * @return the time zone */ public ZoneId getTimeZone() { if (timeZone == null) { return ZoneId.systemDefault(); } try { return ZoneId.of(timeZone); } catch (final ZoneRulesException e) { LOGGER.warn("The time zone was not found. Uses GMT as fallback."); return ZoneId.of("GMT"); } }