diff --git a/Cargo.toml b/Cargo.toml index 021fa1c..f3c469b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,9 @@ name = "crosstime" version = "0.1.1" edition = "2021" +[features] +local-offset = [ "time/local-offset" ] + [dependencies] time = "0.3" diff --git a/src/lib.rs b/src/lib.rs index c52cb88..96dc7ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,3 +11,18 @@ pub fn now_utc() -> OffsetDateTime { pub fn now_utc() -> OffsetDateTime { return OffsetDateTime::now_utc(); } + +#[cfg(feature = "local-offset")] +#[cfg(target_arch = "wasm32")] +pub fn current_local_offset() -> Result { + // 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::current_local_offset() +}