Add current_local_offset
This commit is contained in:
parent
e99f65f98c
commit
f52096d00b
|
@ -3,6 +3,9 @@ name = "crosstime"
|
|||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
local-offset = [ "time/local-offset" ]
|
||||
|
||||
[dependencies]
|
||||
time = "0.3"
|
||||
|
||||
|
|
15
src/lib.rs
15
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<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