Class Duration

java.lang.Object
io.opencensus.common.Duration
All Implemented Interfaces:
Comparable<Duration>
Direct Known Subclasses:
AutoValue_Duration

@Immutable public abstract class Duration extends Object implements Comparable<Duration>
Represents a signed, fixed-length span of time represented as a count of seconds and fractions of seconds at nanosecond resolution. It is independent of any calendar and concepts like "day" or "month". Range is approximately +-10,000 years.
Since:
0.5
  • Constructor Details

    • Duration

      Duration()
  • Method Details

    • create

      public static Duration create(long seconds, int nanos)
      Creates a new time duration from given seconds and nanoseconds.
      Parameters:
      seconds - Signed seconds of the span of time. Must be from -315,576,000,000 to +315,576,000,000 inclusive.
      nanos - Signed fractions of a second at nanosecond resolution of the span of time. Durations less than one second are represented with a 0 `seconds` field and a positive or negative `nanos` field. For durations of one second or more, a non-zero value for the `nanos` field must be of the same sign as the `seconds` field. Must be from -999,999,999 to +999,999,999 inclusive.
      Returns:
      new Duration with specified fields.
      Throws:
      IllegalArgumentException - if the arguments are out of range or have inconsistent sign.
      Since:
      0.5
    • fromMillis

      public static Duration fromMillis(long millis)
      Creates a new Duration from given milliseconds.
      Parameters:
      millis - the duration in milliseconds.
      Returns:
      a new Duration from given milliseconds.
      Throws:
      IllegalArgumentException - if the number of milliseconds is out of the range that can be represented by Duration.
      Since:
      0.5
    • toMillis

      public long toMillis()
      Converts a Duration to milliseconds.
      Returns:
      the milliseconds representation of this Duration.
      Since:
      0.13
    • getSeconds

      public abstract long getSeconds()
      Returns the number of seconds in the Duration.
      Returns:
      the number of seconds in the Duration.
      Since:
      0.5
    • getNanos

      public abstract int getNanos()
      Returns the number of nanoseconds in the Duration.
      Returns:
      the number of nanoseconds in the Duration.
      Since:
      0.5
    • compareTo

      public int compareTo(Duration otherDuration)
      Compares this Duration to the specified Duration.
      Specified by:
      compareTo in interface Comparable<Duration>
      Parameters:
      otherDuration - the other Duration to compare to, not null.
      Returns:
      the comparator value: zero if equal, negative if this duration is smaller than otherDuration, positive if larger.
      Throws:
      NullPointerException - if otherDuration is null.