Skip to content

Commit 4030380

Browse files
committed
More part methods
1 parent 2a628f2 commit 4030380

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import org.threeten.bp.LocalTime.SECONDS_PER_DAY
4343
import org.threeten.bp.LocalTime.SECONDS_PER_HOUR
4444
import org.threeten.bp.LocalTime.SECONDS_PER_MINUTE
4545
import org.threeten.bp.LocalTime.HOURS_PER_DAY
46+
import org.threeten.bp.LocalTime.MINUTES_PER_HOUR
4647
import org.threeten.bp.format.DateTimeParseException
4748
import org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND
4849
import org.threeten.bp.temporal.ChronoUnit
@@ -971,6 +972,20 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
971972
*/
972973
def toSeconds: Long = seconds
973974

975+
/**
976+
* Extracts the number of seconds part in the duration.
977+
*
978+
* This returns the remaining seconds when dividing toSeconds() by seconds in a minute. This is
979+
* based on the standard definition of a minute as 60 seconds.
980+
*
981+
* This instance is immutable and unaffected by this method call.
982+
*
983+
* @return
984+
* the number of seconds parts in the duration, may be negative
985+
* @since 9
986+
*/
987+
def toSecondsPart: Int = (toSeconds / SECONDS_PER_MINUTE).toInt
988+
974989
/**
975990
* Returns a copy of this duration with the length negated.
976991
*
@@ -1134,6 +1149,20 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
11341149
*/
11351150
def toMinutes: Long = seconds / SECONDS_PER_MINUTE
11361151

1152+
/**
1153+
* Extracts the number of minutes part in the duration.
1154+
*
1155+
* This returns the number of remaining minutes when dividing toMinutes() by minutes in an hour.
1156+
* This is based on the standard definition of an hour as 60 minutes.
1157+
*
1158+
* This instance is immutable and unaffected by this method call.
1159+
*
1160+
* @return
1161+
* the number of minutes parts in the duration, may be negative
1162+
* @since 9
1163+
*/
1164+
def toMinutesPart: Int = (toMinutes / MINUTES_PER_HOUR).toInt
1165+
11371166
/**
11381167
* Converts this duration to the total length in milliseconds.
11391168
*
@@ -1154,6 +1183,22 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
11541183
Math.addExact(result, nanos.toLong / Duration.NANOS_PER_MILLI)
11551184
}
11561185

1186+
/**
1187+
* Extracts the number of milliseconds part of the duration.
1188+
*
1189+
* This returns the milliseconds part by dividing the number of nanoseconds by 1,000,000. The
1190+
* length of the duration is stored using two fields - seconds and nanoseconds. The nanoseconds
1191+
* part is a value from 0 to 999,999,999 that is an adjustment to the length in seconds. The total
1192+
* duration is defined by calling getNano() and getSeconds().
1193+
*
1194+
* This instance is immutable and unaffected by this method call.
1195+
*
1196+
* @return
1197+
* the number of milliseconds part of the duration.
1198+
* @since 9
1199+
*/
1200+
def toMillisPart: Int = (nanos / Duration.NANOS_PER_MILLI).toInt
1201+
11571202
/**
11581203
* Converts this duration to the total length in nanoseconds expressed as a {@code long}.
11591204
*
@@ -1170,6 +1215,21 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
11701215
Math.addExact(result, nanos.toLong)
11711216
}
11721217

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 / Duration.NANOS_PER_SECOND).toInt
1232+
11731233
/**
11741234
* Compares this duration to the specified {@code Duration}.
11751235
*

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ import org.threeten.bp.temporal.TemporalUnit
3737
import org.threeten.bp.LocalTime.SECONDS_PER_DAY
3838
import org.threeten.bp.LocalTime.HOURS_PER_DAY
3939
import org.threeten.bp.LocalTime.SECONDS_PER_HOUR
40-
// import org.threeten.bp.LocalTime.SECONDS_PER_MINUTE
40+
import org.threeten.bp.LocalTime.MINUTES_PER_HOUR
41+
import org.threeten.bp.LocalTime.SECONDS_PER_MINUTE
4142

4243
/** Test Duration. */
4344
class TestDuration extends AnyFunSuite with AssertionsHelper {
@@ -182,6 +183,7 @@ class TestDuration extends AnyFunSuite with AssertionsHelper {
182183
assertEquals(test.toDaysPart, test.toSeconds / SECONDS_PER_DAY)
183184
assertEquals(test.toHours, test.toSeconds / SECONDS_PER_HOUR)
184185
assertEquals(test.toHoursPart, test.toHours / HOURS_PER_DAY)
186+
assertEquals(test.toMinutesPart, test.toMinutes / MINUTES_PER_HOUR)
185187
assertEquals(test.getNano, 999999999)
186188
}
187189

@@ -191,6 +193,8 @@ class TestDuration extends AnyFunSuite with AssertionsHelper {
191193
assertEquals(test.toSeconds, Long.MaxValue / 1000000000)
192194
assertEquals(test.toDays, test.toSeconds / SECONDS_PER_DAY)
193195
assertEquals(test.toDaysPart, test.toSeconds / SECONDS_PER_DAY)
196+
assertEquals(test.toMinutesPart, test.toMinutes / MINUTES_PER_HOUR)
197+
assertEquals(test.toSecondsPart, test.toSeconds / SECONDS_PER_MINUTE)
194198
assertEquals(test.getNano, Long.MaxValue % 1000000000)
195199
}
196200

@@ -200,6 +204,7 @@ class TestDuration extends AnyFunSuite with AssertionsHelper {
200204
assertEquals(test.toSeconds, Long.MinValue / 1000000000 - 1)
201205
assertEquals(test.toDays, test.toSeconds / SECONDS_PER_DAY)
202206
assertEquals(test.toDaysPart, test.toSeconds / SECONDS_PER_DAY)
207+
assertEquals(test.toMinutesPart, test.toMinutes / MINUTES_PER_HOUR)
203208
assertEquals(test.getNano, Long.MinValue % 1000000000 + 1000000000)
204209
}
205210

@@ -1914,11 +1919,13 @@ class TestDuration extends AnyFunSuite with AssertionsHelper {
19141919
test("test_toNanos") {
19151920
val test: Duration = Duration.ofSeconds(321, 123456789)
19161921
assertEquals(test.toNanos, 321123456789L)
1922+
assertEquals(test.toNanosPart, 0L)
19171923
}
19181924

19191925
test("test_toNanos_max") {
19201926
val test: Duration = Duration.ofSeconds(0, Long.MaxValue)
19211927
assertEquals(test.toNanos, Long.MaxValue)
1928+
assertEquals(test.toNanosPart, 0L)
19221929
}
19231930

19241931
test("test_toNanos_tooBig") {
@@ -1931,11 +1938,13 @@ class TestDuration extends AnyFunSuite with AssertionsHelper {
19311938
test("test_toMillis") {
19321939
val test: Duration = Duration.ofSeconds(321, 123456789)
19331940
assertEquals(test.toMillis, 321000 + 123)
1941+
assertEquals(test.toMillisPart, 123)
19341942
}
19351943

19361944
test("test_toMillis_max") {
19371945
val test: Duration = Duration.ofSeconds(Long.MaxValue / 1000, (Long.MaxValue % 1000) * 1000000)
19381946
assertEquals(test.toMillis, Long.MaxValue)
1947+
assertEquals(test.toMillisPart, 807)
19391948
}
19401949

19411950
test("test_toMillis_tooBig") {

0 commit comments

Comments
 (0)