silk.system.system_clock_native.
The system provider of the SystemClock effect: the time of day, as seconds since 1970.
When to use#
Provide OsSystemClock for SystemClock in a program, for timestamps. The same provider
works on each supported target, a browser included. In a test, provide a fake that returns a
fixed time.
Details#
Each call reads the real-time clock of the system, or on wasm32-unknown-unknown the host's
silk.clock import (performance.timeOrigin + performance.now()). A failure of the clock is a
trap.
Gotchas#
This clock can go back when the system time is set. Use
silk.system.monotonic_clock_nativeto measure durations.On Linux, it needs glibc 2.17 or later.
The provider exists on a JavaScript host (
wasm32-unknown-unknown, whose host suppliessilk.clock), WASI, Windows on x86_64, Darwin on x86_64 or aarch64, and GNU Linux on x86_64 or aarch64.
Examples#
Read the time#
import silk.core.effect { Effect }
import silk.system.system_clock_native { OsSystemClock }
import silk.system.system_clock { SystemClock }
pub fn main() -> i32 {
let mut clock = OsSystemClock.make()
let now = run SystemClock.now() |> Effect.provideMut<SystemClock>(&mut clock)
// Seconds since 1970. 1600000000 is in September 2020.
if now.unixSeconds() < 1600000000 || now.subsecondNanoseconds() >= 1000000000 {
return 1
}
return 0
}Import as OsSystemClock with import silk.system.system_clock_native { OsSystemClock }.
Public declarations: 1.
OsSystemClock#
pub struct OsSystemClockThe system SystemClock provider: the time of day, as seconds since 1970. It has no state. A
failure is a trap.
Gotchas#
This clock can go back when the system time is set. Use
silk.system.monotonic_clock_nativeto measure durations.On Linux, it needs glibc 2.17 or later.
Implementation OsSystemClock#
impl OsSystemClockAssociated function make#
pub fn make() -> OsSystemClockReturns a clock. It does not read the time.
Implementation SystemClock for OsSystemClock#
impl SystemClock for OsSystemClockOperation now#
effect fn now(self: &mut Self) -> InstantOperation getResolution#
effect fn getResolution(self: &mut Self) -> u64Category: System.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, x86_64-unknown-linux-gnu, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.