Skip to content

Commit

Permalink
Merge pull request #465 from ShadoySV/main
Browse files Browse the repository at this point in the history
Fix Local.from_local_datetime method for wasm
  • Loading branch information
quodlibetor authored Aug 5, 2020
2 parents 8d6d83f + 31e3e16 commit 2761747
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Ken Tossell <[email protected]>
Martin Risell Lilja <[email protected]>
Richard Petrie <[email protected]>
Ryan Lewis <[email protected]>
Sergey V. Shadoy <[email protected]>
Sergey V. Galtsev <[email protected]>
Steve Klabnik <[email protected]>
Tom Gallacher <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Versions with only mechanical changes will be omitted from the following list.
* Add day and week iterators for `NaiveDate` (@gnzlbg & @robyoung)
* Add a `Month` enum (@hhamana)
* Add `locales`. All format functions can now use locales.
* Fix Local.from_local_datetime method for wasm

### Improvements

Expand Down
17 changes: 16 additions & 1 deletion src/offset/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ use oldtime;

use super::fixed::FixedOffset;
use super::{LocalResult, TimeZone};
use naive::{NaiveDate, NaiveDateTime, NaiveTime};
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
use naive::NaiveTime;
use naive::{NaiveDate, NaiveDateTime};
use {Date, DateTime};
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
use {Datelike, Timelike};

/// Converts a `time::Tm` struct into the timezone-aware `DateTime`.
/// This assumes that `time` is working correctly, i.e. any error is fatal.
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
fn tm_to_datetime(mut tm: oldtime::Tm) -> DateTime<Local> {
if tm.tm_sec >= 60 {
tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000;
Expand Down Expand Up @@ -43,6 +47,7 @@ fn tm_to_datetime(mut tm: oldtime::Tm) -> DateTime<Local> {
}

/// Converts a local `NaiveDateTime` to the `time::Timespec`.
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
fn datetime_to_timespec(d: &NaiveDateTime, local: bool) -> oldtime::Timespec {
// well, this exploits an undocumented `Tm::to_timespec` behavior
// to get the exact function we want (either `timegm` or `mktime`).
Expand Down Expand Up @@ -141,6 +146,16 @@ impl TimeZone for Local {
midnight.map(|datetime| Date::from_utc(*local, *datetime.offset()))
}

#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
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);
local -= oldtime::Duration::seconds(offset.local_minus_utc() as i64);
LocalResult::Single(DateTime::from_utc(local, offset))
}

#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
let timespec = datetime_to_timespec(local, true);

Expand Down
11 changes: 11 additions & 0 deletions tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,15 @@ mod test {

assert_eq!(now.get_time() as i64, dt.timestamp_millis());
}

#[wasm_bindgen_test]
fn local_from_local_datetime() {
let now = Local::now();
let ndt = now.naive_local();
let res = match Local.from_local_datetime(&ndt).single() {
Some(v) => v,
None => panic! {"Required for test!"},
};
assert_eq!(now, res);
}
}

0 comments on commit 2761747

Please sign in to comment.