diff --git a/Cargo.toml b/Cargo.toml index d18b58a..65444d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,9 @@ winapi = { version = "0.3.8", features = ["sysinfoapi", "minwindef"] } [target.'cfg(target_os = "redox")'.dependencies] redox_syscall = "0.1.29" +[target.'cfg(target_os = "wasi")'.dependencies] +wasi = "0.10" + [features] default = [ "format", "parse" ] format = [ "num-traits", "pad", "locale" ] diff --git a/src/system.rs b/src/system.rs index f69a4aa..de9f256 100644 --- a/src/system.rs +++ b/src/system.rs @@ -69,7 +69,7 @@ fn file_time_as_u64(ft: &FILETIME) -> u64 { /// Returns the system’s current time, as a tuple of seconds elapsed since /// the Unix epoch, and the millisecond of the second. -#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "redox", windows)))] +#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "redox", target_os = "wasi", windows)))] pub unsafe fn sys_time() -> (i64, i16) { let mut tv = libc::timespec { tv_sec: 0, tv_nsec: 0 }; let _ = clock_gettime(libc::CLOCK_REALTIME, &mut tv); @@ -86,6 +86,17 @@ pub fn sys_time() -> (i64, i16) { (ts.tv_sec, (ts.tv_nsec / 1000) as i16) } +/// Returns the system’s current time, as a tuple of seconds elapsed since +/// the Unix epoch, and the millisecond of the second. +/// +/// This may panic if the clock was not made available +#[cfg(target_os = "wasi")] +pub unsafe fn sys_time() -> (i64, i16) { + let ns = wasi::clock_time_get(wasi::CLOCKID_REALTIME, 1_000_000) + .expect("WASI clock unavailable"); + ((ns / 1_000_000_000) as i64, (ns / 1_000_000 % 1000) as i16) +} + /// Attempts to determine the system’s current time zone. There’s no /// guaranteed way to do this, so this function returns `None` if no /// timezone could be found.