Clock
Module clock
import clock
| Module | clock |
| Source | clock/clock.zirr, clock/module-docs.zirr |
Two kinds of clock, deliberately kept apart.
There are two clocks, and confusing them is a bug. SystemClock reads civil time and can jump when the host is corrected — use it to timestamp and to do calendar work. MonotonicClock only moves forward and has a meaningless origin — use it to measure how long something took.
The attributes keep them apart at the type level: code that measures elapsed time requires HasMonotonicClock and therefore cannot be handed a wall clock by mistake.
Both are plain data holding a function, so a test supplies its own. fixed freezes time, stepping advances it predictably, and steppingMonotonic with a zero step makes every reading identical — which is how code under a timeout is tested without waiting for one.
Dependencies
Contents
- Data —
MonotonicClock,SystemClock - Attributes —
HasMonotonicClock,HasSystemClock - Functions —
fixed,instant,now,stepping,steppingMonotonic
Data
MonotonicClock
`clock/clock.zirr:22`
data MonotonicClock {
now: fn() -> Instant
}
A clock that only ever moves forward, whose origin carries no meaning.
Fields
| Field | Description |
|---|---|
now |
SystemClock
`clock/clock.zirr:17`
data SystemClock {
now: fn() -> Timestamp
}
A clock reading civil time, which can jump when the host is corrected.
Fields
| Field | Description |
|---|---|
now |
Attributes
HasMonotonicClock
`clock/clock.zirr:12`
attr HasMonotonicClock {
clock(self: @HasMonotonicClock) -> MonotonicClock
}
Provides the monotonic clock, for measuring how long something took.
Kept apart from HasSystemClock because a wall clock can jump, so code measuring elapsed time must not be handed one.
Fields
| Field | Description |
|---|---|
clock |
HasSystemClock
`clock/clock.zirr:6`
attr HasSystemClock {
clock(self: @HasSystemClock) -> SystemClock
}
Provides the wall clock, for timestamping and calendar work.
Fields
| Field | Description |
|---|---|
clock |
Functions
fixed
`clock/clock.zirr:37`
fn fixed(at: Timestamp) -> SystemClock
A wall clock frozen at one time, for tests that must see a fixed date.
instant
`clock/clock.zirr:32`
fn instant(c: MonotonicClock) -> Instant
The current monotonic reading. Subtract two to learn how much time passed between them.
now
`clock/clock.zirr:27`
fn now(c: SystemClock) -> Timestamp
The current wall-clock time.
stepping
`clock/clock.zirr:42`
fn stepping(from: Timestamp, step: Duration) -> SystemClock
A wall clock starting at from and advancing by step on every reading, for tests that need time to pass predictably.
steppingMonotonic
`clock/clock.zirr:53`
fn steppingMonotonic(step: Duration) -> MonotonicClock
A monotonic clock starting at the origin and advancing by step on every reading.
A step of zero makes every reading identical, which is how code under a timeout is tested without waiting.