8079063: ZoneOffsetTransitionRule.of should throw IAE for non-zero nanoseconds

Reviewed-by: rriggs, scolebourne
This commit is contained in:
Peter Levart 2015-06-04 10:58:17 +02:00
parent d922187012
commit 1c2084bafb
3 changed files with 18 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -156,6 +156,7 @@ public final class ZoneOffsetTransition
* @param offsetAfter the offset at and after the transition, not null
*/
ZoneOffsetTransition(LocalDateTime transition, ZoneOffset offsetBefore, ZoneOffset offsetAfter) {
assert transition.getNano() == 0;
this.epochSecond = transition.toEpochSecond(offsetBefore);
this.transition = transition;
this.offsetBefore = offsetBefore;
@ -250,7 +251,7 @@ public final class ZoneOffsetTransition
* @return the transition instant, not null
*/
public Instant getInstant() {
return transition.toInstant(offsetBefore);
return Instant.ofEpochSecond(epochSecond);
}
/**
@ -403,13 +404,7 @@ public final class ZoneOffsetTransition
*/
@Override
public int compareTo(ZoneOffsetTransition transition) {
if (epochSecond < transition.epochSecond) {
return -1;
} else if (epochSecond > transition.epochSecond) {
return 1;
} else {
return this.getInstant().compareTo(transition.getInstant());
}
return Long.compare(epochSecond, transition.epochSecond);
}
//-----------------------------------------------------------------------
@ -429,7 +424,6 @@ public final class ZoneOffsetTransition
if (other instanceof ZoneOffsetTransition) {
ZoneOffsetTransition d = (ZoneOffsetTransition) other;
return epochSecond == d.epochSecond &&
transition.equals(d.transition) &&
offsetBefore.equals(d.offsetBefore) && offsetAfter.equals(d.offsetAfter);
}
return false;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -167,6 +167,7 @@ public final class ZoneOffsetTransitionRule implements Serializable {
* @return the rule, not null
* @throws IllegalArgumentException if the day of month indicator is invalid
* @throws IllegalArgumentException if the end of day flag is true when the time is not midnight
* @throws IllegalArgumentException if {@code time.getNano()} returns non-zero value
*/
public static ZoneOffsetTransitionRule of(
Month month,
@ -190,6 +191,9 @@ public final class ZoneOffsetTransitionRule implements Serializable {
if (timeEndOfDay && time.equals(LocalTime.MIDNIGHT) == false) {
throw new IllegalArgumentException("Time must be midnight when end of day flag is true");
}
if (time.getNano() != 0) {
throw new IllegalArgumentException("Time's nano-of-second must be zero");
}
return new ZoneOffsetTransitionRule(month, dayOfMonthIndicator, dayOfWeek, time, timeEndOfDay, timeDefnition, standardOffset, offsetBefore, offsetAfter);
}
@ -220,6 +224,7 @@ public final class ZoneOffsetTransitionRule implements Serializable {
ZoneOffset standardOffset,
ZoneOffset offsetBefore,
ZoneOffset offsetAfter) {
assert time.getNano() == 0;
this.month = month;
this.dom = (byte) dayOfMonthIndicator;
this.dow = dayOfWeek;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -156,6 +156,13 @@ public class TCKZoneOffsetTransitionRule extends AbstractTCKTest {
OFFSET_0200, OFFSET_0200, OFFSET_0300);
}
@Test(expectedExceptions=IllegalArgumentException.class)
public void test_factory_nonZeroTimeNanos() {
ZoneOffsetTransitionRule.of(
Month.MARCH, 20, DayOfWeek.SUNDAY, LocalTime.of(1, 2, 3, 400_000_000),
false, TimeDefinition.WALL, OFFSET_0200, OFFSET_0200, OFFSET_0300);
}
//-----------------------------------------------------------------------
// getters
//-----------------------------------------------------------------------