From f52096d00b0ceba2d34cada2265a66e7606ff782 Mon Sep 17 00:00:00 2001 From: Michael Pfaff Date: Sun, 22 May 2022 01:15:16 -0400 Subject: [PATCH] Add current_local_offset --- Cargo.toml | 3 +++ src/lib.rs | 15 +++++++++++++++ 2 files changed, 18 insertions(+) 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() +}