Skip to content
CryoCryo home
Stdlibtime

clock

import std::time::clock; · source

Types

Instant

type struct Instant {
    secs:  i64;
    nanos: i64;

    static now() -> Instant;
    duration_since(&this, earlier: &Instant) -> Duration;
    saturating_duration_since(&this, earlier: &Instant) -> Duration;
    elapsed(&this) -> Duration;
    as_nanos(&this) -> i64;
    checked_add(&this, dur: &Duration) -> Option<Instant>;
    checked_sub(&this, dur: &Duration) -> Option<Instant>;
}

The monotonic clock. Instant only ever moves forward, which makes it the right tool for measuring elapsed time. It is opaque: a single reading is meaningful only relative to another one.

const start: Instant = Instant::now();
do_work();
println(f"took {start.elapsed()}");

elapsed() is the span from this reading until now, and duration_since(earlier) the span from another reading — saturating_duration_since is an alias naming the saturation explicitly. as_nanos() exposes the raw monotonic count, from an unspecified epoch, for lightweight elapsed math where carrying a Duration is overkill. checked_add and checked_sub return None on overflow or on going below zero.

Underneath it is CLOCK_MONOTONIC via clock_gettime on POSIX, and QueryPerformanceCounter scaled by QueryPerformanceFrequency on Windows.

Properties

PropertyType
secsi64
nanosi64

Constructors

static now() -> Instant;

Read the monotonic clock now.

Methods

duration_since(&this, earlier: &Instant) -> Duration;

The time elapsed from earlier to this (saturating at zero).

saturating_duration_since(&this, earlier: &Instant) -> Duration;

Alias for duration_since, naming the saturation explicitly.

elapsed(&this) -> Duration;

The time elapsed from this until now (saturating at zero).

as_nanos(&this) -> i64;

This reading as raw monotonic nanoseconds. The absolute value is meaningful only relative to another Instant - it counts from an unspecified epoch (typically system boot) - so use it for lightweight elapsed-time math where the difference of two readings is all that matters and carrying a Duration would be overkill.

checked_add(&this, dur: &Duration) -> Option<Instant>;

this + dur, or None if the reading would overflow i64.

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

this - dur, or None if the reading would go below zero.

Trait implementations

implement trait Eq for struct Instant
implement trait Ord for struct Instant

SystemTime

type struct SystemTime {
    secs:  i64;
    nanos: i64;

    static now() -> SystemTime;
    static unix_epoch() -> SystemTime;
    static from_unix_secs(secs: i64) -> SystemTime;
    unix_secs(&this) -> i64;
    duration_since_epoch(&this) -> Duration;
    duration_since(&this, earlier: &SystemTime) -> Duration;
    checked_add(&this, dur: &Duration) -> Option<SystemTime>;
}

The wall clock. SystemTime is subject to NTP steps and manual changes, so it is not for measuring elapsed time. Use it when you need an actual date.

unix_secs() is whole seconds since the epoch, negative before 1970, and duration_since_epoch() saturates at zero for a clock set before then. duration_since(earlier) is only meaningful if the clock has not been stepped in between.

POSIX reads CLOCK_REALTIME; Windows uses GetSystemTimePreciseAsFileTime and converts from FILETIME ticks since 1601 to the Unix epoch. SystemTime has no Eq or Ord impl — compare through unix_secs or duration_since.

Properties

PropertyType
secsi64
nanosi64

Constructors

static now() -> SystemTime;

Read the wall clock now.

static unix_epoch() -> SystemTime;

The Unix epoch (1970-01-01 00:00:00 UTC).

static from_unix_secs(secs: i64) -> SystemTime;

A wall-clock time from a whole-second Unix timestamp.

Methods

unix_secs(&this) -> i64;

Whole seconds since the Unix epoch (may be negative before 1970).

duration_since_epoch(&this) -> Duration;

Time since the Unix epoch. Saturates at zero for a clock set before 1970.

duration_since(&this, earlier: &SystemTime) -> Duration;

The time elapsed from earlier to this (saturating at zero). Only meaningful if the wall clock has not been stepped between the two readings.

checked_add(&this, dur: &Duration) -> Option<SystemTime>;

this + dur.

Free Functions

function sleep(dur: Duration) -> void;

Park the current thread for at least dur. POSIX path restarts across EINTR using the kernel's remaining-time readout; Windows Sleep is uninterruptible at the millisecond granularity it accepts. Sub-millisecond dur rounds UP to 1 ms on Windows so a non-zero request always blocks.

The POSIX path uses nanosleep and restarts across EINTR using the kernel's remaining-time readout, so a signal does not cut the sleep short. Windows uses Sleep, which is uninterruptible at the millisecond granularity it accepts; a sub-millisecond request rounds up to 1 ms there, so a non-zero request always blocks.

Constants

const NSEC_PER_SEC:           i64 = 1000000000;
const I64_MAX:                i64 = 0x7FFFFFFFFFFFFFFF;
const FILETIME_TO_UNIX_TICKS: i64 = 116444736000000000;
const FILETIME_TICKS_PER_SEC: i64 = 10000000;
const FILETIME_NS_PER_TICK:   i64 = 100;