std.time.Duration
class pub DurationA span of time measured in nanoseconds.
A Duration can be used to measure the span of time without having to worry
about casting the time to different scales yourself. A Duration can be
created using various scales such as seconds and milliseconds.
Duration measures a time duration in nanoseconds, limiting it to durations
of up to 292 years. Operations producing values greater than this range will
panic.
Duration objects can represent negative durations such as "-5 seconds". This
is useful when performing arithmetic on Duration objects, as you won't have
to worry about overflows. It also lets you represent a duration that goes back
in time, i.e. "something that happened 5 seconds ago".
Static methods
from_micros
Show source codeHide source code
fn pub static from_micros[T: ToInt](micros: ref T) -> Duration {
Duration(micros.to_int * MILLIS_PER_SEC)
}fn pub static from_micros[T: ToInt](micros: ref T) -> DurationCreates a new Duration from the given number of microseconds.
Examples
import std.time (Duration)
Duration.from_micros(10)
from_millis
Show source codeHide source code
fn pub static from_millis[T: ToInt](millis: ref T) -> Duration {
Duration(millis.to_int * MICROS_PER_SEC)
}fn pub static from_millis[T: ToInt](millis: ref T) -> DurationCreates a new Duration from the given number of milliseconds.
Examples
import std.time (Duration)
Duration.from_millis(10)
from_nanos
Show source codeHide source code
fn pub static from_nanos[T: ToInt](nanos: ref T) -> Duration {
Duration(nanos.to_int)
}fn pub static from_nanos[T: ToInt](nanos: ref T) -> DurationCreates a new Duration from the given number of nanoseconds.
Examples
import std.time (Duration)
Duration.from_nanos(10)
from_secs
Show source codeHide source code
fn pub static from_secs[T: ToFloat](secs: ref T) -> Duration {
Duration((secs.to_float * NANOS_PER_SEC).to_int)
}fn pub static from_secs[T: ToFloat](secs: ref T) -> DurationCreates a new Duration from the given number of seconds.
Examples
import std.time (Duration)
Duration.from_secs(10.5)
Instance methods
!=
Show source codeHide source code
fn pub !=(other: T) -> Bool {
(self == other).false?
}fn pub !=(other: T) -> BoolReturns true if self and the given object are not equal to each other.
*
Show source codeHide source code
fn pub *(other: ref Int) -> Duration {
Duration(@nanos * other)
}fn pub *(other: ref Int) -> DurationMultiplies self with the given object.
+
Show source codeHide source code
fn pub +(other: ref Duration) -> Duration {
Duration(@nanos + other.nanos)
}fn pub +(other: ref Duration) -> DurationAdds the given object to self.
-
Show source codeHide source code
fn pub -(other: ref Duration) -> Duration {
Duration(@nanos - other.nanos)
}fn pub -(other: ref Duration) -> DurationSubtracts 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) -> BoolReturns 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) -> BoolReturns true if self is lower than or equal to the given argument.
==
Show source codeHide source code
fn pub ==(other: ref Duration) -> Bool {
@nanos == other.nanos
}fn pub ==(other: ref Duration) -> BoolReturns 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) -> BoolReturns 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) -> BoolReturns true if self is equal to or greater than the given argument.
clone
Show source codeHide source code
fn pub clone -> Duration {
Duration(@nanos)
}fn pub clone -> DurationCreates a clone of self.
cmp
Show source codeHide source code
fn pub cmp(other: ref Duration) -> Ordering {
@nanos.cmp(other.nanos)
}fn pub cmp(other: ref Duration) -> OrderingReturns the ordering between self and the given argument.
The returned value should be as follows:
a == b:Ordering.Equala > b:Ordering.Greatera < b:Ordering.Less
fmt
Show source codeHide source code
fn pub fmt(formatter: mut Formatter) {
let abs = @nanos.absolute
let write = if abs >= 1_000_000_000 {
'${to_secs} sec'
} else if abs >= 1_000_000 {
'${to_millis} msec'
} else if abs >= 1_000 {
'${to_micros} µsec'
} else {
'${@nanos} nsec'
}
formatter.write(write)
}fn pub fmt(formatter: mut Formatter)Formats self in a human-readable format for debugging purposes.
to_instant
Show source codeHide source code
fn pub to_instant -> Instant {
Instant.new + self
}fn pub to_instant -> InstantAdds self to the current monotonic time, returning a new Instant
representing the resulting point in time.
Examples
import std.time (Duration, Instant)
let duration = Duration.from_secs(5)
duration.to_instant.remaining <= duration # => true
Panics
This method panics if the resulting Instant is invalid, such as when it's
a negative time.
to_micros
Show source codeHide source code
fn pub to_micros -> Int {
@nanos / MILLIS_PER_SEC
}fn pub to_micros -> IntReturns the duration in microseconds.
Examples
import std.time (Duration)
Duration.from_secs(5).to_micros # => 5000000
to_millis
Show source codeHide source code
fn pub to_millis -> Int {
@nanos / MICROS_PER_SEC
}fn pub to_millis -> IntReturns the duration in milliseconds.
Examples
import std.time (Duration)
Duration.from_secs(5).to_millis # => 5000
to_nanos
Show source codeHide source code
fn pub to_nanos -> Int {
@nanos
}fn pub to_nanos -> IntReturns the duration in nanoseconds.
Examples
import std.time (Duration)
Duration.from_secs(5).to_nanos # => 5000000000
to_secs
Show source codeHide source code
fn pub to_secs -> Float {
@nanos.to_float / NANOS_PER_SEC
}fn pub to_secs -> FloatReturns the duration in seconds.
Examples
import std.time (Duration)
Duration.from_secs(5).to_secs # => 5.0
Implemented traits
Clone
impl Clone[Duration] for DurationCompare
impl Compare[Duration] for DurationEqual
impl Equal[ref Duration] for DurationFormat
impl Format for DurationAdd
impl Add[Duration, Duration] for DurationMultiply
impl Multiply[Int, Duration] for DurationSubtract
impl Subtract[Duration, Duration] for DurationToInstant
impl ToInstant for Duration