Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b024d1b

Browse files
authored
Merge pull request #5228 from sjrd/better-system-time-impl
Better implementations of System.{currentTimeMillis,nanoTime}.
2 parents a6b0eb9 + a06bb44 commit b024d1b

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

javalib/src/main/scala/java/lang/System.scala

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,20 @@ object System {
6868

6969
@inline
7070
def currentTimeMillis(): scala.Long =
71-
(new js.Date).getTime().toLong
71+
js.Date.now().toLong
7272

7373
private object NanoTime {
74-
val getHighPrecisionTime: js.Function0[scala.Double] = {
75-
import js.DynamicImplicits.truthValue
76-
77-
if (js.typeOf(global.performance) != "undefined") {
78-
if (global.performance.now) {
79-
() => global.performance.now().asInstanceOf[scala.Double]
80-
} else if (global.performance.webkitNow) {
81-
() => global.performance.webkitNow().asInstanceOf[scala.Double]
82-
} else {
83-
() => new js.Date().getTime()
84-
}
85-
} else {
86-
() => new js.Date().getTime()
87-
}
74+
val highPrecisionTimer: js.Dynamic = {
75+
if (js.typeOf(global.performance) != "undefined" && !Utils.isUndefined(global.performance.now))
76+
global.performance
77+
else
78+
global.Date
8879
}
8980
}
9081

9182
@inline
9283
def nanoTime(): scala.Long =
93-
(NanoTime.getHighPrecisionTime() * 1000000).toLong
84+
(NanoTime.highPrecisionTimer.now().asInstanceOf[scala.Double] * 1000000).toLong
9485

9586
// arraycopy ----------------------------------------------------------------
9687

javalib/src/main/scala/java/util/Date.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ class Date(private var millis: Long) extends Object
2323

2424
import Date._
2525

26-
def this() = {
27-
/* No need to check for overflow. If SJS lives that long (~year 275760),
28-
* it's OK to have a bug ;-)
29-
*/
30-
this(js.Date.now().toLong)
31-
}
26+
def this() = this(System.currentTimeMillis())
3227

3328
@Deprecated
3429
def this(year: Int, month: Int, date: Int, hrs: Int, min: Int, sec: Int) =

test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/lang/SystemTest.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ class SystemTest {
5353
}
5454
}
5555

56+
@Test def currentTimeMillis(): Unit = {
57+
// Test that the "scale" (order of magnitude) of currentTimeMillis() is correct
58+
val result = System.currentTimeMillis()
59+
assertTrue(result.toString(), result >= 1360059308000L) // timestamp of the first commit of Scala.js
60+
assertTrue(result.toString(), result <= 2937896108000L) // 50 years later
61+
}
62+
63+
@Test def nanoTime(): Unit = {
64+
/* nanoTime() can return arbitrary results; even negative values.
65+
* It is supposed to be monotonic, but apparently it sometimes incorrectly
66+
* goes back in time: https://bugs.java.com/bugdatabase/view_bug?bug_id=6458294
67+
*
68+
* So the only thing we can test is that it links.
69+
*/
70+
System.nanoTime()
71+
}
72+
5673
@Test def identityHashCode(): Unit = {
5774
class HasIDHashCode
5875

0 commit comments

Comments
 (0)