Merge "Update java.time.Duration partially to 11+28"
diff --git a/api/current.txt b/api/current.txt
index c0ba527..544fb3e 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -10588,6 +10588,7 @@
method public static java.time.Duration between(java.time.temporal.Temporal, java.time.temporal.Temporal);
method public int compareTo(java.time.Duration);
method public java.time.Duration dividedBy(long);
+ method public long dividedBy(java.time.Duration);
method public static java.time.Duration from(java.time.temporal.TemporalAmount);
method public long get(java.time.temporal.TemporalUnit);
method public int getNano();
@@ -10624,10 +10625,18 @@
method public java.time.Duration plusSeconds(long);
method public java.time.temporal.Temporal subtractFrom(java.time.temporal.Temporal);
method public long toDays();
+ method public long toDaysPart();
method public long toHours();
+ method public int toHoursPart();
method public long toMillis();
+ method public int toMillisPart();
method public long toMinutes();
+ method public int toMinutesPart();
method public long toNanos();
+ method public int toNanosPart();
+ method public long toSeconds();
+ method public int toSecondsPart();
+ method public java.time.Duration truncatedTo(java.time.temporal.TemporalUnit);
method public java.time.Duration withNanos(int);
method public java.time.Duration withSeconds(long);
field public static final java.time.Duration ZERO;
diff --git a/ojluni/src/main/java/java/time/Duration.java b/ojluni/src/main/java/java/time/Duration.java
index 8637c40..ef1e802 100644
--- a/ojluni/src/main/java/java/time/Duration.java
+++ b/ojluni/src/main/java/java/time/Duration.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2018, 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
@@ -61,6 +61,8 @@
*/
package java.time;
+import static java.time.LocalTime.MINUTES_PER_HOUR;
+import static java.time.LocalTime.NANOS_PER_MILLI;
import static java.time.LocalTime.NANOS_PER_SECOND;
import static java.time.LocalTime.SECONDS_PER_DAY;
import static java.time.LocalTime.SECONDS_PER_HOUR;
@@ -223,7 +225,7 @@
* This method allows an arbitrary number of nanoseconds to be passed in.
* The factory will alter the values of the second and nanosecond in order
* to ensure that the stored nanosecond is in the range 0 to 999,999,999.
- * For example, the following will result in the exactly the same duration:
+ * For example, the following will result in exactly the same duration:
* <pre>
* Duration.ofSeconds(3, 1);
* Duration.ofSeconds(4, -999_999_999);
@@ -954,7 +956,7 @@
if (multiplicand == 1) {
return this;
}
- return create(toSeconds().multiply(BigDecimal.valueOf(multiplicand)));
+ return create(toBigDecimalSeconds().multiply(BigDecimal.valueOf(multiplicand)));
}
/**
@@ -973,16 +975,34 @@
if (divisor == 1) {
return this;
}
- return create(toSeconds().divide(BigDecimal.valueOf(divisor), RoundingMode.DOWN));
+ return create(toBigDecimalSeconds().divide(BigDecimal.valueOf(divisor), RoundingMode.DOWN));
}
/**
+ * Returns number of whole times a specified Duration occurs within this Duration.
+ * <p>
+ * This instance is immutable and unaffected by this method call.
+ *
+ * @param divisor the value to divide the duration by, positive or negative, not null
+ * @return number of whole times, rounded toward zero, a specified
+ * {@code Duration} occurs within this Duration, may be negative
+ * @throws ArithmeticException if the divisor is zero, or if numeric overflow occurs
+ * @since 9
+ */
+ public long dividedBy(Duration divisor) {
+ Objects.requireNonNull(divisor, "divisor");
+ BigDecimal dividendBigD = toBigDecimalSeconds();
+ BigDecimal divisorBigD = divisor.toBigDecimalSeconds();
+ return dividendBigD.divideToIntegralValue(divisorBigD).longValueExact();
+ }
+
+ /**
* Converts this duration to the total length in seconds and
* fractional nanoseconds expressed as a {@code BigDecimal}.
*
* @return the total length of the duration in seconds, with a scale of 9, not null
*/
- private BigDecimal toSeconds() {
+ private BigDecimal toBigDecimalSeconds() {
return BigDecimal.valueOf(seconds).add(BigDecimal.valueOf(nanos, 9));
}
@@ -1149,6 +1169,20 @@
}
/**
+ * Gets the number of seconds in this duration.
+ * <p>
+ * This returns the total number of whole seconds in the duration.
+ * <p>
+ * This instance is immutable and unaffected by this method call.
+ *
+ * @return the whole seconds part of the length of the duration, positive or negative
+ * @since 9
+ */
+ public long toSeconds() {
+ return seconds;
+ }
+
+ /**
* Converts this duration to the total length in milliseconds.
* <p>
* If this duration is too large to fit in a {@code long} milliseconds, then an
@@ -1182,6 +1216,150 @@
return totalNanos;
}
+ /**
+ * Extracts the number of days in the duration.
+ * <p>
+ * This returns the total number of days in the duration by dividing the
+ * number of seconds by 86400.
+ * This is based on the standard definition of a day as 24 hours.
+ * <p>
+ * This instance is immutable and unaffected by this method call.
+ *
+ * @return the number of days in the duration, may be negative
+ * @since 9
+ */
+ public long toDaysPart(){
+ return seconds / SECONDS_PER_DAY;
+ }
+
+ /**
+ * Extracts the number of hours part in the duration.
+ * <p>
+ * This returns the number of remaining hours when dividing {@link #toHours}
+ * by hours in a day.
+ * This is based on the standard definition of a day as 24 hours.
+ * <p>
+ * This instance is immutable and unaffected by this method call.
+ *
+ * @return the number of hours part in the duration, may be negative
+ * @since 9
+ */
+ public int toHoursPart(){
+ return (int) (toHours() % 24);
+ }
+
+ /**
+ * Extracts the number of minutes part in the duration.
+ * <p>
+ * This returns the number of remaining minutes when dividing {@link #toMinutes}
+ * by minutes in an hour.
+ * This is based on the standard definition of an hour as 60 minutes.
+ * <p>
+ * This instance is immutable and unaffected by this method call.
+ *
+ * @return the number of minutes parts in the duration, may be negative
+ * @since 9
+ */
+ public int toMinutesPart(){
+ return (int) (toMinutes() % MINUTES_PER_HOUR);
+ }
+
+ /**
+ * Extracts the number of seconds part in the duration.
+ * <p>
+ * This returns the remaining seconds when dividing {@link #toSeconds}
+ * by seconds in a minute.
+ * This is based on the standard definition of a minute as 60 seconds.
+ * <p>
+ * This instance is immutable and unaffected by this method call.
+ *
+ * @return the number of seconds parts in the duration, may be negative
+ * @since 9
+ */
+ public int toSecondsPart(){
+ return (int) (seconds % SECONDS_PER_MINUTE);
+ }
+
+ /**
+ * Extracts the number of milliseconds part of the duration.
+ * <p>
+ * This returns the milliseconds part by dividing the number of nanoseconds by 1,000,000.
+ * The length of the duration is stored using two fields - seconds and nanoseconds.
+ * The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to
+ * the length in seconds.
+ * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}.
+ * <p>
+ * This instance is immutable and unaffected by this method call.
+ *
+ * @return the number of milliseconds part of the duration.
+ * @since 9
+ */
+ public int toMillisPart(){
+ return nanos / 1000_000;
+ }
+
+ /**
+ * Get the nanoseconds part within seconds of the duration.
+ * <p>
+ * The length of the duration is stored using two fields - seconds and nanoseconds.
+ * The nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to
+ * the length in seconds.
+ * The total duration is defined by calling {@link #getNano()} and {@link #getSeconds()}.
+ * <p>
+ * This instance is immutable and unaffected by this method call.
+ *
+ * @return the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
+ * @since 9
+ */
+ public int toNanosPart(){
+ return nanos;
+ }
+
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns a copy of this {@code Duration} truncated to the specified unit.
+ * <p>
+ * Truncating the duration returns a copy of the original with conceptual fields
+ * smaller than the specified unit set to zero.
+ * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
+ * round down towards zero to the nearest minute, setting the seconds and
+ * nanoseconds to zero.
+ * <p>
+ * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
+ * that divides into the length of a standard day without remainder.
+ * This includes all
+ * {@linkplain ChronoUnit#isTimeBased() time-based units on {@code ChronoUnit}}
+ * and {@link ChronoUnit#DAYS DAYS}. Other ChronoUnits throw an exception.
+ * <p>
+ * This instance is immutable and unaffected by this method call.
+ *
+ * @param unit the unit to truncate to, not null
+ * @return a {@code Duration} based on this duration with the time truncated, not null
+ * @throws DateTimeException if the unit is invalid for truncation
+ * @throws UnsupportedTemporalTypeException if the unit is not supported
+ * @since 9
+ */
+ public Duration truncatedTo(TemporalUnit unit) {
+ Objects.requireNonNull(unit, "unit");
+ if (unit == ChronoUnit.SECONDS && (seconds >= 0 || nanos == 0)) {
+ return new Duration(seconds, 0);
+ } else if (unit == ChronoUnit.NANOS) {
+ return this;
+ }
+ Duration unitDur = unit.getDuration();
+ if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
+ throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
+ }
+ long dur = unitDur.toNanos();
+ if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
+ throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
+ }
+ long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
+ long result = (nod / dur) * dur;
+ return plusNanos(result - nod);
+ }
+
//-----------------------------------------------------------------------
/**
* Compares this duration to the specified {@code Duration}.
@@ -1189,7 +1367,7 @@
* The comparison is based on the total length of the durations.
* It is "consistent with equals", as defined by {@link Comparable}.
*
- * @param otherDuration the other duration to compare to, not null
+ * @param otherDuration the other duration to compare to, not null
* @return the comparator value, negative if less, positive if greater
*/
@Override
@@ -1207,7 +1385,7 @@
* <p>
* The comparison is based on the total length of the durations.
*
- * @param otherDuration the other duration, null returns false
+ * @param otherDuration the other duration, null returns false
* @return true if the other duration is equal to this one
*/
@Override
@@ -1240,7 +1418,7 @@
* <p>
* The format of the returned string will be {@code PTnHnMnS}, where n is
* the relevant hours, minutes or seconds part of the duration.
- * Any fractional seconds are placed after a decimal point i the seconds section.
+ * Any fractional seconds are placed after a decimal point in the seconds section.
* If a section has a zero value, it is omitted.
* The hours, minutes and seconds will all have the same sign.
* <p>
diff --git a/ojluni/src/test/java/time/tck/java/time/TCKDuration.java b/ojluni/src/test/java/time/tck/java/time/TCKDuration.java
index c12e914..fb11a30 100644
--- a/ojluni/src/test/java/time/tck/java/time/TCKDuration.java
+++ b/ojluni/src/test/java/time/tck/java/time/TCKDuration.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2012, 2013, 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
@@ -65,9 +65,11 @@
import static java.time.temporal.ChronoUnit.MICROS;
import static java.time.temporal.ChronoUnit.MILLIS;
import static java.time.temporal.ChronoUnit.MINUTES;
+import static java.time.temporal.ChronoUnit.MONTHS;
import static java.time.temporal.ChronoUnit.NANOS;
import static java.time.temporal.ChronoUnit.SECONDS;
import static java.time.temporal.ChronoUnit.WEEKS;
+import static java.time.temporal.ChronoUnit.YEARS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
@@ -2266,6 +2268,133 @@
}
//-----------------------------------------------------------------------
+ // truncated(TemporalUnit)
+ //-----------------------------------------------------------------------
+ TemporalUnit NINETY_MINS = new TemporalUnit() {
+ @Override
+ public Duration getDuration() {
+ return Duration.ofMinutes(90);
+ }
+ @Override
+ public boolean isDurationEstimated() {
+ return false;
+ }
+ @Override
+ public boolean isDateBased() {
+ return false;
+ }
+ @Override
+ public boolean isTimeBased() {
+ return true;
+ }
+ @Override
+ public boolean isSupportedBy(Temporal temporal) {
+ return false;
+ }
+ @Override
+ public <R extends Temporal> R addTo(R temporal, long amount) {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public long between(Temporal temporal1, Temporal temporal2) {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public String toString() {
+ return "NinetyMins";
+ }
+ };
+
+ TemporalUnit NINETY_FIVE_MINS = new TemporalUnit() {
+ @Override
+ public Duration getDuration() {
+ return Duration.ofMinutes(95);
+ }
+ @Override
+ public boolean isDurationEstimated() {
+ return false;
+ }
+ @Override
+ public boolean isDateBased() {
+ return false;
+ }
+ @Override
+ public boolean isTimeBased() {
+ return false;
+ }
+ @Override
+ public boolean isSupportedBy(Temporal temporal) {
+ return false;
+ }
+ @Override
+ public <R extends Temporal> R addTo(R temporal, long amount) {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public long between(Temporal temporal1, Temporal temporal2) {
+ throw new UnsupportedOperationException();
+ }
+ @Override
+ public String toString() {
+ return "NinetyFiveMins";
+ }
+ };
+
+ @DataProvider(name="truncatedToValid")
+ Object[][] data_truncatedToValid() {
+ return new Object[][] {
+ {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), NANOS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789)},
+ {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MICROS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_000)},
+ {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MILLIS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 1230_00_000)},
+ {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), SECONDS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 0)},
+ {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), MINUTES, Duration.ofSeconds(86400 + 3600 + 60, 0)},
+ {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), HOURS, Duration.ofSeconds(86400 + 3600, 0)},
+ {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), DAYS, Duration.ofSeconds(86400, 0)},
+
+ {Duration.ofSeconds(86400 + 3600 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 0, 0)},
+ {Duration.ofSeconds(86400 + 7200 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 5400, 0)},
+ {Duration.ofSeconds(86400 + 10800 + 60 + 1, 123_456_789), NINETY_MINS, Duration.ofSeconds(86400 + 10800, 0)},
+
+ {Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_456_789), MINUTES, Duration.ofSeconds(-86400 - 3600 - 60, 0 )},
+ {Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_456_789), MICROS, Duration.ofSeconds(-86400 - 3600 - 60 - 1, 123_457_000)},
+
+ {Duration.ofSeconds(86400 + 3600 + 60 + 1, 0), SECONDS, Duration.ofSeconds(86400 + 3600 + 60 + 1, 0)},
+ {Duration.ofSeconds(-86400 - 3600 - 120, 0), MINUTES, Duration.ofSeconds(-86400 - 3600 - 120, 0)},
+
+ {Duration.ofSeconds(-1, 0), SECONDS, Duration.ofSeconds(-1, 0)},
+ {Duration.ofSeconds(-1, 123_456_789), SECONDS, Duration.ofSeconds(0, 0)},
+ {Duration.ofSeconds(-1, 123_456_789), NANOS, Duration.ofSeconds(0, -876_543_211)},
+ {Duration.ofSeconds(0, 123_456_789), SECONDS, Duration.ofSeconds(0, 0)},
+ {Duration.ofSeconds(0, 123_456_789), NANOS, Duration.ofSeconds(0, 123_456_789)},
+ };
+ }
+
+ @Test(dataProvider="truncatedToValid")
+ public void test_truncatedTo_valid(Duration input, TemporalUnit unit, Duration expected) {
+ assertEquals(input.truncatedTo(unit), expected);
+ }
+
+ @DataProvider(name="truncatedToInvalid")
+ Object[][] data_truncatedToInvalid() {
+ return new Object[][] {
+ {Duration.ofSeconds(1, 123_456_789), NINETY_FIVE_MINS},
+ {Duration.ofSeconds(1, 123_456_789), WEEKS},
+ {Duration.ofSeconds(1, 123_456_789), MONTHS},
+ {Duration.ofSeconds(1, 123_456_789), YEARS},
+ };
+ }
+
+ @Test(dataProvider="truncatedToInvalid", expectedExceptions=DateTimeException.class)
+ public void test_truncatedTo_invalid(Duration input, TemporalUnit unit) {
+ input.truncatedTo(unit);
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void test_truncatedTo_null() {
+ Duration.ofSeconds(1234).truncatedTo(null);
+ }
+
+ //-----------------------------------------------------------------------
// dividedBy()
//-----------------------------------------------------------------------
@DataProvider(name="DividedBy")
@@ -2372,6 +2501,65 @@
}
//-----------------------------------------------------------------------
+ // dividedbyDur()
+ //-----------------------------------------------------------------------
+
+ @DataProvider(name="dividedByDur_provider")
+ Object[][] provider_dividedByDur() {
+ return new Object[][] {
+ {Duration.ofSeconds(0, 0), Duration.ofSeconds(1, 0), 0},
+ {Duration.ofSeconds(1, 0), Duration.ofSeconds(1, 0), 1},
+ {Duration.ofSeconds(6, 0), Duration.ofSeconds(3, 0), 2},
+ {Duration.ofSeconds(3, 0), Duration.ofSeconds(6, 0), 0},
+ {Duration.ofSeconds(7, 0), Duration.ofSeconds(3, 0), 2},
+
+ {Duration.ofSeconds(0, 333_333_333), Duration.ofSeconds(0, 333_333_333), 1},
+ {Duration.ofSeconds(0, 666_666_666), Duration.ofSeconds(0, 333_333_333), 2},
+ {Duration.ofSeconds(0, 333_333_333), Duration.ofSeconds(0, 666_666_666), 0},
+ {Duration.ofSeconds(0, 777_777_777), Duration.ofSeconds(0, 333_333_333), 2},
+
+ {Duration.ofSeconds(-7, 0), Duration.ofSeconds(3, 0), -2},
+ {Duration.ofSeconds(0, 7), Duration.ofSeconds(0, -3), -2},
+ {Duration.ofSeconds(0, -777_777_777), Duration.ofSeconds(0, 333_333_333), -2},
+
+ {Duration.ofSeconds(432000L, -777_777_777L), Duration.ofSeconds(14400L, 333_333_333L), 29},
+ {Duration.ofSeconds(-432000L, 777_777_777L), Duration.ofSeconds(14400L, 333_333_333L), -29},
+ {Duration.ofSeconds(-432000L, -777_777_777L), Duration.ofSeconds(14400L, 333_333_333L), -29},
+ {Duration.ofSeconds(-432000L, -777_777_777L), Duration.ofSeconds(14400L, -333_333_333L), -30},
+ {Duration.ofSeconds(432000L, -777_777_777L), Duration.ofSeconds(-14400L, 333_333_333L), -30},
+ {Duration.ofSeconds(432000L, -777_777_777L), Duration.ofSeconds(-14400L, -333_333_333L), -29},
+ {Duration.ofSeconds(-432000L, -777_777_777L), Duration.ofSeconds(-14400L, -333_333_333L), 29},
+
+ {Duration.ofSeconds(Long.MAX_VALUE, 0), Duration.ofSeconds(1, 0), Long.MAX_VALUE},
+ {Duration.ofSeconds(Long.MAX_VALUE, 0), Duration.ofSeconds(Long.MAX_VALUE, 0), 1},
+ };
+ }
+
+ @Test(dataProvider="dividedByDur_provider")
+ public void test_dividedByDur(Duration dividend, Duration divisor, long expected) {
+ assertEquals(dividend.dividedBy(divisor), expected);
+ }
+
+ @Test(expectedExceptions=ArithmeticException.class)
+ public void test_dividedByDur_zero() {
+ Duration t = Duration.ofSeconds(1, 0);
+ t.dividedBy(Duration.ZERO);
+ }
+
+ @Test(expectedExceptions=NullPointerException.class)
+ public void test_dividedByDur_null() {
+ Duration t = Duration.ofSeconds(1, 0);
+ t.dividedBy(null);
+ }
+
+ @Test(expectedExceptions=ArithmeticException.class)
+ public void test_dividedByDur_overflow() {
+ Duration dur1 = Duration.ofSeconds(Long.MAX_VALUE, 0);
+ Duration dur2 = Duration.ofNanos(1);
+ dur1.dividedBy(dur2);
+ }
+
+ //-----------------------------------------------------------------------
// negated()
//-----------------------------------------------------------------------
@Test
@@ -2454,6 +2642,147 @@
}
//-----------------------------------------------------------------------
+ // toSeconds()
+ //-----------------------------------------------------------------------
+ @DataProvider(name="toSeconds_provider")
+ Object[][] provider_toSeconds() {
+ return new Object[][] {
+ {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 31556926L},
+ {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -31556927L},
+ {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, 123_456_789), -31556926L},
+ {Duration.ofSeconds(0), 0L},
+ {Duration.ofSeconds(0, 123_456_789), 0L},
+ {Duration.ofSeconds(0, -123_456_789), -1L},
+ {Duration.ofSeconds(Long.MAX_VALUE), 9223372036854775807L},
+ {Duration.ofSeconds(Long.MIN_VALUE), -9223372036854775808L},
+ };
+ }
+
+ @Test(dataProvider="toSeconds_provider")
+ public void test_toSeconds(Duration dur, long seconds) {
+ assertEquals(dur.toSeconds(), seconds);
+ }
+
+ //-----------------------------------------------------------------------
+ // toDaysPart()
+ //-----------------------------------------------------------------------
+ @DataProvider(name="toDaysPart_provider")
+ Object[][] provider_toDaysPart() {
+ return new Object[][] {
+ {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 365L},
+ {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -365L},
+ {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 123_456_789), 0L},
+ {Duration.ofDays(365), 365L},
+ {Duration.ofHours(2), 0L},
+ {Duration.ofHours(-2), 0L},
+ };
+ }
+
+ @Test(dataProvider="toDaysPart_provider")
+ public void test_toDaysPart(Duration dur, long days) {
+ assertEquals(dur.toDaysPart(), days);
+ }
+
+ //-----------------------------------------------------------------------
+ // toHoursPart()
+ //-----------------------------------------------------------------------
+ @DataProvider(name="toHoursPart_provider")
+ Object[][] provider_toHoursPart() {
+ return new Object[][] {
+ {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 5},
+ {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -5},
+ {Duration.ofSeconds(48 * 60 + 46, 123_456_789), 0},
+ {Duration.ofHours(2), 2},
+ {Duration.ofHours(-2), -2},
+ };
+ }
+
+ @Test(dataProvider="toHoursPart_provider")
+ public void test_toHoursPart(Duration dur, int hours) {
+ assertEquals(dur.toHoursPart(), hours);
+ }
+
+ //-----------------------------------------------------------------------
+ // toMinutesPart()
+ //-----------------------------------------------------------------------
+ @DataProvider(name="toMinutesPart_provider")
+ Object[][] provider_toMinutesPart() {
+ return new Object[][] {
+ {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 48},
+ {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -48},
+ {Duration.ofSeconds(46, 123_456_789),0},
+ {Duration.ofMinutes(48), 48},
+ {Duration.ofHours(2), 0},
+ {Duration.ofHours(-2),0},
+ };
+ }
+
+ @Test(dataProvider="toMinutesPart_provider")
+ public void test_toMinutesPart(Duration dur, int minutes) {
+ assertEquals(dur.toMinutesPart(), minutes);
+ }
+
+ //-----------------------------------------------------------------------
+ // toSecondsPart()
+ //-----------------------------------------------------------------------
+ @DataProvider(name="toSecondsPart_provider")
+ Object[][] provider_toSecondsPart() {
+ return new Object[][] {
+ {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 46},
+ {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), -47},
+ {Duration.ofSeconds(0, 123_456_789), 0},
+ {Duration.ofSeconds(46), 46},
+ {Duration.ofHours(2), 0},
+ {Duration.ofHours(-2), 0},
+ };
+ }
+
+ @Test(dataProvider="toSecondsPart_provider")
+ public void test_toSecondsPart(Duration dur, int seconds) {
+ assertEquals(dur.toSecondsPart(), seconds);
+ }
+
+ //-----------------------------------------------------------------------
+ // toMillisPart()
+ //-----------------------------------------------------------------------
+ @DataProvider(name="toMillisPart_provider")
+ Object[][] provider_toMillisPart() {
+ return new Object[][] {
+ {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 123},
+ {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), 876},
+ {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 0), 0},
+ {Duration.ofMillis(123), 123},
+ {Duration.ofHours(2), 0},
+ {Duration.ofHours(-2), 0},
+ };
+ }
+
+ @Test(dataProvider="toMillisPart_provider")
+ public void test_toMillisPart(Duration dur, int millis) {
+ assertEquals(dur.toMillisPart(), millis);
+ }
+
+ //-----------------------------------------------------------------------
+ // toNanosPart()
+ //-----------------------------------------------------------------------
+ @DataProvider(name="toNanosPart_provider")
+ Object[][] provider_toNanosPart() {
+ return new Object[][] {
+ {Duration.ofSeconds(365 * 86400 + 5 * 3600 + 48 * 60 + 46, 123_456_789), 123_456_789},
+ {Duration.ofSeconds(-365 * 86400 - 5 * 3600 - 48 * 60 - 46, -123_456_789), 876_543_211},
+ {Duration.ofSeconds(5 * 3600 + 48 * 60 + 46, 0), 0},
+ {Duration.ofNanos(123_456_789), 123_456_789},
+ {Duration.ofHours(2), 0},
+ {Duration.ofHours(-2), 0},
+ };
+ }
+
+ @Test(dataProvider="toNanosPart_provider")
+ public void test_toNanosPart(Duration dur, int nanos) {
+ assertEquals(dur.toNanosPart(), nanos);
+ }
+
+ //-----------------------------------------------------------------------
// compareTo()
//-----------------------------------------------------------------------
@Test