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
| Property | Type |
|---|---|
secs | i64 |
nanos | i64 |
Constructors
static now() -> Instant;
static now() -> Instant;Read the monotonic clock now.
Methods
duration_since(&this, earlier: &Instant) -> Duration;
duration_since(&this, earlier: &Instant) -> Duration;The time elapsed from earlier to this (saturating at zero).
saturating_duration_since(&this, earlier: &Instant) -> Duration;
saturating_duration_since(&this, earlier: &Instant) -> Duration;Alias for duration_since, naming the saturation explicitly.
elapsed(&this) -> Duration;
elapsed(&this) -> Duration;The time elapsed from this until now (saturating at zero).
as_nanos(&this) -> i64;
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>;
checked_add(&this, dur: &Duration) -> Option<Instant>;this + dur, or None if the reading would overflow i64.
checked_sub(&this, dur: &Duration) -> Option<Instant>;
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
| Property | Type |
|---|---|
secs | i64 |
nanos | i64 |
Constructors
static now() -> SystemTime;
static now() -> SystemTime;Read the wall clock now.
static unix_epoch() -> SystemTime;
static unix_epoch() -> SystemTime;The Unix epoch (1970-01-01 00:00:00 UTC).
static from_unix_secs(secs: i64) -> SystemTime;
static from_unix_secs(secs: i64) -> SystemTime;A wall-clock time from a whole-second Unix timestamp.
Methods
unix_secs(&this) -> i64;
unix_secs(&this) -> i64;Whole seconds since the Unix epoch (may be negative before 1970).
duration_since_epoch(&this) -> Duration;
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;
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>;
checked_add(&this, dur: &Duration) -> Option<SystemTime>;this + dur.
Free Functions
function sleep(dur: Duration) -> void;
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;type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Instant {
secs: i64;
nanos: i64;
}A measurement of the monotonic clock. Opaque: the fields are the raw timespec, meaningful only relative to another Instant. POD.
Instant.secs: i64Instant.nanos: i64static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static Instant::now() -> InstantRead the monotonic clock now.
type struct Instant {
secs: i64;
nanos: i64;
}A measurement of the monotonic clock. Opaque: the fields are the raw timespec, meaningful only relative to another Instant. POD.
Instant::duration_since(&this, earlier: &Instant) -> DurationThe time elapsed from earlier to this (saturating at zero).
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) earlier: &Instanttype struct Duration {
secs: u64;
nanos: u32;
}A length of time: whole secs plus a sub-second nanos remainder (0 <= nanos < 1_000_000_000). POD; copy-by-value, no drop.
Instant::saturating_duration_since(&this, earlier: &Instant) -> DurationAlias for duration_since, naming the saturation explicitly.
Instant::elapsed(&this) -> DurationThe time elapsed from this until now (saturating at zero).
Instant::as_nanos(&this) -> i64This 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.
Instant::checked_add(&this, dur: &Duration) -> Option<Instant>this + dur, or None if the reading would overflow i64.
(parameter) dur: &DurationInstant::checked_sub(&this, dur: &Duration) -> Option<Instant>this - dur, or None if the reading would go below zero.
const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const start: Instantconst start: InstantInstant::elapsed(&this) -> DurationThe time elapsed from this until now (saturating at zero).
type struct SystemTime {
secs: i64;
nanos: i64;
}A measurement of the wall clock (CLOCK_REALTIME) as a raw timespec. duration_since_epoch() yields a Unix timestamp. Subject to NTP steps and manual changes; do not use for elapsed-time measurement. POD.
SystemTime.secs: i64SystemTime.nanos: i64static SystemTime::now() -> SystemTimeRead the wall clock now.
type struct SystemTime {
secs: i64;
nanos: i64;
}A measurement of the wall clock (CLOCK_REALTIME) as a raw timespec. duration_since_epoch() yields a Unix timestamp. Subject to NTP steps and manual changes; do not use for elapsed-time measurement. POD.
static SystemTime::unix_epoch() -> SystemTimeThe Unix epoch (1970-01-01 00:00:00 UTC).
static SystemTime::from_unix_secs(secs: i64) -> SystemTimeA wall-clock time from a whole-second Unix timestamp.
(parameter) secs: i64SystemTime::unix_secs(&this) -> i64Whole seconds since the Unix epoch (may be negative before 1970).
SystemTime::duration_since_epoch(&this) -> DurationTime since the Unix epoch. Saturates at zero for a clock set before 1970.
SystemTime::duration_since(&this, earlier: &SystemTime) -> DurationThe time elapsed from earlier to this (saturating at zero). Only meaningful if the wall clock has not been stepped between the two readings.
(parameter) earlier: &SystemTimeSystemTime::checked_add(&this, dur: &Duration) -> Option<SystemTime>this + dur.
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;import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
Instant::duration_since(&this, earlier: &Instant) -> DurationThe time elapsed from earlier to this (saturating at zero).
Instant::saturating_duration_since(&this, earlier: &Instant) -> DurationAlias for duration_since, naming the saturation explicitly.
Instant::as_nanos(&this) -> i64This 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.
Instant::checked_add(&this, dur: &Duration) -> Option<Instant>this + dur, or None if the reading would overflow i64.
Instant::checked_sub(&this, dur: &Duration) -> Option<Instant>this - dur, or None if the reading would go below zero.
Option::NoneClockId::CLOCK_MONOTONIC = 1Instant.secs: i64Instant.nanos: i64implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype trait Ord : Eq {
compare(&this, other: &This) -> Ordering;
}Types that have a total order: every pair is Less, Equal, or Greater. compare must be consistent with equals: if a.equals(b) then a.compare(b) == Equal.
SystemTime::unix_secs(&this) -> i64Whole seconds since the Unix epoch (may be negative before 1970).
std::time::clockSystemTime::duration_since_epoch(&this) -> DurationTime since the Unix epoch. Saturates at zero for a clock set before 1970.
std::time::clockClockId::CLOCK_REALTIME = 0static SystemTime::unix_epoch() -> SystemTimeThe Unix epoch (1970-01-01 00:00:00 UTC).
std::time::clockstatic SystemTime::from_unix_secs(secs: i64) -> SystemTimeA wall-clock time from a whole-second Unix timestamp.
std::time::clockfunction name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function sleep(dur: Duration) -> voidPark 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.
(parameter) dur: Durationtype struct Sleep {
deadline: i64;
id: u64;
rt: Reactor*;
}A future that completes once the monotonic clock reaches its deadline.
std::future::timer