Package io.opencensus.common
Class Timestamp
- java.lang.Object
-
- io.opencensus.common.Timestamp
-
- All Implemented Interfaces:
java.lang.Comparable<Timestamp>
- Direct Known Subclasses:
AutoValue_Timestamp
@Immutable public abstract class Timestamp extends java.lang.Object implements java.lang.Comparable<Timestamp>
A representation of an instant in time. The instant is the number of nanoseconds after the number of seconds since the Unix Epoch.Use
Tracing.getClock().now()to get the current timestamp since epoch (1970-01-01T00:00:00Z).- Since:
- 0.5
-
-
Constructor Summary
Constructors Constructor Description Timestamp()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description TimestampaddDuration(Duration duration)Returns aTimestampcalculated as thisTimestampplus someDuration.TimestampaddNanos(long nanosToAdd)Returns aTimestampcalculated as thisTimestampplus some number of nanoseconds.intcompareTo(Timestamp otherTimestamp)Compares thisTimestampto the specifiedTimestamp.static Timestampcreate(long seconds, int nanos)Creates a new timestamp from given seconds and nanoseconds.private static longfloorDiv(long x, long y)private static longfloorMod(long x, long y)static TimestampfromMillis(long epochMilli)Creates a new timestamp from the given milliseconds.abstract intgetNanos()Returns the number of nanoseconds after the number of seconds since the Unix Epoch represented by this timestamp.abstract longgetSeconds()Returns the number of seconds since the Unix Epoch represented by this timestamp.private static TimestampofEpochSecond(long epochSecond, long nanoAdjustment)private Timestampplus(long secondsToAdd, long nanosToAdd)DurationsubtractTimestamp(Timestamp timestamp)Returns aDurationcalculated as:this - timestamp.
-
-
-
Method Detail
-
create
public static Timestamp create(long seconds, int nanos)
Creates a new timestamp from given seconds and nanoseconds.- Parameters:
seconds- Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must be from from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.nanos- Non-negative fractions of a second at nanosecond resolution. Negative second values with fractions must still have non-negative nanos values that count forward in time. Must be from 0 to 999,999,999 inclusive.- Returns:
- new
Timestampwith specified fields. - Throws:
java.lang.IllegalArgumentException- if the arguments are out of range.- Since:
- 0.5
-
fromMillis
public static Timestamp fromMillis(long epochMilli)
Creates a new timestamp from the given milliseconds.- Parameters:
epochMilli- the timestamp represented in milliseconds since epoch.- Returns:
- new
Timestampwith specified fields. - Throws:
java.lang.IllegalArgumentException- if the number of milliseconds is out of the range that can be represented byTimestamp.- Since:
- 0.5
-
getSeconds
public abstract long getSeconds()
Returns the number of seconds since the Unix Epoch represented by this timestamp.- Returns:
- the number of seconds since the Unix Epoch.
- Since:
- 0.5
-
getNanos
public abstract int getNanos()
Returns the number of nanoseconds after the number of seconds since the Unix Epoch represented by this timestamp.- Returns:
- the number of nanoseconds after the number of seconds since the Unix Epoch.
- Since:
- 0.5
-
addNanos
public Timestamp addNanos(long nanosToAdd)
Returns aTimestampcalculated as thisTimestampplus some number of nanoseconds.- Parameters:
nanosToAdd- the nanos to add, positive or negative.- Returns:
- the calculated
Timestamp. For invalid inputs, aTimestampof zero is returned. - Throws:
java.lang.ArithmeticException- if numeric overflow occurs.- Since:
- 0.5
-
addDuration
public Timestamp addDuration(Duration duration)
Returns aTimestampcalculated as thisTimestampplus someDuration.- Parameters:
duration- theDurationto add.- Returns:
- a
Timestampwith the specifiedDurationadded. - Since:
- 0.5
-
subtractTimestamp
public Duration subtractTimestamp(Timestamp timestamp)
Returns aDurationcalculated as:this - timestamp.- Parameters:
timestamp- theTimestampto subtract.- Returns:
- the calculated
Duration. For invalid inputs, aDurationof zero is returned. - Since:
- 0.5
-
compareTo
public int compareTo(Timestamp otherTimestamp)
Compares thisTimestampto the specifiedTimestamp.- Specified by:
compareToin interfacejava.lang.Comparable<Timestamp>- Parameters:
otherTimestamp- the otherTimestampto compare to, notnull.- Returns:
- the comparator value: zero if equal, negative if this timestamp happens before otherTimestamp, positive if after.
- Throws:
java.lang.NullPointerException- if otherTimestamp isnull.
-
plus
private Timestamp plus(long secondsToAdd, long nanosToAdd)
-
ofEpochSecond
private static Timestamp ofEpochSecond(long epochSecond, long nanoAdjustment)
-
floorDiv
private static long floorDiv(long x, long y)
-
floorMod
private static long floorMod(long x, long y)
-
-