Skip to content

Commit 6fe730a

Browse files
ummelsnicolasstucki
authored andcommitted
Fix #2071: Duration.ofSeconds(Long.MinValue).dividedBy(-1)
1 parent ce0ad30 commit 6fe730a

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/main/scala/java/time/Duration.scala

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,26 @@ final class Duration private (seconds: Long, nanos: Int)
130130
new Duration(newSeconds, newNanos)
131131
}
132132

133-
def dividedBy(divisor: Long): Duration = {
134-
val secondsQuot = normalizedSeconds / divisor
135-
val secondsRem = normalizedSeconds % divisor
136-
val nanos = {
137-
try {
138-
val total = Math.addExact(
139-
Math.multiplyExact(secondsRem, NANOS_IN_SECOND),
140-
normalizedNanos)
141-
total / divisor
142-
} catch {
143-
case _: ArithmeticException =>
144-
val total = BigInt(secondsRem) * NANOS_IN_SECOND + normalizedNanos
145-
(total / divisor).toLong
133+
def dividedBy(divisor: Long): Duration = divisor match {
134+
case 1 => this
135+
case -1 => negated
136+
137+
case _ =>
138+
val secondsQuot = normalizedSeconds / divisor
139+
val secondsRem = normalizedSeconds % divisor
140+
val nanos = {
141+
try {
142+
val total = Math.addExact(
143+
Math.multiplyExact(secondsRem, NANOS_IN_SECOND),
144+
normalizedNanos)
145+
total / divisor
146+
} catch {
147+
case _: ArithmeticException =>
148+
val total = BigInt(secondsRem) * NANOS_IN_SECOND + normalizedNanos
149+
(total / divisor).toLong
150+
}
146151
}
147-
}
148-
Duration.ofSeconds(secondsQuot).plusNanos(nanos)
152+
Duration.ofSeconds(secondsQuot).plusNanos(nanos)
149153
}
150154

151155
def negated(): Duration = multipliedBy(-1)

0 commit comments

Comments
 (0)