silk.system.system_clock_native.
The native provider of the SystemClock effect: the time of day, as seconds since 1970.
When to use#
Provide OsSystemClock for SystemClock in a native program, for timestamps. In a test,
provide a fake that returns a fixed time.
Details#
Each call reads the real-time clock of the system. 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 WASI, Darwin on aarch64, and Linux with GNU libc 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 { Instant, 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.
let nanoseconds = SystemClock.nanoseconds(&now)
if SystemClock.seconds(&now) < 1600000000 || nanoseconds < 0 || nanoseconds >= 1000000000 {
return 1
}
return 0
}Import as OsSystemClock with import silk.system.system_clock_native { OsSystemClock }.
Public declarations: 1.
OsSystemClock#
pub struct OsSystemClockThe native 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-wasip1, wasm32-wasip2.