Browse the docs

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_native to 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#

silk
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#

silk
pub struct OsSystemClock

The 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_native to measure durations.

  • On Linux, it needs glibc 2.17 or later.

Implementation OsSystemClock#

silk
impl OsSystemClock

Associated function make#

silk
pub fn make() -> OsSystemClock

Returns a clock. It does not read the time.

Implementation SystemClock for OsSystemClock#

silk
impl SystemClock for OsSystemClock

Operation now#

silk
effect fn now(self: &mut Self) -> Instant

Operation getResolution#

silk
effect fn getResolution(self: &mut Self) -> u64

Category: System.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, x86_64-unknown-linux-gnu, wasm32-wasip1, wasm32-wasip2.

View the source of this page