Skip to content

Commit 0f5f39a

Browse files
committed
Last attempt at toNanosPart
1 parent fe36335 commit 0f5f39a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

core/shared/src/main/scala/org/threeten/bp/Duration.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,21 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
12151215
Math.addExact(result, nanos.toLong)
12161216
}
12171217

1218+
/**
1219+
* Get the nanoseconds part within seconds of the duration.
1220+
*
1221+
* The length of the duration is stored using two fields - seconds and nanoseconds. The
1222+
* nanoseconds part is a value from 0 to 999,999,999 that is an adjustment to the length in
1223+
* seconds. The total duration is defined by calling getNano() and getSeconds().
1224+
*
1225+
* This instance is immutable and unaffected by this method call.
1226+
*
1227+
* @return
1228+
* the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
1229+
* @since 9
1230+
*/
1231+
def toNanosPart: Int = nanos
1232+
12181233
/**
12191234
* Compares this duration to the specified {@code Duration}.
12201235
*

tests/shared/src/test/scala/org/threeten/bp/TestDuration.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1902,14 +1902,15 @@ class TestDuration extends AnyFunSuite with AssertionsHelper {
19021902
assertEquals(Duration.ofDays(387).plusHours(18).plusMinutes(29).plusSeconds(88).plusMillis(234).toHoursPart, 18)
19031903
assertEquals(Duration.ofDays(387).plusHours(18).plusMinutes(29).plusSeconds(38).plusMillis(234).toMinutesPart, 29)
19041904
assertEquals(Duration.ofDays(387).plusHours(18).plusMinutes(29).plusSeconds(88).plusMillis(234).toSecondsPart, 28)
1905-
assertEquals(Duration.ofDays(387).plusHours(18).plusMinutes(29).plusSeconds(88).plusMillis(234).toMillisPart, 234)
1905+
assertEquals(Duration.ofDays(387).plusHours(18).plusMinutes(29).plusSeconds(88).plusMillis(234).plusNanos(4000).toNanosPart, 234004000)
19061906
}
19071907

19081908
test("test_*_part_with_overflow") {
19091909
assertEquals(Duration.ofDays(3).plusHours(38).toDaysPart, 4)
19101910
assertEquals(Duration.ofDays(0).plusHours(18).plusMinutes(79).toHoursPart, 19)
19111911
assertEquals(Duration.ofDays(0).plusHours(18).plusMinutes(19).plusSeconds(90).toSecondsPart, 30)
19121912
assertEquals(Duration.ofDays(0).plusHours(18).plusMinutes(19).plusSeconds(90).plusMillis(10000).toSecondsPart, 40)
1913+
assertEquals(Duration.ofDays(0).plusHours(18).plusMinutes(19).plusSeconds(90).plusMillis(10000).plusNanos(666666).toNanosPart, 666666)
19131914
}
19141915

19151916
test("test_negated_overflow") {
@@ -1938,11 +1939,13 @@ class TestDuration extends AnyFunSuite with AssertionsHelper {
19381939
test("test_toNanos") {
19391940
val test: Duration = Duration.ofSeconds(321, 123456789)
19401941
assertEquals(test.toNanos, 321123456789L)
1942+
assertEquals(test.toNanosPart, 123456789L)
19411943
}
19421944

19431945
test("test_toNanos_max") {
19441946
val test: Duration = Duration.ofSeconds(0, Long.MaxValue)
19451947
assertEquals(test.toNanos, Long.MaxValue)
1948+
assertEquals(test.toNanosPart, 854775807L)
19461949
}
19471950

19481951
test("test_toNanos_tooBig") {

0 commit comments

Comments
 (0)