8211990: DateTimeException thrown when calculating duration between certain dates

Reviewed-by: lancea, scolebourne, rriggs
This commit is contained in:
Naoto Sato 2019-08-13 10:10:42 -07:00
parent 320eaaccc4
commit cf9351a41f
4 changed files with 49 additions and 9 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019, 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
@ -1654,8 +1654,14 @@ public final class OffsetDateTime
public long until(Temporal endExclusive, TemporalUnit unit) {
OffsetDateTime end = OffsetDateTime.from(endExclusive);
if (unit instanceof ChronoUnit) {
end = end.withOffsetSameInstant(offset);
return dateTime.until(end.dateTime, unit);
OffsetDateTime start = this;
try {
end = end.withOffsetSameInstant(offset);
} catch (DateTimeException ex) {
// end may be out of valid range. Adjust to end's offset.
start = withOffsetSameInstant(end.offset);
}
return start.dateTime.until(end.dateTime, unit);
}
return unit.between(this, end);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019, 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
@ -2129,11 +2129,17 @@ public final class ZonedDateTime
public long until(Temporal endExclusive, TemporalUnit unit) {
ZonedDateTime end = ZonedDateTime.from(endExclusive);
if (unit instanceof ChronoUnit) {
end = end.withZoneSameInstant(zone);
ZonedDateTime start = this;
try {
end = end.withZoneSameInstant(zone);
} catch (DateTimeException ex) {
// end may be out of valid range. Adjust to end's zone.
start = withZoneSameInstant(end.zone);
}
if (unit.isDateBased()) {
return dateTime.until(end.dateTime, unit);
return start.dateTime.until(end.dateTime, unit);
} else {
return toOffsetDateTime().until(end.toOffsetDateTime(), unit);
return start.toOffsetDateTime().until(end.toOffsetDateTime(), unit);
}
}
return unit.between(this, end);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019, 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
@ -60,7 +60,9 @@
package test.java.time;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertEquals;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@ -73,6 +75,8 @@ import org.testng.annotations.Test;
/**
* Test OffsetDateTime.
*
* @bug 8211990
*/
@Test
public class TestOffsetDateTime extends AbstractTest {
@ -320,4 +324,11 @@ public class TestOffsetDateTime extends AbstractTest {
assertSame(test, base);
}
@Test
public void test_duration() {
OffsetDateTime start = OffsetDateTime.MAX
.withOffsetSameLocal(ZoneOffset.ofHours(-17));
OffsetDateTime end = OffsetDateTime.MAX;
assertEquals(Duration.between(start, end), Duration.ofHours(1));
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019, 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
@ -59,12 +59,19 @@
*/
package test.java.time;
import static org.testng.Assert.assertEquals;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import org.testng.annotations.Test;
/**
* Test ZonedDateTime.
*
* @bug 8211990
*/
@Test
public class TestZonedDateTime extends AbstractTest {
@ -74,4 +81,14 @@ public class TestZonedDateTime extends AbstractTest {
assertImmutable(ZonedDateTime.class);
}
@Test
public void test_duration() {
ZoneId tokyo = ZoneId.of("Asia/Tokyo");
ZoneId sanJose = ZoneId.of("America/Los_Angeles");
ZonedDateTime end = ZonedDateTime.of(LocalDateTime.MAX, sanJose);
ZonedDateTime start = end.withZoneSameLocal(tokyo);
assertEquals(Duration.between(start, end), Duration.ofHours(17));
}
}