Skip to main content

huff_neo_utils/
time.rs

1use cfg_if;
2
3// `std::time::SystemTime` panics on `wasm32-unknown-unknown` target so use a u64 timestamp
4// instead
5cfg_if::cfg_if! {
6    if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
7        use js_sys;
8
9        /// Time is represented as a u64 unix timestamp on wasm32-unknown-unknown
10        pub type Time = u64;
11
12        /// Returns the current time
13        pub fn get_current_time() -> Time {
14            (js_sys::Date::now() / 1000.0) as u64
15        }
16    } else {
17        use std::time::SystemTime;
18
19        /// Time is represented as a SystemTime on other targets
20        pub type Time = SystemTime;
21
22        /// Returns the current time
23        pub fn get_current_time() -> Time {
24            SystemTime::now()
25        }
26    }
27}