Skip to content

Commit bee7f13

Browse files
committed
Properly implement some of the part methods and add proper tests
1 parent 4030380 commit bee7f13

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
984984
* the number of seconds parts in the duration, may be negative
985985
* @since 9
986986
*/
987-
def toSecondsPart: Int = (toSeconds / SECONDS_PER_MINUTE).toInt
987+
def toSecondsPart: Int = (toSeconds % SECONDS_PER_MINUTE).toInt
988988

989989
/**
990990
* Returns a copy of this duration with the length negated.
@@ -1134,7 +1134,7 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
11341134
* the number of hours part in the duration, may be negative
11351135
* @since 9
11361136
*/
1137-
def toHoursPart: Int = (toHours / HOURS_PER_DAY).toInt
1137+
def toHoursPart: Int = (toHours % HOURS_PER_DAY).toInt
11381138

11391139
/**
11401140
* Gets the number of minutes in this duration.
@@ -1161,7 +1161,7 @@ final class Duration private (private val seconds: Long, private val nanos: Int)
11611161
* the number of minutes parts in the duration, may be negative
11621162
* @since 9
11631163
*/
1164-
def toMinutesPart: Int = (toMinutes / MINUTES_PER_HOUR).toInt
1164+
def toMinutesPart: Int = (toMinutes % MINUTES_PER_HOUR).toInt
11651165

11661166
/**
11671167
* Converts this duration to the total length in milliseconds.

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ class TestDuration extends AnyFunSuite with AssertionsHelper {
193193
assertEquals(test.toSeconds, Long.MaxValue / 1000000000)
194194
assertEquals(test.toDays, test.toSeconds / SECONDS_PER_DAY)
195195
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)
196+
assertEquals(test.toMinutesPart, test.toMinutes % MINUTES_PER_HOUR)
197+
assertEquals(test.toSecondsPart, test.toSeconds % SECONDS_PER_MINUTE)
198198
assertEquals(test.getNano, Long.MaxValue % 1000000000)
199199
}
200200

@@ -204,7 +204,7 @@ class TestDuration extends AnyFunSuite with AssertionsHelper {
204204
assertEquals(test.toSeconds, Long.MinValue / 1000000000 - 1)
205205
assertEquals(test.toDays, test.toSeconds / SECONDS_PER_DAY)
206206
assertEquals(test.toDaysPart, test.toSeconds / SECONDS_PER_DAY)
207-
assertEquals(test.toMinutesPart, test.toMinutes / MINUTES_PER_HOUR)
207+
assertEquals(test.toMinutesPart, test.toMinutes % MINUTES_PER_HOUR)
208208
assertEquals(test.getNano, Long.MinValue % 1000000000 + 1000000000)
209209
}
210210

@@ -1893,6 +1893,25 @@ class TestDuration extends AnyFunSuite with AssertionsHelper {
18931893
assertEquals(Duration.ofSeconds(Long.MaxValue).negated, Duration.ofSeconds(-Long.MaxValue))
18941894
}
18951895

1896+
test("test_*_part") {
1897+
assertEquals(Duration.ofSeconds(0).toSecondsPart, 0)
1898+
assertEquals(Duration.ofSeconds(59).toSecondsPart, 59)
1899+
assertEquals(Duration.ofSeconds(61).toSecondsPart, 1)
1900+
assertEquals(Duration.ofSeconds(187).toSecondsPart, 7)
1901+
assertEquals(Duration.ofDays(387).plusHours(18).plusMinutes(29).plusSeconds(88).plusMillis(234).toDaysPart, 387)
1902+
assertEquals(Duration.ofDays(387).plusHours(18).plusMinutes(29).plusSeconds(88).plusMillis(234).toHoursPart, 18)
1903+
assertEquals(Duration.ofDays(387).plusHours(18).plusMinutes(29).plusSeconds(38).plusMillis(234).toMinutesPart, 29)
1904+
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)
1906+
}
1907+
1908+
test("test_*_part_with_overflow") {
1909+
assertEquals(Duration.ofDays(3).plusHours(38).toDaysPart, 4)
1910+
assertEquals(Duration.ofDays(0).plusHours(18).plusMinutes(79).toHoursPart, 19)
1911+
assertEquals(Duration.ofDays(0).plusHours(18).plusMinutes(19).plusSeconds(90).toSecondsPart, 30)
1912+
assertEquals(Duration.ofDays(0).plusHours(18).plusMinutes(19).plusSeconds(90).plusMillis(10000).toSecondsPart, 40)
1913+
}
1914+
18961915
test("test_negated_overflow") {
18971916
assertThrows[ArithmeticException] {
18981917
Duration.ofSeconds(Long.MinValue).negated

0 commit comments

Comments
 (0)