Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 4b8dda2

Browse files
ummelsnicolasstucki
authored andcommitted
Fix #2073: Duration.ZERO.addTo(t) throws for some Temporals
1 parent 6fe730a commit 4b8dda2

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,21 @@ final class Duration private (seconds: Long, nanos: Int)
156156

157157
def abs(): Duration = if (isNegative()) negated() else this
158158

159-
def addTo(temporal: Temporal): Temporal =
160-
temporal.plus(normalizedSeconds, SECONDS).plus(normalizedNanos, NANOS)
159+
def addTo(temporal: Temporal): Temporal = {
160+
val t1 =
161+
if (seconds == 0) temporal
162+
else temporal.plus(seconds, SECONDS)
163+
if (nanos == 0) t1
164+
else t1.plus(nanos, NANOS)
165+
}
161166

162-
def subtractFrom(temporal: Temporal): Temporal =
163-
temporal.minus(normalizedSeconds, SECONDS).minus(normalizedNanos, NANOS)
167+
def subtractFrom(temporal: Temporal): Temporal = {
168+
val t1 =
169+
if (seconds == 0) temporal
170+
else temporal.minus(seconds, SECONDS)
171+
if (nanos == 0) t1
172+
else t1.minus(nanos, NANOS)
173+
}
164174

165175
def toDays(): Long = seconds / SECONDS_IN_DAY
166176

0 commit comments

Comments
 (0)