silk.system.system_clock.
The SystemClock effect: the current date and time as Unix time, and the Instant type that
both clocks use.
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#
An Instant has signed whole seconds since 1970-01-01T00:00:00Z and a nanosecond part from
0 to 999,999,999. 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#
import silk.core.effect { Effect }
import silk.system.system_clock { Instant, SystemClock }
struct FixedClock {}
effect fn fixedNow(self: &mut FixedClock) -> Instant {
return SystemClock.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 SystemClock.seconds(&instant) != 1700000000 || SystemClock.nanoseconds(&instant) != 500 {
return 1
}
return 0
}Import as SystemClock with import silk.system.system_clock { SystemClock }.
Public declarations: 2.
Instant#
pub struct InstantA point in time: signed whole seconds and a nanosecond part.
Details#
For the system clock, the seconds count from 1970-01-01T00:00:00Z. A monotonic clock uses the
same type with an origin that it does not specify. The nanosecond part is always from 0 to
999,999,999, also for times before the origin. So SystemClock.make(-1, 999999999) is one
nanosecond before the origin.
SystemClock#
pub service SystemClockThe effect for the current date and time as Unix 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: signed whole seconds since 1970-01-01T00:00:00Z and a nanosecond
part. 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#
effect fn now() -> Instant ? &mut SystemClockReturns the current time. It can be earlier than an earlier read, because the system time can be adjusted.
Operation getResolution#
effect fn getResolution() -> u64 ? &mut SystemClockReturns the tick of the clock in nanoseconds. It is at least 1. The real precision can be lower.
Implementation SystemClock#
impl SystemClockAssociated function make#
pub fn make(seconds: i64, nanoseconds: i64) -> InstantReturns the instant seconds plus nanoseconds nanoseconds.
Details#
Whole seconds in nanoseconds move into the seconds, so SystemClock.make(0, 5000000000) is
five seconds and SystemClock.make(0, -1) is one nanosecond before the origin.
Gotchas#
A total outside the i64 seconds range traps with an integer overflow.
Method seconds#
pub fn seconds(self: &Instant) -> i64Returns the whole seconds of the instant. It can be negative.
Method nanoseconds#
pub fn nanoseconds(self: &Instant) -> i64Returns the nanosecond part of the instant, from 0 to 999,999,999.
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.