diff --git a/Cargo.lock b/Cargo.lock index 3b92e7fe..2c3ea457 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -952,6 +952,7 @@ dependencies = [ "deckcraft-text", "i_overlay", "image", + "js-sys", "log", "serde", "serde_json", diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 70479e44..9493ddba 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -28,3 +28,7 @@ image = { workspace = true } [dev-dependencies] deckcraft-anim = { workspace = true } + +# The browser clock for comment timestamps (wasm has no std::time::SystemTime). +[target.'cfg(target_arch = "wasm32")'.dependencies] +js-sys = "0.3" diff --git a/crates/engine/src/cmd/review.rs b/crates/engine/src/cmd/review.rs index c3475aa5..c8370688 100644 --- a/crates/engine/src/cmd/review.rs +++ b/crates/engine/src/cmd/review.rs @@ -61,20 +61,29 @@ pub fn specs() -> Vec { ] } -fn now() -> String { +/// Seconds since the Unix epoch: the system clock, or the browser's on wasm (which has no `SystemTime`). +fn now_secs() -> u64 { #[cfg(not(target_arch = "wasm32"))] { - let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0); - let (y, m, d) = super::design::civil((secs / 86_400) as i64); - let t = secs % 86_400; - format!("{y:04}-{m:02}-{d:02}T{:02}:{:02}:{:02}Z", t / 3600, t / 60 % 60, t % 60) + std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0) } #[cfg(target_arch = "wasm32")] { - String::new() + (js_sys::Date::now() / 1000.0).max(0.0) as u64 } } +/// A UTC timestamp as saved on comments: `2026-10-10T13:10:35Z`. +fn iso_utc(secs: u64) -> String { + let (y, m, d) = super::design::civil((secs / 86_400) as i64); + let t = secs % 86_400; + format!("{y:04}-{m:02}-{d:02}T{:02}:{:02}:{:02}Z", t / 3600, t / 60 % 60, t % 60) +} + +fn now() -> String { + iso_utc(now_secs()) +} + fn add(s: &mut Session, p: &Value) -> Result { let text = str_param(p, "text").ok_or_else(|| bad("comment.add", "missing `text`"))?.to_string(); let author = s.prefs.author.clone(); @@ -330,6 +339,7 @@ fn custom_show(s: &mut Session, p: &Value) -> Result { #[cfg(test)] mod tests { + use super::*; use serde_json::json; use crate::Session; @@ -376,4 +386,16 @@ mod tests { let here = s.execute("comment.next", &json!({})).unwrap(); assert_eq!(here["text"], "on first"); } + + #[test] + fn timestamps_are_iso_utc() { + assert_eq!(iso_utc(0), "1970-01-01T00:00:00Z"); + assert_eq!(iso_utc(1_791_637_835), "2026-10-10T13:10:35Z"); + } + + #[test] + fn the_clock_is_not_empty() { + // On wasm this used to return an empty string, so browser comments were saved undated. + assert!(now().starts_with("20"), "{}", now()); + } }