Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down
13 changes: 12 additions & 1 deletion src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
Expand Down