Skip to content
CryoCryo home
Stdlibtime

duration

import std::time::duration; · source

Types

Duration

type struct Duration {
    secs:  u64;
    nanos: u32;

    static new(secs: u64, nanos: u32) -> Duration;
    static zero() -> Duration;
    static second() -> Duration;
    static millisecond() -> Duration;
    static microsecond() -> Duration;
    static nanosecond() -> Duration;
    static from_secs(secs: u64) -> Duration;
    static from_millis(millis: u64) -> Duration;
    static from_micros(micros: u64) -> Duration;
    static from_nanos(nanos: u64) -> Duration;
    static from_secs_f64(secs: f64) -> Duration;
    as_secs(&this) -> u64;
    subsec_nanos(&this) -> u32;
    subsec_millis(&this) -> u32;
    subsec_micros(&this) -> u32;
    as_millis(&this) -> u64;
    as_micros(&this) -> u64;
    as_nanos(&this) -> u64;
    as_secs_f64(&this) -> f64;
    is_zero(&this) -> boolean;
    checked_add(&this, other: &Duration) -> Option<Duration>;
    checked_sub(&this, other: &Duration) -> Option<Duration>;
    saturating_add(&this, other: &Duration) -> Duration;
    saturating_sub(&this, other: &Duration) -> Duration;
    scale(&this, factor: u64) -> Duration;
    mul_f64(&this, factor: f64) -> Duration;
    divide(&this, divisor: u64) -> Duration;
    to_string(&this) -> String;
    to_debug_string(&this) -> String;
}

Whole seconds plus a sub-second nanosecond remainder, always normalized so that nanos < 1_000_000_000.

import std::time;

const timeout: Duration = Duration::from_millis(250);
sleep(timeout);

Properties

PropertyType
secsu64
nanosu32

Constructors

static new(secs: u64, nanos: u32) -> Duration;

Construct from a (seconds, nanoseconds) pair, carrying any nanosecond overflow (>= 1e9) into the seconds field so the stored value is always normalized.

static zero() -> Duration;

The zero duration.

static second() -> Duration;

One second.

static millisecond() -> Duration;

One millisecond.

static microsecond() -> Duration;

One microsecond.

static nanosecond() -> Duration;

One nanosecond.

static from_secs(secs: u64) -> Duration;
static from_millis(millis: u64) -> Duration;
static from_micros(micros: u64) -> Duration;
static from_nanos(nanos: u64) -> Duration;
static from_secs_f64(secs: f64) -> Duration;

Construct from a floating-point number of seconds. Negative inputs clamp to zero.

Methods

as_secs(&this) -> u64;

Whole seconds (the sub-second part is dropped).

subsec_nanos(&this) -> u32;

The sub-second remainder in nanoseconds (< 1e9).

subsec_millis(&this) -> u32;

The sub-second remainder in whole milliseconds (< 1000).

subsec_micros(&this) -> u32;

The sub-second remainder in whole microseconds (< 1_000_000).

as_millis(&this) -> u64;

Total whole milliseconds.

as_micros(&this) -> u64;

Total whole microseconds.

as_nanos(&this) -> u64;

Total nanoseconds. Wraps for spans beyond ~584 years.

as_secs_f64(&this) -> f64;

The whole span as floating-point seconds.

is_zero(&this) -> boolean;
checked_add(&this, other: &Duration) -> Option<Duration>;

this + other, or None if the seconds field would overflow.

checked_sub(&this, other: &Duration) -> Option<Duration>;

this - other, or None when other is the larger.

saturating_add(&this, other: &Duration) -> Duration;

this + other, saturating at the maximum representable span on overflow.

saturating_sub(&this, other: &Duration) -> Duration;

this - other, saturating at zero when other is the larger.

scale(&this, factor: u64) -> Duration;

this * factor.

mul_f64(&this, factor: f64) -> Duration;

this * factor for a floating-point factor.

divide(&this, divisor: u64) -> Duration;

this / divisor. Panics on division by zero.

to_string(&this) -> String;

Render the most natural unit as an owned String (1.5s, 250ms, 10ns) - the same text as the Display impl, available directly without an f-string.

to_debug_string(&this) -> String;

Render the Debug form (Duration { secs: S, nanos: N }) as an owned String.

Building

new(secs, nanos) carries any nanosecond overflow into the seconds field, so the stored value is always normalized. Then the unit constructors — from_secs, from_millis, from_micros, from_nanos, and from_secs_f64 (negative input clamps to zero) — plus the named constants zero(), second(), millisecond(), microsecond(), and nanosecond().

Reading

as_secs() drops the sub-second part; as_millis(), as_micros(), and as_nanos() give the whole span in that unit and as_secs_f64() as fractional seconds; the subsec_* accessors give just the remainder.

Arithmetic

checked_add and checked_sub return Option<Duration>, None on overflow or underflow. saturating_add and saturating_sub clamp instead. scale(factor), mul_f64(factor), and divide(divisor) scale the span; mul_f64 clamps a negative factor to zero.

to_string() renders in the largest sensible unit — 1.5s, 250ms, 10ns — the same text as the Display impl, and to_debug_string() the Debug form.

Trait implementations

implement trait Eq for struct Duration
implement trait Ord for struct Duration
implement trait Display for struct Duration
implement trait Debug for struct Duration

Constants

const NANOS_PER_SEC:   u64 = 1000000000;
const NANOS_PER_MILLI: u64 = 1000000;
const NANOS_PER_MICRO: u64 = 1000;
const MILLIS_PER_SEC:  u64 = 1000;
const MICROS_PER_SEC:  u64 = 1000000;
const U64_MAX:         u64 = 0xFFFFFFFFFFFFFFFF;