Skip to content

Commit

Permalink
Fix wasm build
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Sep 11, 2022
1 parent c76a799 commit 42236f0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
14 changes: 9 additions & 5 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,11 @@ impl<Tz: TimeZone> From<DateTime<Tz>> for SystemTime {
not(any(target_os = "emscripten", target_os = "wasi"))
)))
)]
impl From<js_sys::Date> for DateTime<Utc> {
fn from(date: js_sys::Date) -> DateTime<Utc> {
DateTime::<Utc>::from(&date)
impl std::convert::TryFrom<js_sys::Date> for DateTime<Utc> {
type Error = ChronoError;

fn try_from(date: js_sys::Date) -> Result<Self, Self::Error> {
DateTime::<Utc>::try_from(&date)
}
}

Expand All @@ -1137,8 +1139,10 @@ impl From<js_sys::Date> for DateTime<Utc> {
not(any(target_os = "emscripten", target_os = "wasi"))
)))
)]
impl From<&js_sys::Date> for DateTime<Utc> {
fn from(date: &js_sys::Date) -> DateTime<Utc> {
impl std::convert::TryFrom<&js_sys::Date> for DateTime<Utc> {
type Error = ChronoError;

fn try_from(date: &js_sys::Date) -> Result<Self, Self::Error> {
Utc.timestamp_millis(date.get_time() as i64)
}
}
Expand Down
21 changes: 12 additions & 9 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ impl Local {
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
))]
pub fn now() -> DateTime<Local> {
pub fn now() -> Result<DateTime<Local>, ChronoError> {
use super::Utc;
let now: DateTime<Utc> = super::Utc::now();
let now: DateTime<Utc> = super::Utc::now()?;

// Workaround missing timezone logic in `time` crate
let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60);
DateTime::from_utc(now.naive_utc(), offset)
let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60)?;
Ok(DateTime::from_utc(now.naive_utc(), offset))
}
}

Expand Down Expand Up @@ -131,12 +131,15 @@ impl TimeZone for Local {
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
))]
fn from_local_datetime(&self, local: &NaiveDateTime) -> Result<DateTime<Local>, ChronoError> {
fn from_local_datetime(
&self,
local: &NaiveDateTime,
) -> Result<LocalResult<DateTime<Local>>, ChronoError> {
let mut local = local.clone();
// Get the offset from the js runtime
let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60);
let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60)?;
local -= crate::TimeDelta::seconds(offset.local_minus_utc() as i64);
Ok(DateTime::from_utc(local, offset))
Ok(LocalResult::Single(DateTime::from_utc(local, offset)))
}

#[cfg(not(all(
Expand All @@ -163,8 +166,8 @@ impl TimeZone for Local {
))]
fn from_utc_datetime(&self, utc: &NaiveDateTime) -> Result<DateTime<Local>, ChronoError> {
// Get the offset from the js runtime
let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60);
DateTime::from_utc(*utc, offset)
let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60)?;
Ok(DateTime::from_utc(*utc, offset))
}

#[cfg(not(all(
Expand Down
4 changes: 3 additions & 1 deletion src/offset/utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ impl Utc {
not(any(target_os = "emscripten", target_os = "wasi"))
))]
pub fn now() -> Result<DateTime<Utc>, ChronoError> {
use std::convert::TryFrom;

let now = js_sys::Date::new_0();
Ok(DateTime::<Utc>::from(now))
DateTime::<Utc>::try_from(now)
}
}

Expand Down
27 changes: 13 additions & 14 deletions tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
not(any(target_os = "emscripten", target_os = "wasi"))
))]

use self::chrono::prelude::*;
use self::wasm_bindgen_test::*;
use std::convert::TryFrom;

use chrono::prelude::*;
use wasm_bindgen_test::*;

#[wasm_bindgen_test]
fn now() {
let utc: DateTime<Utc> = Utc::now();
let utc: DateTime<Utc> = Utc::now().unwrap();
let local: DateTime<Local> = Local::now().unwrap();

// Ensure time set by the test script is correct
let now = env!("NOW");
let actual = Utc.datetime_from_str(&now, "%s").unwrap();
let diff = utc - actual;
assert!(
diff < chrono::Duration::minutes(5),
diff < chrono::TimeDelta::minutes(5),
"expected {} - {} == {} < 5m (env var: {})",
utc,
actual,
Expand All @@ -30,11 +32,11 @@ fn now() {

// Ensure offset retrieved when getting local time is correct
let expected_offset = match tz {
"ACST-9:30" => FixedOffset::east(19 * 30 * 60),
"Asia/Katmandu" => FixedOffset::east(23 * 15 * 60), // No DST thankfully
"EDT" | "EST4" | "-0400" => FixedOffset::east(-4 * 60 * 60),
"EST" | "-0500" => FixedOffset::east(-5 * 60 * 60),
"UTC0" | "+0000" => FixedOffset::east(0),
"ACST-9:30" => FixedOffset::east(19 * 30 * 60).unwrap(),
"Asia/Katmandu" => FixedOffset::east(23 * 15 * 60).unwrap(), // No DST thankfully
"EDT" | "EST4" | "-0400" => FixedOffset::east(-4 * 60 * 60).unwrap(),
"EST" | "-0500" => FixedOffset::east(-5 * 60 * 60).unwrap(),
"UTC0" | "+0000" => FixedOffset::east(0).unwrap(),
tz => panic!("unexpected TZ {}", tz),
};
assert_eq!(
Expand All @@ -50,7 +52,7 @@ fn now() {
fn from_is_exact() {
let now = js_sys::Date::new_0();

let dt = DateTime::<Utc>::from(now.clone());
let dt = DateTime::<Utc>::try_from(now.clone()).unwrap();

assert_eq!(now.get_time() as i64, dt.timestamp_millis());
}
Expand All @@ -59,10 +61,7 @@ fn from_is_exact() {
fn local_from_local_datetime() {
let now = Local::now().unwrap();
let ndt = now.naive_local();
let res = match Local.from_local_datetime(&ndt).single() {
Some(v) => v,
None => panic! {"Required for test!"},
};
let res = Local.from_local_datetime(&ndt).unwrap().single().unwrap();
assert_eq!(now, res);
}

Expand Down

0 comments on commit 42236f0

Please sign in to comment.