Skip to content

Commit

Permalink
Fix rounding errors in creation of Durations.
Browse files Browse the repository at this point in the history
Use round instead of manually trying to convert to a Long.

Fixes scala/bug#9949 and scala/bug#10320
  • Loading branch information
Jasper-M committed May 31, 2017
1 parent b68cc6c commit 2a2f588
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/library/scala/concurrent/duration/Duration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ object Duration {
else if (nanos > Long.MaxValue || nanos < Long.MinValue)
throw new IllegalArgumentException("trying to construct too large duration with " + nanos + "ns")
else
fromNanos((nanos + 0.5).toLong)
fromNanos(nanos.round)
}

private[this] final val µs_per_ns = 1000L
Expand Down
8 changes: 8 additions & 0 deletions test/files/jvm/duration-tck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,12 @@ object Test extends App {
((2 seconds fromNow).timeLeft: FiniteDuration) < 4.seconds mustBe true
val finite3: FiniteDuration = 3.5 seconds span

// scala/bug#9949
Duration(-1.0, DAYS) mustBe Duration(-1, DAYS)
Duration("-10 s").toNanos mustBe -10000000000L
Duration(1.0, DAYS) mustBe Duration(1, DAYS)
Duration("10 s").toNanos mustBe 10000000000L

// scala/bug#10320
Duration("6803536004516701ns").toNanos mustBe 6803536004516701L
}

0 comments on commit 2a2f588

Please sign in to comment.