Skip to content

Commit

Permalink
fix time
Browse files Browse the repository at this point in the history
  • Loading branch information
boralg committed Jun 26, 2024
1 parent 4848f1f commit 9647390
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions sursface/src/time.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
use web_time::Instant;
use lazy_static::lazy_static;

#[cfg(target_arch = "wasm32")]
mod platform_time {
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = performance)]
fn now() -> f64;
}

pub fn now() -> f64 {
now() / 1000.0 // Convert milliseconds to seconds
}
}

#[cfg(not(target_arch = "wasm32"))]
mod platform_time {
use lazy_static::lazy_static;
use std::time::Instant;

lazy_static! {
static ref TIME_AT_START: Instant = Instant::now();
}

pub fn now() -> f64 {
TIME_AT_START.elapsed().as_secs_f64()
}
}

lazy_static! {
static ref TIME_AT_START: Instant = Instant::now();
static ref TIME_AT_START: f64 = platform_time::now();
}

pub fn now() -> f32 {
Instant::now().duration_since(*TIME_AT_START).as_secs_f64() as f32
}
(platform_time::now() - *TIME_AT_START) as f32
}

0 comments on commit 9647390

Please sign in to comment.