--- old/src/java.base/share/classes/java/time/Duration.java 2016-02-01 07:42:57.660791509 +0300 +++ new/src/java.base/share/classes/java/time/Duration.java 2016-02-01 07:42:57.464791509 +0300 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2016, 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 @@ -1214,8 +1214,16 @@ * @throws ArithmeticException if numeric overflow occurs */ public long toMillis() { - long millis = Math.multiplyExact(seconds, 1000); - millis = Math.addExact(millis, nanos / 1000_000); + long tempSeconds = seconds; + long tempNanos = nanos; + if (tempSeconds < 0) { + // change the seconds and nano value to + // handle Long.MIN_VALUE case + tempSeconds = tempSeconds + 1; + tempNanos = tempNanos - 1000_000_000L; + } + long millis = Math.multiplyExact(tempSeconds, 1000); + millis = Math.addExact(millis, tempNanos / 1000_000); return millis; } @@ -1229,8 +1237,16 @@ * @throws ArithmeticException if numeric overflow occurs */ public long toNanos() { - long totalNanos = Math.multiplyExact(seconds, NANOS_PER_SECOND); - totalNanos = Math.addExact(totalNanos, nanos); + long tempSeconds = seconds; + long tempNanos = nanos; + if (tempSeconds < 0) { + // change the seconds and nano value to + // handle Long.MIN_VALUE case + tempSeconds = tempSeconds + 1; + tempNanos = tempNanos - 1000_000_000L; + } + long totalNanos = Math.multiplyExact(tempSeconds, NANOS_PER_SECOND); + totalNanos = Math.addExact(totalNanos, tempNanos); return totalNanos; } --- old/test/java/time/tck/java/time/TCKDuration.java 2016-02-01 07:42:58.255791511 +0300 +++ new/test/java/time/tck/java/time/TCKDuration.java 2016-02-01 07:42:58.034791511 +0300 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2016, 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 @@ -2496,8 +2496,9 @@ //----------------------------------------------------------------------- @Test public void test_toNanos() { - Duration test = Duration.ofSeconds(321, 123456789); - assertEquals(test.toNanos(), 321123456789L); + assertEquals(Duration.ofSeconds(321, 123456789).toNanos(), 321123456789L); + assertEquals(Duration.ofNanos(Long.MAX_VALUE).toNanos(), 9223372036854775807L); + assertEquals(Duration.ofNanos(Long.MIN_VALUE).toNanos(), -9223372036854775808L); } @Test @@ -2512,13 +2513,26 @@ test.toNanos(); } + @Test + public void test_toNanos_min() { + Duration test = Duration.ofSeconds(0, Long.MIN_VALUE); + assertEquals(test.toNanos(), Long.MIN_VALUE); + } + + @Test(expectedExceptions=ArithmeticException.class) + public void test_toNanos_tooSmall() { + Duration test = Duration.ofSeconds(0, Long.MIN_VALUE).minusNanos(1); + test.toNanos(); + } + //----------------------------------------------------------------------- // toMillis() //----------------------------------------------------------------------- @Test public void test_toMillis() { - Duration test = Duration.ofSeconds(321, 123456789); - assertEquals(test.toMillis(), 321000 + 123); + assertEquals(Duration.ofSeconds(321, 123456789).toMillis(), 321000 + 123); + assertEquals(Duration.ofMillis(Long.MAX_VALUE).toMillis(), 9223372036854775807L); + assertEquals(Duration.ofMillis(Long.MIN_VALUE).toMillis(), -9223372036854775808L); } @Test @@ -2533,6 +2547,18 @@ test.toMillis(); } + @Test + public void test_toMillis_min() { + Duration test = Duration.ofSeconds(Long.MIN_VALUE / 1000, (Long.MIN_VALUE % 1000) * 1000000); + assertEquals(test.toMillis(), Long.MIN_VALUE); + } + + @Test(expectedExceptions=ArithmeticException.class) + public void test_toMillis_tooSmall() { + Duration test = Duration.ofSeconds(Long.MIN_VALUE / 1000, ((Long.MIN_VALUE % 1000) - 1) * 1000000); + test.toMillis(); + } + //----------------------------------------------------------------------- // toSeconds() //-----------------------------------------------------------------------