silk.system.system_clock.

The SystemClock effect: the current date and time.

When to use#

Require &mut SystemClock when you need the current time to show to a person, to store, or to send to another system, such as a timestamp in a log or a certificate check. To measure durations, use silk.system.monotonic_clock, because the system time can jump.

Details#

SystemClock.now returns an Instant of silk.time.instant: a point on the UTC timeline with nanosecond precision. The system time can go forward or back when the computer adjusts its clock. Importing this module does not install a clock. The program provides one.

Gotchas#

If a provider returns a value that is not valid, the program traps, because this effect has no error type.

Examples#

Provide a fixed time in a test#

silk
import silk.core.effect { Effect }
import silk.system.system_clock { SystemClock }
import silk.time.instant { Instant }

struct FixedClock {}

effect fn fixedNow(self: &mut FixedClock) -> Instant {
  return Instant.make(1700000000, 500)
}

effect fn fixedResolution(self: &mut FixedClock) -> u64 {
  return 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 instant.unixSeconds() != 1700000000 || instant.subsecondNanoseconds() != 500 {
    return 1
  }
  return 0
}

Import as SystemClock with import silk.system.system_clock { SystemClock }.

Public declarations: 1.

SystemClock#

silk
pub service SystemClock

The effect for the current date and time.

When to use#

Require &mut SystemClock when you need the current time to show to a person, to store, or to send to another system, such as a timestamp in a log or a certificate check. To measure durations, use silk.system.monotonic_clock, because the system time can jump.

Details#

The time is an Instant of silk.time.instant. The system time can go forward or back when the computer adjusts its clock. Exclusive access lets a test provider change the time between calls. There is no operation to set the time.

Operation now#

silk
effect fn now() -> Instant ? &mut SystemClock

Returns the current time. It can be earlier than an earlier read, because the system time can be adjusted.

Operation getResolution#

silk
effect fn getResolution() -> u64 ? &mut SystemClock

Returns the tick of the clock in nanoseconds. It is at least 1. The real precision can be lower.

Category: System.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page