1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use crate::snapshots::{
    common::{
        error::Errno,
        types::{__wasi_clockid_t, __wasi_errno_t, __wasi_timestamp_t},
    },
    WasiCtx,
};

pub fn wasi_clock_res_get(clock_id: __wasi_clockid_t::Type) -> Result<u64, Errno> {
    match clock_id {
        __wasi_clockid_t::__WASI_CLOCKID_MONOTONIC => Ok(1),
        __wasi_clockid_t::__WASI_CLOCKID_REALTIME => Ok(1),
        _ => Err(Errno(__wasi_errno_t::__WASI_ERRNO_BADF)),
    }
}

pub fn wasi_clock_time_get(
    ctx: &WasiCtx,
    clock_id: __wasi_clockid_t::Type,
    _precision: __wasi_timestamp_t,
) -> Result<u64, Errno> {
    use std::time::SystemTime;
    match clock_id {
        __wasi_clockid_t::__WASI_CLOCKID_REALTIME | __wasi_clockid_t::__WASI_CLOCKID_MONOTONIC => {
            let d = SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .unwrap();
            Ok(d.as_nanos() as u64)
        }
        _ => Err(Errno(__wasi_errno_t::__WASI_ERRNO_NODEV)),
    }
}