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

Duration for intervals between two points in time.

Stopwatch for measuring elapsed time.

TimeFormat for converting to and from String representations.

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

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

    An undefined value: behaves like Double.NaN

    An undefined value: behaves like Double.NaN

    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.

    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
  11. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  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: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  15. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  16. def fromDays(days: Int): Time
    Definition Classes
    TimeLikeOps
  17. 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
  18. def fromHours(hours: Int): Time
    Definition Classes
    TimeLikeOps
  19. def fromMicroseconds(micros: Long): Time
    Definition Classes
    TimeTimeLikeOps
  20. def fromMilliseconds(millis: Long): Time
    Definition Classes
    TimeTimeLikeOps
  21. def fromMinutes(minutes: Int): Time
    Definition Classes
    TimeTimeLikeOps
  22. 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
  23. 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"

  24. def fromSeconds(seconds: Int): Time
    Definition Classes
    TimeTimeLikeOps
  25. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  26. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  27. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  28. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  29. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  30. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  31. 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.

  32. 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.

  33. 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)
      }
    }
  34. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  35. def toString(): String
    Definition Classes
    AnyRef → Any
  36. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  37. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  38. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  39. 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)
      }
    }
  40. 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)
      }
    }
  41. 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.

  42. 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
  43. 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 TimeLikeOps[Time]

Inherited from AnyRef

Inherited from Any

Ungrouped