Compare commits
4 Commits
57c95464c7
...
7a30f61c1b
Author | SHA1 | Date |
---|---|---|
Michael Pfaff | 7a30f61c1b | |
Michael Pfaff | f52096d00b | |
Michael Pfaff | e99f65f98c | |
Michael Pfaff | b52e381367 |
|
@ -1,11 +1,14 @@
|
||||||
[package]
|
[package]
|
||||||
name = "crosstime"
|
name = "crosstime"
|
||||||
version = "0.1.0"
|
version = "0.1.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
local-offset = [ "time/local-offset" ]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
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"
|
||||||
|
|
||||||
|
|
30
src/lib.rs
30
src/lib.rs
|
@ -1,18 +1,28 @@
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "local-offset")]
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
pub fn current_local_offset() -> Result<time::UtcOffset, time::error::IndeterminateOffset> {
|
||||||
|
// get_timezone_offset is in minutes
|
||||||
|
time::UtcOffset::from_whole_seconds((js_sys::Date::new_0().get_timezone_offset() * 60f64) as i32)
|
||||||
|
.map_err(|_| time::error::IndeterminateOffset)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "local-offset")]
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn current_local_offset() -> Result<time::UtcOffset, time::error::IndeterminateOffset> {
|
||||||
|
time::UtcOffset::current_local_offset()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue