dax: read the browser clock for NOW/TODAY on wasm (#2228)

This commit is contained in:
alosslessdev
2026-11-05 06:59:49 -05:00
parent 292f0a3447
commit c37fa6a799
3 changed files with 22 additions and 4 deletions
Generated
+1
View File
@@ -2078,6 +2078,7 @@ name = "gridcraft-dax"
version = "0.1.0"
dependencies = [
"chrono",
"js-sys",
"quick-xml 0.41.0",
"serde",
"serde_json",
+5
View File
@@ -21,5 +21,10 @@ thiserror = { workspace = true }
# same with the same workspace dependency).
chrono = { workspace = true }
[target.'cfg(target_arch = "wasm32")'.dependencies]
# The browser clock for NOW/TODAY under wasm, the same dependency `crates/core` uses: the standard
# library clock traps there, and `eval/support.rs` would otherwise freeze at 1970.
js-sys = "0.3"
[dev-dependencies]
serde_json = { workspace = true }
+16 -4
View File
@@ -277,8 +277,20 @@ pub(crate) fn today_serial() -> f64 {
#[cfg(target_arch = "wasm32")]
pub(crate) fn today_serial() -> f64 {
// The web build has no clock; TODAY/NOW return a fixed serial instead of trapping.
25569.0
now_serial().floor()
}
/// The browser clock as an Excel serial, the wasm arm of [`now_serial`]/[`utc_now_serial`].
///
/// The standard library's clock traps on wasm, so the epoch comes from `js_sys::Date`, the same
/// source `crates/core/src/date.rs` uses. `Date::now()` is UTC milliseconds; for the local serial
/// the browser's time-zone offset is subtracted (it counts minutes *west* of UTC), and only
/// `TODAY`/`UTCTODAY` floor the result.
#[cfg(target_arch = "wasm32")]
fn wasm_now_serial(local: bool) -> f64 {
const MILLISECONDS_PER_DAY: f64 = 86_400_000.0;
let offset_minutes = if local { -js_sys::Date::new_0().get_timezone_offset() } else { 0.0 };
25569.0 + js_sys::Date::now() / MILLISECONDS_PER_DAY + offset_minutes / 1_440.0
}
/// The local date and time as an Excel serial (NOW).
@@ -298,7 +310,7 @@ pub(crate) fn now_serial() -> f64 {
#[cfg(target_arch = "wasm32")]
pub(crate) fn now_serial() -> f64 {
25569.0
wasm_now_serial(true)
}
/// The UTC date and time as an Excel serial (UTCNOW).
@@ -318,7 +330,7 @@ pub(crate) fn utc_now_serial() -> f64 {
#[cfg(target_arch = "wasm32")]
pub(crate) fn utc_now_serial() -> f64 {
25569.0
wasm_now_serial(false)
}
/// The UTC date at midnight as an Excel serial (UTCTODAY).