#[cfg(not(target_arch = "wasm32"))]
pub fn real_now_secs() -> f64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs_f64()
}
#[cfg(target_arch = "wasm32")]
pub fn real_now_secs() -> f64 {
js_sys::Date::now() / 1000.0
}
pub fn now_secs() -> f64 {
#[cfg(feature = "testing")]
if let Some(t) = crate::testing::test_clock() {
return t;
}
real_now_secs()
}
pub fn now_ms() -> u64 {
(now_secs() * 1000.0) as u64
}
#[cfg(not(target_arch = "wasm32"))]
#[derive(Debug, Clone, Copy)]
pub struct Instant(std::time::Instant);
#[cfg(not(target_arch = "wasm32"))]
impl Instant {
pub fn now() -> Self {
Instant(std::time::Instant::now())
}
pub fn elapsed(&self) -> std::time::Duration {
self.0.elapsed()
}
pub fn elapsed_ms(&self) -> u64 {
self.0.elapsed().as_millis() as u64
}
}
#[cfg(target_arch = "wasm32")]
#[derive(Debug, Clone, Copy)]
pub struct Instant(f64);
#[cfg(target_arch = "wasm32")]
impl Instant {
pub fn now() -> Self {
Instant(js_sys::Date::now())
}
pub fn elapsed(&self) -> std::time::Duration {
std::time::Duration::from_secs_f64((js_sys::Date::now() - self.0).max(0.0) / 1000.0)
}
pub fn elapsed_ms(&self) -> u64 {
(js_sys::Date::now() - self.0).max(0.0) as u64
}
}