Fix wasm32 implementation of current_local_offset

This commit is contained in:
Michael Pfaff 2022-05-30 17:01:37 -04:00
parent 311ff6bc31
commit 530e405103
Signed by: michael
GPG Key ID: CF402C4A012AA9D4
1 changed files with 10 additions and 4 deletions

View File

@ -27,7 +27,11 @@ pub struct Instant(InnerInstant);
impl Instant {
#[cfg(target_arch = "wasm32")]
pub fn now() -> Instant {
let stamp = web_sys::window().expect("cannot get window").performance().expect("cannot get performance").now();
let stamp = web_sys::window()
.expect("cannot get window")
.performance()
.expect("cannot get performance")
.now();
return Instant(Duration::seconds_f64(stamp / 1_000f64));
}
@ -55,9 +59,11 @@ impl std::ops::Sub for Instant {
#[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)
// The value returned by `getTimezoneOffset()` is inverted. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset#negative_values_and_positive_values.
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")]