Package io.opencensus.common
Class Timestamp
java.lang.Object
io.opencensus.common.Timestamp
- All Implemented Interfaces:
Comparable<Timestamp>
- Direct Known Subclasses:
AutoValue_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 -
Method Summary
Modifier and TypeMethodDescriptionaddDuration
(Duration duration) Returns aTimestamp
calculated as thisTimestamp
plus someDuration
.addNanos
(long nanosToAdd) Returns aTimestamp
calculated as thisTimestamp
plus some number of nanoseconds.int
Compares thisTimestamp
to the specifiedTimestamp
.static Timestamp
create
(long seconds, int nanos) Creates a new timestamp from given seconds and nanoseconds.private static long
floorDiv
(long x, long y) private static long
floorMod
(long x, long y) static Timestamp
fromMillis
(long epochMilli) Creates a new timestamp from the given milliseconds.abstract int
getNanos()
Returns the number of nanoseconds after the number of seconds since the Unix Epoch represented by this timestamp.abstract long
Returns the number of seconds since the Unix Epoch represented by this timestamp.private static Timestamp
ofEpochSecond
(long epochSecond, long nanoAdjustment) private Timestamp
plus
(long secondsToAdd, long nanosToAdd) subtractTimestamp
(Timestamp timestamp) Returns aDuration
calculated as:this - timestamp
.
-
Constructor Details
-
Timestamp
Timestamp()
-
-
Method Details
-
create
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
Timestamp
with specified fields. - Throws:
IllegalArgumentException
- if the arguments are out of range.- Since:
- 0.5
-
fromMillis
Creates a new timestamp from the given milliseconds.- Parameters:
epochMilli
- the timestamp represented in milliseconds since epoch.- Returns:
- new
Timestamp
with specified fields. - Throws:
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
Returns aTimestamp
calculated as thisTimestamp
plus some number of nanoseconds.- Parameters:
nanosToAdd
- the nanos to add, positive or negative.- Returns:
- the calculated
Timestamp
. For invalid inputs, aTimestamp
of zero is returned. - Throws:
ArithmeticException
- if numeric overflow occurs.- Since:
- 0.5
-
addDuration
Returns aTimestamp
calculated as thisTimestamp
plus someDuration
.- Parameters:
duration
- theDuration
to add.- Returns:
- a
Timestamp
with the specifiedDuration
added. - Since:
- 0.5
-
subtractTimestamp
Returns aDuration
calculated as:this - timestamp
.- Parameters:
timestamp
- theTimestamp
to subtract.- Returns:
- the calculated
Duration
. For invalid inputs, aDuration
of zero is returned. - Since:
- 0.5
-
compareTo
Compares thisTimestamp
to the specifiedTimestamp
.- Specified by:
compareTo
in interfaceComparable<Timestamp>
- Parameters:
otherTimestamp
- the otherTimestamp
to compare to, notnull
.- Returns:
- the comparator value: zero if equal, negative if this timestamp happens before otherTimestamp, positive if after.
- Throws:
NullPointerException
- if otherTimestamp isnull
.
-
plus
-
ofEpochSecond
-
floorDiv
private static long floorDiv(long x, long y) -
floorMod
private static long floorMod(long x, long y)
-