Use js_sys instead of wasm_bindgen and re-arrange implementation
This commit is contained in:
parent
57c95464c7
commit
b52e381367
|
@ -7,5 +7,5 @@ edition = "2021"
|
||||||
time = "0.3"
|
time = "0.3"
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
wasm-bindgen = "0.2"
|
js-sys = "0.3.57"
|
||||||
|
|
||||||
|
|
15
src/lib.rs
15
src/lib.rs
|
@ -1,18 +1,13 @@
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
#[wasm_bindgen::prelude::wasm_bindgen(inline_js = r#"
|
pub fn now_utc() -> OffsetDateTime {
|
||||||
export function unix_millis_now() {
|
return OffsetDateTime::from_unix_timestamp_nanos((js_sys::Date::now() * 1_000_000f64) as i128)
|
||||||
return Date.now();
|
.expect("current time outside supported range");
|
||||||
}"#)]
|
|
||||||
extern "C" {
|
|
||||||
fn unix_millis_now() -> f64;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
#[inline(always)]
|
||||||
pub fn now_utc() -> OffsetDateTime {
|
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();
|
return OffsetDateTime::now_utc();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue