Initial commit

master
Michael Pfaff 11 months ago
commit bc1d171cad
Signed by: michael
GPG Key ID: F1A27427218FCA77
  1. 2
      .gitignore
  2. 11
      Cargo.toml
  3. 18
      src/lib.rs

2
.gitignore vendored

@ -0,0 +1,2 @@
/target
Cargo.lock

@ -0,0 +1,11 @@
[package]
name = "wasm-time"
version = "0.1.0"
edition = "2021"
[dependencies]
time = "0.3"
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"

@ -0,0 +1,18 @@
use time::OffsetDateTime;
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen::prelude::wasm_bindgen(inline_js = r#"
export function unix_millis_now() {
return Date.now();
}"#)]
extern "C" {
fn unix_millis_now() -> f64;
}
pub fn now_utc() -> OffsetDateTime {
#[cfg(target_arch = "wasm32")]
return OffsetDateTime::from_unix_timestamp_nanos((unix_millis_now() * 1_000_000f64) as i128).expect("time outside supported range");
#[cfg(not(target_arch = "wasm32"))]
return OffsetDateTime::now_utc();
}
Loading…
Cancel
Save