diff --git a/src/wasm.rs b/src/wasm.rs index 3e416b4..004a710 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -16,7 +16,14 @@ impl Eq for Instant {} impl Instant { #[inline] pub fn now() -> Self { - Instant(duration_from_f64(now())) + // `performance.now()` starts counting from the creation of the page, which mean that + // instants before the page start are negative. + // + // So, to allow represing past instants using a `Duration` (that can't represent negative + // values), the duration will be increased by 2^63 seconds. + + let duration = duration_from_f64(now()); + Self(duration + Duration::from_secs(1 << 63)) } #[inline] diff --git a/tests/wasm.rs b/tests/wasm.rs index e858faa..1861ab2 100644 --- a/tests/wasm.rs +++ b/tests/wasm.rs @@ -55,3 +55,10 @@ fn test_system_time() { .is_err()); } +#[wasm_bindgen_test] +fn test_past() { + let hundred_sec = Duration::from_secs(100); + let hundred_seconds_ago = Instant::now() - hundred_sec; + assert!(hundred_seconds_ago.elapsed() >= hundred_sec); +} +