1use std::time::{Duration, Instant, SystemTime};
2
3use crate::{check_concern, hostcalls, log_concern};
4
5pub fn now() -> SystemTime {
7 check_concern("now", hostcalls::get_current_time()).expect("failed to fetch realtime clock")
8}
9
10#[allow(dead_code)]
11struct Timespec {
12 tv_sec: i64,
13 tv_nsec: u32,
14}
15
16#[cfg(target_arch = "wasm32")]
18pub fn instant_now() -> Instant {
19 let raw_ns: u64 = unsafe { wasi::clock_time_get(wasi::CLOCKID_MONOTONIC, 0) }
21 .expect("failed to fetch monotonic time");
22 debug_assert_eq!(
23 std::mem::size_of::<Instant>(),
24 std::mem::size_of::<Timespec>()
25 );
26 unsafe {
27 std::mem::transmute::<Timespec, Instant>(Timespec {
28 tv_sec: (raw_ns / 1000000000) as i64,
29 tv_nsec: (raw_ns % 1000000000) as u32,
30 })
31 }
32}
33
34#[cfg(not(target_arch = "wasm32"))]
35pub fn instant_now() -> Instant {
36 Instant::now()
37}
38
39pub fn set_tick_period(period: Duration) {
41 log_concern("set-tick-period", hostcalls::set_tick_period(period));
42}