object Time extends TimeLikeOps[Time] with Serializable
By using Time.now in your program instead of
System.currentTimeMillis
unit tests are able to adjust
the current time to verify timeouts and other time-dependent
behavior, without calling sleep
, and providing deterministic
tests.
The current time can be manipulated via Time.withTimeAt, Time.withCurrentTimeFrozen, Time.withTimeFunction and Time.sleep.
While now
is not a "global" it is however properly propagated through
to other code via the standard usage of Locals throughout util
.
Specifically, code using Futures, FuturePools,
and MockTimers will have their code see the manipulated
values.
val time = Time.fromMilliseconds(123456L) Time.withTimeAt(time) { timeControl => assert(Time.now == time) // you can control time via the `TimeControl` instance. timeControl.advance(2.seconds) FuturePool.unboundedPool { assert(Time.now == time + 2.seconds) } }
- See also
Duration for intervals between two points in time.
Stopwatch for measuring elapsed time.
TimeFormatter for converting to and from
String
representations.
- Alphabetic
- By Inheritance
- Time
- Serializable
- TimeLikeOps
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- val Bottom: Time
Time
Bottom
is smaller than any other time, and is equal only to itself.Time
Bottom
is smaller than any other time, and is equal only to itself. It may be used as a sentinel value, representing a time infinitely far in the past.- Definition Classes
- Time → TimeLikeOps
- val Top: Time
Time
Top
is greater than any other definable time, and is equal only to itself.Time
Top
is greater than any other definable time, and is equal only to itself. It may be used as a sentinel value, representing a time infinitely far into the future.- Definition Classes
- Time → TimeLikeOps
- val Undefined: Time
An undefined value: behaves like
Double.NaN
An undefined value: behaves like
Double.NaN
- Definition Classes
- Time → TimeLikeOps
- val Zero: Time
The zero value
The zero value
- Definition Classes
- TimeLikeOps
- def apply(date: Date): Time
Creates a Time instance of the given
Date
. - final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def at(datetime: String): Time
Creates a Time instance at the given
datetime
string in the "yyyy-MM-dd HH:mm:ss Z" format.Creates a Time instance at the given
datetime
string in the "yyyy-MM-dd HH:mm:ss Z" format.val time = Time.at("2019-05-12 00:00:00 -0800") time.toString // 2019-05-12 08:00:00 +0000
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- val epoch: Time
The unix epoch.
The unix epoch. Times are measured relative to this.
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- def fromDays(days: Int): Time
- Definition Classes
- TimeLikeOps
- def fromFractionalSeconds(seconds: Double): Time
Make a new
This
from the given number of seconds.Make a new
This
from the given number of seconds. Because this method takes a Double, it can represent values less than a second. Note however that there is some slop in floating-point conversion that limits precision. Currently we can assume at least microsecond precision.- Definition Classes
- Time → TimeLikeOps
- def fromHours(hours: Int): Time
- Definition Classes
- TimeLikeOps
- def fromInstant(instant: Instant): Time
- def fromMicroseconds(micros: Long): Time
- Definition Classes
- Time → TimeLikeOps
- def fromMilliseconds(millis: Long): Time
- Definition Classes
- Time → TimeLikeOps
- def fromMinutes(minutes: Int): Time
- Definition Classes
- Time → TimeLikeOps
- def fromNanoseconds(nanoseconds: Long): Time
Make a new
This
from the given number of nanosecondsMake a new
This
from the given number of nanoseconds- Definition Classes
- Time → TimeLikeOps
- def fromRss(rss: String): Time
Returns the Time parsed from a string in RSS format.
Returns the Time parsed from a string in RSS format. Eg: "Wed, 15 Jun 2005 19:00:00 GMT"
- def fromSeconds(seconds: Int): Time
- Definition Classes
- Time → TimeLikeOps
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def now: Time
Returns the current Time.
Returns the current Time.
Note that returned values are not monotonic. This means it is not suitable for measuring durations where the clock cannot drift backwards. If monotonicity is desired prefer a monotonic Stopwatch.
The current time can be manipulated via Time.withTimeAt, Time.withCurrentTimeFrozen, Time.withTimeFunction and Time.sleep.
While
now
is not a "global" it is however properly propagated through to other code via the standard usage of Locals throughoututil
. Specifically, code using Futures, FuturePools, and MockTimers will have their code see the manipulated values. - def nowNanoPrecision: Time
Returns the current Time with, at best, nanosecond precision.
Returns the current Time with, at best, nanosecond precision.
Note that nanosecond precision is only available in JDK versions greater than JDK8. In JDK8 this API has the same precision as Time#now and
System#currentTimeMillis
. In JDK9+ this will change and all timestamps taken from this API will have nanosecond resolution.Note that returned values are not monotonic. This means it is not suitable for measuring durations where the clock cannot drift backwards. If monotonicity is desired prefer a monotonic Stopwatch.
- def sleep(duration: Duration): Unit
Puts the currently executing thread to sleep for the given duration, according to object Time.
Puts the currently executing thread to sleep for the given duration, according to object Time.
This is useful for testing.
val time = Time.fromMilliseconds(123456L) Time.withTimeAt(time) { timeControl => assert(Time.now == time) // you can control time via the `TimeControl` instance. timeControl.advance(2.seconds) FuturePool.unboundedPool { assert(Time.now == time + 2.seconds) } }
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- def withCurrentTimeFrozen[A](body: (TimeControl) => A): A
Runs the given body at the current time.
Runs the given body at the current time. Makes for simple, fast, predictable unit tests that are dependent on time.
- Note
this intended for use in tests.
val time = Time.fromMilliseconds(123456L) Time.withTimeAt(time) { timeControl => assert(Time.now == time) // you can control time via the `TimeControl` instance. timeControl.advance(2.seconds) FuturePool.unboundedPool { assert(Time.now == time + 2.seconds) } }
- def withTimeAt[A](time: Time)(body: (TimeControl) => A): A
Runs the given body at a specified time.
Runs the given body at a specified time. Makes for simple, fast, predictable unit tests that are dependent on time.
- Note
this intended for use in tests.
val time = Time.fromMilliseconds(123456L) Time.withTimeAt(time) { timeControl => assert(Time.now == time) // you can control time via the `TimeControl` instance. timeControl.advance(2.seconds) FuturePool.unboundedPool { assert(Time.now == time + 2.seconds) } }
- def withTimeFunction[A](timeFunction: => Time)(body: (TimeControl) => A): A
Execute body with the time function replaced by
timeFunction
WARNING: This is only meant for testing purposes.Execute body with the time function replaced by
timeFunction
WARNING: This is only meant for testing purposes. You can break it with nested calls if you have an outstanding Future executing in a worker pool. - object Finite
An extractor for finite TimeLikes; eg.:
An extractor for finite TimeLikes; eg.:
duration match { case Duration.Finite(d) => ... case Duration.Top => ..
- Definition Classes
- TimeLikeOps
- object Nanoseconds
An extractor for finite
This
, yielding its value in nanoseconds.An extractor for finite
This
, yielding its value in nanoseconds.duration match { case Duration.Nanoseconds(ns) => ... case Duration.Top => ... }
- Definition Classes
- TimeLikeOps