Add current_local_offset

This commit is contained in:
Michael Pfaff 2022-05-22 01:15:16 -04:00
parent e99f65f98c
commit f52096d00b
Signed by: michael
GPG Key ID: F1A27427218FCA77
2 changed files with 18 additions and 0 deletions

View File

@ -3,6 +3,9 @@ name = "crosstime"
version = "0.1.1"
edition = "2021"
[features]
local-offset = [ "time/local-offset" ]
[dependencies]
time = "0.3"

View File

@ -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<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()
}