std.time.Instant
class pub Instant
A monotonically increasing clock.
The clock is measured in nanoseconds since an unspecified point in time. This means the maximum time span we can represent is roughly 292 years. Operations producing values greater than this range will panic.
Unlike a system clock a monotonic clock never decreases, making it useful for tasks such as measuring the execution time of a block of code.
An Instant
can never represent a negative time (e.g. -5).
Static methods
new
Show source codeHide source code
fn pub static new -> Instant {
Instant(inko_time_monotonic(_INKO.state) as Int)
}
fn pub static new -> Instant
Returns a new Instant
representing the current time.
Instance methods
!=
Show source codeHide source code
fn pub !=(other: T) -> Bool {
(self == other).false?
}
fn pub !=(other: T) -> Bool
Returns true
if self
and the given object are not equal to each other.
+
Show source codeHide source code
fn pub +(other: ref Duration) -> Instant {
let nanos = @nanos + other.nanos
if nanos < 0 { panic("Instant can't represent a negative time (${nanos})") }
Instant(nanos)
}
fn pub +(other: ref Duration) -> Instant
Adds the given object to self
.
-
Show source codeHide source code
fn pub -(other: ref Duration) -> Instant {
let nanos = @nanos - other.nanos
if nanos < 0 { panic("Instant can't represent a negative time (${nanos})") }
Instant(nanos)
}
fn pub -(other: ref Duration) -> Instant
Subtracts the given object from self
.
<
Show source codeHide source code
fn pub <(other: ref T) -> Bool {
match cmp(other) {
case Less -> true
case _ -> false
}
}
fn pub <(other: ref T) -> Bool
Returns true
if self
is lower than the given argument.
<=
Show source codeHide source code
fn pub <=(other: ref T) -> Bool {
match cmp(other) {
case Less or Equal -> true
case _ -> false
}
}
fn pub <=(other: ref T) -> Bool
Returns true
if self
is lower than or equal to the given argument.
==
Show source codeHide source code
fn pub ==(other: ref Instant) -> Bool {
@nanos == other.nanos
}
fn pub ==(other: ref Instant) -> Bool
Returns true
if self
and the given object are equal to each other.
This operator is used to perform structural equality. This means two objects residing in different memory locations may be considered equal, provided their structure is equal. For example, two different arrays may be considered to have structural equality if they contain the exact same values.
>
Show source codeHide source code
fn pub >(other: ref T) -> Bool {
match cmp(other) {
case Greater -> true
case _ -> false
}
}
fn pub >(other: ref T) -> Bool
Returns true
if self
is greater than the given argument.
>=
Show source codeHide source code
fn pub >=(other: ref T) -> Bool {
match cmp(other) {
case Greater or Equal -> true
case _ -> false
}
}
fn pub >=(other: ref T) -> Bool
Returns true
if self
is equal to or greater than the given argument.
clone
Show source codeHide source code
fn pub clone -> Instant {
Instant(@nanos)
}
fn pub clone -> Instant
Creates a clone of self
.
cmp
Show source codeHide source code
fn pub cmp(other: ref Instant) -> Ordering {
@nanos.cmp(other.nanos)
}
fn pub cmp(other: ref Instant) -> Ordering
Returns the ordering between self
and the given argument.
The returned value should be as follows:
a == b
:Ordering.Equal
a > b
:Ordering.Greater
a < b
:Ordering.Less
elapsed
Show source codeHide source code
fn pub elapsed -> Duration {
Duration(inko_time_monotonic(_INKO.state) as Int - @nanos)
}
fn pub elapsed -> Duration
Returns a Duration
measuring the time elapsed since the point in time
that self
represents.
Panics
This method panics if the duration can't be expressed in nanoseconds.
Examples
Obtaining the time elapsed:
import std.process
import std.time (Duration, Instant)
let start = Instant.new
process.sleep(Duration.from_secs(1))
start.elapsed.to_secs >= 1.0 # => true
fmt
Show source codeHide source code
fn pub fmt(formatter: mut Formatter) {
formatter.write('+${@nanos}')
}
fn pub fmt(formatter: mut Formatter)
Formats self
in a human-readable format for debugging purposes.
remaining
Show source codeHide source code
fn pub remaining -> Duration {
Duration(@nanos - (inko_time_monotonic(_INKO.state) as Int))
}
fn pub remaining -> Duration
Returns a Duration
measuring the time remaining until reaching self
.
If self
is in the past, the Duration
represents a negative duration.
Examples
import std.time (Duration, Instant)
let time = Instant.new + Duration.from_secs(5)
time.remaining # => Duration.from_secs(5)
to_float
Show source codeHide source code
fn pub to_float -> Float {
@nanos.to_float
}
fn pub to_float -> Float
Converts self
to a Float
to_instant
Show source codeHide source code
fn pub to_instant -> Instant {
clone
}
fn pub to_instant -> Instant
Converts self
into an Instant
.
to_int
Show source codeHide source code
fn pub to_int -> Int {
@nanos.to_int
}
fn pub to_int -> Int
Converts self
to a Int
Implemented traits
Clone
impl Clone[Instant] for Instant
Compare
impl Compare[Instant] for Instant
Equal
impl Equal[ref Instant] for Instant
ToFloat
impl ToFloat for Instant
Format
impl Format for Instant
ToInt
impl ToInt for Instant
Add
impl Add[Duration, Instant] for Instant
Subtract
impl Subtract[Duration, Instant] for Instant
ToInstant
impl ToInstant for Instant