diff --git a/Cargo.toml b/Cargo.toml index e80568ca..25d7d002 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ zip = {version = "0.6.4 ", default-features = false, features = ["deflate"]} regex = "1.7.3" lazy_static = "1.4.0" polars-error= {version = "0.32.1", default-features = false, features = [], optional = true } +js-sys = {version = "0.3.64", optional = true} [dev-dependencies] pretty_assertions = "1.3.0" @@ -37,6 +38,9 @@ chrono = ["dep:chrono"] # easier to write. polars = ["dep:polars-error"] +# `wasm`: todo. +wasm = ["js-sys"] + # `test-resave`: Developer only testing feature. test-resave = [] diff --git a/src/datetime.rs b/src/datetime.rs index 9c7a4709..93b95978 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -6,6 +6,12 @@ #![warn(missing_docs)] use regex::Regex; + +#[cfg(not(all( + feature = "wasm", + target_arch = "wasm32", + not(any(target_os = "emscripten", target_os = "wasi")) +)))] use std::time::SystemTime; #[cfg(feature = "chrono")] @@ -1175,11 +1181,32 @@ impl ExcelDateTime { // Get the current UTC time. This is used to set some Excel metadata // timestamps. pub(crate) fn utc_now() -> String { + let timestamp = Self::system_now(); + Self::unix_time_to_rfc3339(timestamp) + } + + // Get the current time from the system time. + #[cfg(not(all( + feature = "wasm", + target_arch = "wasm32", + not(any(target_os = "emscripten", target_os = "wasi")) + )))] + fn system_now() -> u64 { let timestamp = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .expect("SystemTime::now() is before Unix epoch"); + timestamp.as_secs() + } - Self::unix_time_to_rfc3339(timestamp.as_secs()) + // Get the current time on Wasm/JS systems. + #[cfg(all( + feature = "wasm", + target_arch = "wasm32", + not(any(target_os = "emscripten", target_os = "wasi")) + ))] + fn system_now() -> u64 { + let timestamp = js_sys::Date::now(); + timestamp as u64 } // Convert to UTC date in RFC 3339 format. This is used in custom diff --git a/src/packager.rs b/src/packager.rs index 39184381..c238210f 100644 --- a/src/packager.rs +++ b/src/packager.rs @@ -42,6 +42,8 @@ use std::collections::HashSet; use std::io::{Seek, Write}; + +#[cfg(not(feature = "wasm"))] use std::thread; use zip::write::FileOptions; @@ -110,6 +112,7 @@ impl Packager { // Assemble, but don't write, the worksheet files in parallel. These are // generally the largest files and the threading can help performance if // there are multiple large worksheets. + #[cfg(not(feature = "wasm"))] thread::scope(|scope| { for worksheet in &mut workbook.worksheets { scope.spawn(|| { @@ -118,6 +121,12 @@ impl Packager { } }); + #[cfg(feature = "wasm")] + // For wasm targets don't use threading. + for worksheet in &mut workbook.worksheets { + worksheet.assemble_xml_file(); + } + // Write the worksheet file and and associated rel files. for (index, worksheet) in workbook.worksheets.iter_mut().enumerate() { self.write_worksheet_file(worksheet, index + 1)?;