Object/Class

com.twitter.util

Time

Related Docs: class Time | package util

Permalink

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

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. val Bottom: Time

    Permalink

    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. object Finite

    Permalink

    An extractor for finite TimeLikes; eg.:

    An extractor for finite TimeLikes; eg.:

    duration match {
      case Duration.Finite(d) => ...
      case Duration.Top => ..
    Definition Classes
    TimeLikeOps
  6. object Nanoseconds

    Permalink

    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
  7. val Top: Time

    Permalink

    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
  8. val Undefined: Time

    Permalink

    An undefined value: behaves like Double.NaN

    An undefined value: behaves like Double.NaN

    Definition Classes
    TimeTimeLikeOps
  9. val Zero: Time

    Permalink

    The zero value

    The zero value

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

    Permalink

    Creates a Time instance of the given Date.

  11. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  12. def at(datetime: String): Time

    Permalink

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

  13. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  14. val epoch: Time

    Permalink

    The unix epoch.

    The unix epoch. Times are measured relative to this.

  15. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  16. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  17. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  18. def fromFractionalSeconds(seconds: Double): Time

    Permalink

    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
  19. def fromMicroseconds(micros: Long): Time

    Permalink
    Definition Classes
    TimeTimeLikeOps
  20. def fromMilliseconds(millis: Long): Time

    Permalink
    Definition Classes
    TimeTimeLikeOps
  21. def fromNanoseconds(nanoseconds: Long): Time

    Permalink

    Make a new This from the given number of nanoseconds

    Make a new This from the given number of nanoseconds

    Definition Classes
    TimeTimeLikeOps
  22. def fromRss(rss: String): Time

    Permalink

    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"

  23. def fromSeconds(seconds: Int): Time

    Permalink
    Definition Classes
    TimeTimeLikeOps
  24. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  25. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  26. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  27. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  28. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  29. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  30. def now: Time

    Permalink

    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.

  31. def sleep(duration: Duration): Unit

    Permalink

    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)
      }
    }
  32. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  33. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  34. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  35. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  36. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  37. def withCurrentTimeFrozen[A](body: (TimeControl) ⇒ A): A

    Permalink

    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)
      }
    }
  38. def withTimeAt[A](time: Time)(body: (TimeControl) ⇒ A): A

    Permalink

    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)
      }
    }
  39. def withTimeFunction[A](timeFunction: ⇒ Time)(body: (TimeControl) ⇒ A): A

    Permalink

    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.

Inherited from Serializable

Inherited from Serializable

Inherited from TimeLikeOps[Time]

Inherited from AnyRef

Inherited from Any

Ungrouped