Packages

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

TimeFormat for converting to and from String representations.

Stopwatch for measuring elapsed time.

Duration for intervals between two points in time.

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Time
  2. Serializable
  3. Serializable
  4. TimeLikeOps
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. 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
    TimeTimeLikeOps
  5. 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
    TimeTimeLikeOps
  6. val Undefined: Time
    Definition Classes
    TimeTimeLikeOps
  7. val Zero: Time

    The zero value

    The zero value

    Definition Classes
    TimeLikeOps
  8. def apply(date: Date): Time

    Creates a Time instance of the given Date.

  9. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  10. def at(datetime: String): Time

    Creates a Time instance at the given datetime string in the "yyyy-MM-dd HH:mm:ss Z" format.

  11. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  12. val epoch: Time

    The unix epoch.

    The unix epoch. Times are measured relative to this.

  13. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  14. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  15. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  16. 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
    TimeTimeLikeOps
  17. def fromMicroseconds(micros: Long): Time
    Definition Classes
    TimeTimeLikeOps
  18. def fromMilliseconds(millis: Long): Time
    Definition Classes
    TimeTimeLikeOps
  19. def fromNanoseconds(nanoseconds: Long): Time

    Make a new This from the given number of nanoseconds

    Make a new This from the given number of nanoseconds

    Definition Classes
    TimeTimeLikeOps
  20. 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"

  21. def fromSeconds(seconds: Int): Time
    Definition Classes
    TimeTimeLikeOps
  22. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  23. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  24. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  25. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  26. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  27. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  28. 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 throughout util. Specifically, code using Futures, FuturePools, and MockTimers will have their code see the manipulated values.

  29. 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)
      }
    }
  30. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  31. def toString(): String
    Definition Classes
    AnyRef → Any
  32. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  33. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  34. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  35. 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)
      }
    }
  36. 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)
      }
    }
  37. 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.

  38. 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
  39. 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

Inherited from Serializable

Inherited from Serializable

Inherited from TimeLikeOps[Time]

Inherited from AnyRef

Inherited from Any

Ungrouped