< prev index next >

src/java.base/share/classes/java/time/zone/ZoneRules.java

Print this page
rev 58280 : 8239836: ZoneRules.of() doesn't check transitionList/standardOffsetTL arguments validity
Reviewed-by: rriggs, joehw, scolebourne

*** 468,478 **** * Checks of the zone rules are fixed, such that the offset never varies. * * @return true if the time-zone is fixed and the offset never changes */ public boolean isFixedOffset() { ! return savingsInstantTransitions.length == 0; } /** * Gets the offset applicable at the specified instant in these rules. * <p> --- 468,481 ---- * Checks of the zone rules are fixed, such that the offset never varies. * * @return true if the time-zone is fixed and the offset never changes */ public boolean isFixedOffset() { ! return standardOffsets[0].equals(wallOffsets[0]) && ! standardTransitions.length == 0 && ! savingsInstantTransitions.length == 0 && ! lastRules.length == 0; } /** * Gets the offset applicable at the specified instant in these rules. * <p>
*** 484,494 **** * may be ignored if the rules have a single offset for all instants * @return the offset, not null */ public ZoneOffset getOffset(Instant instant) { if (savingsInstantTransitions.length == 0) { ! return standardOffsets[0]; } long epochSec = instant.getEpochSecond(); // check if using last rules if (lastRules.length > 0 && epochSec > savingsInstantTransitions[savingsInstantTransitions.length - 1]) { --- 487,497 ---- * may be ignored if the rules have a single offset for all instants * @return the offset, not null */ public ZoneOffset getOffset(Instant instant) { if (savingsInstantTransitions.length == 0) { ! return wallOffsets[0]; } long epochSec = instant.getEpochSecond(); // check if using last rules if (lastRules.length > 0 && epochSec > savingsInstantTransitions[savingsInstantTransitions.length - 1]) {
*** 570,580 **** * and the later offset at index 1. * <p> * There are various ways to handle the conversion from a {@code LocalDateTime}. * One technique, using this method, would be: * <pre> ! * List&lt;ZoneOffset&gt; validOffsets = rules.getOffset(localDT); * if (validOffsets.size() == 1) { * // Normal case: only one valid offset * zoneOffset = validOffsets.get(0); * } else { * // Gap or Overlap: determine what to do from transition (which will be non-null) --- 573,583 ---- * and the later offset at index 1. * <p> * There are various ways to handle the conversion from a {@code LocalDateTime}. * One technique, using this method, would be: * <pre> ! * List&lt;ZoneOffset&gt; validOffsets = rules.getValidOffsets(localDT); * if (validOffsets.size() == 1) { * // Normal case: only one valid offset * zoneOffset = validOffsets.get(0); * } else { * // Gap or Overlap: determine what to do from transition (which will be non-null)
*** 638,649 **** Object info = getOffsetInfo(localDateTime); return (info instanceof ZoneOffsetTransition ? (ZoneOffsetTransition) info : null); } private Object getOffsetInfo(LocalDateTime dt) { ! if (savingsInstantTransitions.length == 0) { ! return standardOffsets[0]; } // check if using last rules if (lastRules.length > 0 && dt.isAfter(savingsLocalTransitions[savingsLocalTransitions.length - 1])) { ZoneOffsetTransition[] transArray = findTransitionArray(dt.getYear()); --- 641,652 ---- Object info = getOffsetInfo(localDateTime); return (info instanceof ZoneOffsetTransition ? (ZoneOffsetTransition) info : null); } private Object getOffsetInfo(LocalDateTime dt) { ! if (savingsLocalTransitions.length == 0) { ! return wallOffsets[0]; } // check if using last rules if (lastRules.length > 0 && dt.isAfter(savingsLocalTransitions[savingsLocalTransitions.length - 1])) { ZoneOffsetTransition[] transArray = findTransitionArray(dt.getYear());
*** 754,764 **** * @param instant the instant to find the offset information for, not null, but null * may be ignored if the rules have a single offset for all instants * @return the standard offset, not null */ public ZoneOffset getStandardOffset(Instant instant) { ! if (savingsInstantTransitions.length == 0) { return standardOffsets[0]; } long epochSec = instant.getEpochSecond(); int index = Arrays.binarySearch(standardTransitions, epochSec); if (index < 0) { --- 757,767 ---- * @param instant the instant to find the offset information for, not null, but null * may be ignored if the rules have a single offset for all instants * @return the standard offset, not null */ public ZoneOffset getStandardOffset(Instant instant) { ! if (standardTransitions.length == 0) { return standardOffsets[0]; } long epochSec = instant.getEpochSecond(); int index = Arrays.binarySearch(standardTransitions, epochSec); if (index < 0) {
*** 784,794 **** * @param instant the instant to find the daylight savings for, not null, but null * may be ignored if the rules have a single offset for all instants * @return the difference between the standard and actual offset, not null */ public Duration getDaylightSavings(Instant instant) { ! if (savingsInstantTransitions.length == 0) { return Duration.ZERO; } ZoneOffset standardOffset = getStandardOffset(instant); ZoneOffset actualOffset = getOffset(instant); return Duration.ofSeconds(actualOffset.getTotalSeconds() - standardOffset.getTotalSeconds()); --- 787,797 ---- * @param instant the instant to find the daylight savings for, not null, but null * may be ignored if the rules have a single offset for all instants * @return the difference between the standard and actual offset, not null */ public Duration getDaylightSavings(Instant instant) { ! if (isFixedOffset()) { return Duration.ZERO; } ZoneOffset standardOffset = getStandardOffset(instant); ZoneOffset actualOffset = getOffset(instant); return Duration.ofSeconds(actualOffset.getTotalSeconds() - standardOffset.getTotalSeconds());
< prev index next >