From 5d38faeb40699cea266424171af431f131b83e52 Mon Sep 17 00:00:00 2001 From: eV Date: Fri, 16 Aug 2019 09:35:56 +0000 Subject: [PATCH] fix local timezone, add tests --- .travis.yml | 2 ++ Cargo.toml | 5 +++-- ci/travis.sh | 10 ++++++++++ src/offset/local.rs | 5 ++++- tests/wasm.rs | 27 +++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/wasm.rs diff --git a/.travis.yml b/.travis.yml index d74f3425e8..71677ae2e7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,8 @@ env: global: - LD_LIBRARY_PATH: /usr/local/lib - CLIPPY: n +install: + - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh script: ./ci/travis.sh notifications: email: false diff --git a/Cargo.toml b/Cargo.toml index 82b8351ba1..66fb839a75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,8 +39,6 @@ serde = { version = "1", optional = true } wasm-bindgen = { version = "0.2" } js-sys = "0.3" # contains FFI bindings for the JS Date API - - [dev-dependencies] serde_json = { version = "1" } serde_derive = { version = "1" } @@ -48,6 +46,9 @@ bincode = { version = "0.8.0" } num-iter = { version = "0.1.35", default-features = false } doc-comment = "0.3" +[target.'cfg(target_arch = "wasm32")'.dev-dependencies] +wasm-bindgen-test = "0.2" + [package.metadata.docs.rs] all-features = true diff --git a/ci/travis.sh b/ci/travis.sh index 4a974b1398..2a932712b9 100755 --- a/ci/travis.sh +++ b/ci/travis.sh @@ -48,6 +48,16 @@ build_and_test() { channel build -v --no-default-features --features serde,rustc-serialize TZ=Asia/Katmandu channel test -v --no-default-features --features serde,rustc-serialize --lib + # wasm tests + touch tests/wasm.rs # ensure rebuild happens so TZ / NOW take effect + TZ=ACST-9:30 NOW=$(date +%s) wasm-pack test --node + touch tests/wasm.rs + TZ=EST4 NOW=$(date +%s) wasm-pack test --node + touch tests/wasm.rs + TZ=UTC0 NOW=$(date +%s) wasm-pack test --node + touch tests/wasm.rs + TZ=Asia/Katmandu NOW=$(date +%s) wasm-pack test --node + if [[ "$CHANNEL" == stable ]]; then if [[ -n "$TRAVIS" ]] ; then check_readme diff --git a/src/offset/local.rs b/src/offset/local.rs index cce193bc90..ae43d4e0e1 100644 --- a/src/offset/local.rs +++ b/src/offset/local.rs @@ -97,7 +97,10 @@ impl Local { pub fn now() -> DateTime { use super::Utc; let now: DateTime = super::Utc::now(); - now.with_timezone(&Local) + + // 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) } } diff --git a/tests/wasm.rs b/tests/wasm.rs new file mode 100644 index 0000000000..21935480e7 --- /dev/null +++ b/tests/wasm.rs @@ -0,0 +1,27 @@ +extern crate chrono; +extern crate wasm_bindgen_test; + +use chrono::prelude::*; +use wasm_bindgen_test::*; + +use std::env; + +#[wasm_bindgen_test] +fn now() { + let utc: DateTime = Utc::now(); + let local: DateTime = Local::now(); + + // Ensure time fetched is correct + let actual = Utc.datetime_from_str(env!("NOW"), "%s").unwrap(); + assert!(utc - actual < chrono::Duration::minutes(5)); + + // Ensure offset retrieved when getting local time is correct + let expected_offset = match env!("TZ") { + "ACST-9:30" => FixedOffset::east(19 * 30 * 60), + "Asia/Katmandu" => FixedOffset::east(23 * 15 * 60), // No DST thankfully + "EST4" => FixedOffset::east(-4 * 60 * 60), + "UTC0" => FixedOffset::east(0), + _ => panic!("unexpected TZ"), + }; + assert_eq!(&expected_offset, local.offset()); +}