Skip to content

Commit d1cce10

Browse files
committed
Try to parse Duration as Long before parsing as Double
1 parent 7a6d118 commit d1cce10

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

library/src/scala/concurrent/duration/Duration.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
package scala.concurrent.duration
1414

15-
import java.lang.{ Double => JDouble, Long => JLong }
15+
import java.lang.{ Double => JDouble }
16+
import scala.collection.StringParsers
1617

1718
object Duration {
1819

@@ -44,7 +45,7 @@ object Duration {
4445
def apply(length: Long, unit: String): FiniteDuration = new FiniteDuration(length, Duration.timeUnit(unit))
4546

4647
// Double stores 52 bits mantissa, but there is an implied '1' in front, making the limit 2^53
47-
private[this] final val maxPreciseDouble = 9007199254740992d
48+
// private[this] final val maxPreciseDouble = 9007199254740992d // not used after https://github.com/scala/scala/pull/9233
4849

4950
/**
5051
* Parse String into Duration. Format is `"<length><unit>"`, where
@@ -65,9 +66,8 @@ object Duration {
6566
timeUnit get unitName match {
6667
case Some(unit) =>
6768
val valueStr = s1 dropRight unitName.length
68-
val valueD = JDouble.parseDouble(valueStr)
69-
if (valueD >= -maxPreciseDouble && valueD <= maxPreciseDouble) Duration(valueD, unit)
70-
else Duration(JLong.parseLong(valueStr), unit)
69+
StringParsers.parseLong(valueStr).map(Duration(_, unit))
70+
.getOrElse(Duration(JDouble.parseDouble(valueStr), unit))
7171
case _ => throw new NumberFormatException("format error " + s)
7272
}
7373
}

0 commit comments

Comments
 (0)