Expand description
Time values and the clocks that read them, for code where a nanosecond counts.
Each timeline has its own type and its own clocks, so a reading from one is never subtracted from another’s:
| Reading | Timeline | Read by |
|---|---|---|
Timestamp | wall clock, from 1970 UTC | SystemClock, CoarseSystemClock |
TaiTimestamp | TAI, from 1970 TAI | TaiClock |
Uptime | monotonic clock, near boot | MonotonicClock, CoarseMonotonicClock |
RawUptime | monotonic clock, unslewed | RawMonotonicClock |
BootUptime | boot clock, with suspensions | BootClock |
Tickstamp | hardware counter | Counter |
Timedelta | CPU time used, a span | ProcessCpuClock, ThreadCpuClock |
The span between two points is a Timedelta, or for a Tickstamp, a Tickdelta that its
counter’s TickRate turns into one.
Every point and span is an i64, every operator saturates at the ends of the range, with a
checked_* twin, and every clock answers one verb, now. A workspace that
denies clippy::arithmetic_side_effects names each point and span in its clippy configuration’s
arithmetic-side-effects-allowed.
§Choosing a Clock
- Stamp a capture with
SystemClock. Its reading names a moment another machine names too. It may step backwards across a correction. - Wait on
MonotonicClock. It never steps, so a deadline isMonotonicClock.now() + timeout. - Measure with
Counter. One instruction that touches no memory, and one counter for every core and process on the machine. - Poll with
CoarseSystemClockorCoarseMonotonicClock. Either clock as the kernel’s timer last left it, which reads no counter; never for a stamp. - Drive a test or a replay with
ManualClock, orAtomicManualClockacross threads.
The OS clocks need the std feature, on 64-bit Linux or macOS; clock has them all.
§Examples
use t2t::clock::{Clock, ManualClock};
use t2t::{Timed, Timedelta, Timestamp};
let clock = ManualClock::new(Timestamp::from_secs(1_700_000_000));
let sample = Timed::new(clock.now(), 101_u64);
clock.advance(Timedelta::from_millis(1_500));
assert!(sample.elapsed(clock.now()) > Timedelta::SECOND, "stale a second and a half on");§Crate Features
None is on by default, and nothing reaches the operating system unless std is named; what
std adds is on 64-bit Linux and macOS.
| Feature | Adds |
|---|---|
std | the OS clocks, an x86_64 counter’s measured rate, SystemTime conversions |
serde | every value’s spelling, and the serde modules for counts in a named unit |
schemars | JsonSchema for every value with a spelling; turns serde on |
zerocopy | the zerocopy traits each type can honour, native-endian |
chrono-04 | conversions to and from chrono 0.4’s DateTime and TimeDelta |
jiff-02 | conversions to and from jiff 0.2’s Timestamp and SignedDuration |
time-03 | conversions to and from time 0.3’s OffsetDateTime and Duration |
Modules§
- clock
- Clocks that read the time: the OS’s wall and monotonic clocks, the CPU’s counter, and clocks set by hand.
- serde
serde - How a time crosses to a config file, a log or a peer.
Structs§
- Boot
Uptime - A point on the boot clock, in nanoseconds since boot, the machine’s suspensions counted.
- OutOf
Range Error - Why a time does not fit the type it was converted to: a negative span for a
Duration, or a value past what a count of nanoseconds holds. - Parse
TaiTimestamp Error - Why a string is not a
TaiTimestamp. - Parse
Tick Rate Error - Why a string is not a
TickRate. - Parse
Tickdelta Error - Why a string is not a
Tickdelta, nor aTickstamp. - Parse
Timedelta Error - Why a string is not a
Timedelta, nor a point counted from boot. - Parse
Timestamp Error - Why a string is not a
Timestamp. - RawUptime
- A point on the monotonic clock at the hardware’s own rate, in nanoseconds since an origin near boot.
- TaiTimestamp
- A point on International Atomic Time, in nanoseconds since 1970-01-01T00:00:00 TAI.
- Tick
Rate - How many ticks a counter advances in a second: what turns
Tickdeltainto aTimedeltaand back. - Tickdelta
- A signed span of counter ticks: how far apart two
Tickstamps are. - Tickstamp
- A point on a hardware counter, in ticks since an origin the hardware chose.
- Timed
- A value
Tand the stampSits writer captured it with. - Timedelta
- A signed span of nanoseconds: how far apart two points on one timeline are.
- Timestamp
- A point on the wall clock, in nanoseconds since the Unix epoch.
- Uptime
- A point on the monotonic clock, in nanoseconds since an origin near boot.
- UtcDate
Time - A point on the wall clock read as a date and a time of day: UTC, in the proleptic Gregorian calendar.
Traits§
- Time
Point - A point on one timeline, carried as a count of its unit since the timeline’s origin, with the span between two of them.