silk/system_clock
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc.
Provider-replaceable Unix-epoch time with canonical split-second instants.
When to use
Require SystemClock when an application needs the current time for reporting, persistence,
or exchange with an external Unix-time boundary. Use silk.monotonic_clock instead when
measuring elapsed time.
Details
Instant stores signed seconds from 1970-01-01T00:00:00Z and a canonical non-negative
nanosecond fraction. The clock may move forwards or backwards when its external reference is
adjusted. Providers are explicit and lexically replaceable; importing this module installs no
ambient clock.
Gotchas
SystemClock.getResolution reports a positive nominal tick in whole nanoseconds. The tick may be a lower
bound on observable precision. An invalid or unrepresentable provider result traps because this
service has no typed failure channel.
Examples
Read a fixed instant through an ordinary provider
import silk.effect { Effect }
import silk.system_clock { SystemClock, Instant }
import silk.u64
struct FixedClock {}
effect fn fixedNow(self: &mut FixedClock) -> Instant {
return SystemClock.make(-1, 999999999)
}
effect fn fixedResolution(self: &mut FixedClock) -> u64 {
return u64.toU64(1)
}
impl SystemClock for FixedClock {
now: FixedClock.fixedNow
getResolution: FixedClock.fixedResolution
}
pub fn main() -> i32 {
let mut provider = FixedClock {}
let instant = run SystemClock.now()
|> Effect.provideMut<SystemClock>(&mut provider)
if SystemClock.seconds(&instant) != -1 { return 1 }
if SystemClock.nanoseconds(&instant) != 999999999 { return 2 }
return 42
}Import as SystemClock with import silk.system_clock { SystemClock }.
Public declarations: 2.
Instant
pub struct InstantA canonical point on a clock represented by signed seconds and a nanosecond fraction.
Details
System-clock seconds are measured from 1970-01-01T00:00:00Z. A monotonic provider may reuse
this representation with an unspecified provider-local origin. The fraction is always in
0..1000000000; consequently { seconds: -1, nanoseconds: 999999999 } is one nanosecond before
zero.
SystemClock
pub service SystemClockAn explicit provider of Unix-epoch time and nominal clock resolution.
Details
Exclusive access permits deterministic providers to advance a script between calls. The service does not install an implementation or expose a clock-setting operation.
Operation now
effect<'static> fn now() -> Instant ? &mut SystemClockReads the current Unix-epoch instant. The result is canonical but need not be greater than an earlier read because the external reference may be adjusted.
Operation getResolution
effect<'static> fn getResolution() -> u64 ? &mut SystemClockReturns the provider-reported positive nominal resolution in whole nanoseconds. The value may be a lower bound on actual observable precision.
Associated function SystemClock.make
pub fn make(seconds: i64, nanoseconds: i64) -> InstantCreates a canonical split-second instant.
Gotchas
nanoseconds must be non-negative and less than one billion. A noncanonical fraction traps
instead of being normalized into another field pair.
Associated function SystemClock.seconds
pub fn seconds<'life0>(self: &'life0 silk/system_clock.Instant) -> i64Returns the signed whole-second component of an instant.
Associated function SystemClock.nanoseconds
pub fn nanoseconds<'life0>(self: &'life0 silk/system_clock.Instant) -> i64Returns the canonical non-negative nanosecond fraction of an instant.