.nib std
nib 0.3.0

module use "std/time.nib"

Time

The clock: Time.Clock.now() reads wall-clock milliseconds, sleep(ms) waits. Time.System is the real one; a test writes its own.

Time has one interface, Time.Clock, with the two things a program asks of a clock: what time is it, and wait a while. A function that reads the clock says ! Time.Clock, so it is plain from its signature that its answer can change from run to run — and a test can bind a clock that cannot.

use "std/time.nib"

module Job where
  func timed :: -> String ! Time.Clock =
    let start = @Time.Clock.now();
    let _ = @Time.Clock.sleep(50);
    "took {@Time.Clock.now() - start} ms"

wire W runs Job.timed where Time.Clock = Time.System

Bind Time.System for the machine's clock.

A clock for tests

A clock is just an interface, so a test writes an instance that answers what the test needs. This one starts at a fixed time and moves forward only when the program sleeps:

use "std/time.nib"

effector Manual is Time.Clock where
  config start :: Int = 1700000000000;
  unkeyed var slept :: Int = 0;
  now() = start + slept
  sleep(ms) = if ms > 0 then (slept := slept + ms; ()) else ()

module Job where
  func timed :: -> String ! Time.Clock =
    let before = @Time.Clock.now();
    let _ = @Time.Clock.sleep(250);
    let _ = @Time.Clock.sleep(-5);
    "took {@Time.Clock.now() - before} ms"

wire Test runs Job.timed where Time.Clock = Manual { start: 0 }
took 250 ms

Interfaces

interfaceClock
interface Clock where
  now() -> Int;
  sleep(ms: Int) -> ()

Reading and waiting.

  • now() answers the wall-clock time in milliseconds since the Unix epoch, UTC. Because it is the wall clock rather than a monotonic counter, two readings can go backwards when the system clock is adjusted; don't rely on now() never decreasing.
  • sleep(ms) waits at least ms milliseconds, then answers (). It may wait longer. A zero or negative ms returns immediately.

An instance of your own must keep the same unit: milliseconds since the epoch.

Effectors

effectorTime.System builtin instance
effector Time.System is Time.Clock  -- installed by nibc
  now :: -> Int
  sleep :: Int -> ()

The machine's clock, installed by nibc. now() reads the system's wall clock and sleep(ms) really waits. Its readings differ on every run, which is exactly why a test binds a clock of its own instead.

use "std/time.nib"

module Main where
  func main :: -> String ! Time.Clock =
    let _ = @Time.Clock.sleep(1);
    "slept"

wire W runs Main.main where Time.Clock = Time.System
effectorTime.Sim builtin instance
effector Time.Sim is Time.Clock  -- installed by nibc
  now :: -> Int
  sleep :: Int -> ()

A virtual clock, for testing. now() starts at 0 and moves only when no fibre can run, jumping to the next event, so a test that sleeps ten minutes takes no wall time and two runs read the same times. Pair it with Net.Sim, whose message delays it measures.

use "std/time.nib"

module Wait where
  func main :: -> Int ! Time.Clock =
    let _ = @Time.Clock.sleep(600000);
    @Time.Clock.now()

wire W runs Wait.main where Time.Clock = Time.Sim