Skip to content

Commit 5d38fae

Browse files
committed
fix local timezone, add tests
1 parent 8bfaca9 commit 5d38fae

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ env:
2121
global:
2222
- LD_LIBRARY_PATH: /usr/local/lib
2323
- CLIPPY: n
24+
install:
25+
- curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
2426
script: ./ci/travis.sh
2527
notifications:
2628
email: false

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ serde = { version = "1", optional = true }
3939
wasm-bindgen = { version = "0.2" }
4040
js-sys = "0.3" # contains FFI bindings for the JS Date API
4141

42-
43-
4442
[dev-dependencies]
4543
serde_json = { version = "1" }
4644
serde_derive = { version = "1" }
4745
bincode = { version = "0.8.0" }
4846
num-iter = { version = "0.1.35", default-features = false }
4947
doc-comment = "0.3"
5048

49+
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
50+
wasm-bindgen-test = "0.2"
51+
5152
[package.metadata.docs.rs]
5253
all-features = true
5354

ci/travis.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ build_and_test() {
4848
channel build -v --no-default-features --features serde,rustc-serialize
4949
TZ=Asia/Katmandu channel test -v --no-default-features --features serde,rustc-serialize --lib
5050

51+
# wasm tests
52+
touch tests/wasm.rs # ensure rebuild happens so TZ / NOW take effect
53+
TZ=ACST-9:30 NOW=$(date +%s) wasm-pack test --node
54+
touch tests/wasm.rs
55+
TZ=EST4 NOW=$(date +%s) wasm-pack test --node
56+
touch tests/wasm.rs
57+
TZ=UTC0 NOW=$(date +%s) wasm-pack test --node
58+
touch tests/wasm.rs
59+
TZ=Asia/Katmandu NOW=$(date +%s) wasm-pack test --node
60+
5161
if [[ "$CHANNEL" == stable ]]; then
5262
if [[ -n "$TRAVIS" ]] ; then
5363
check_readme

src/offset/local.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ impl Local {
9797
pub fn now() -> DateTime<Local> {
9898
use super::Utc;
9999
let now: DateTime<Utc> = super::Utc::now();
100-
now.with_timezone(&Local)
100+
101+
// Workaround missing timezone logic in `time` crate
102+
let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60);
103+
DateTime::from_utc(now.naive_utc(), offset)
101104
}
102105
}
103106

tests/wasm.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
extern crate chrono;
2+
extern crate wasm_bindgen_test;
3+
4+
use chrono::prelude::*;
5+
use wasm_bindgen_test::*;
6+
7+
use std::env;
8+
9+
#[wasm_bindgen_test]
10+
fn now() {
11+
let utc: DateTime<Utc> = Utc::now();
12+
let local: DateTime<Local> = Local::now();
13+
14+
// Ensure time fetched is correct
15+
let actual = Utc.datetime_from_str(env!("NOW"), "%s").unwrap();
16+
assert!(utc - actual < chrono::Duration::minutes(5));
17+
18+
// Ensure offset retrieved when getting local time is correct
19+
let expected_offset = match env!("TZ") {
20+
"ACST-9:30" => FixedOffset::east(19 * 30 * 60),
21+
"Asia/Katmandu" => FixedOffset::east(23 * 15 * 60), // No DST thankfully
22+
"EST4" => FixedOffset::east(-4 * 60 * 60),
23+
"UTC0" => FixedOffset::east(0),
24+
_ => panic!("unexpected TZ"),
25+
};
26+
assert_eq!(&expected_offset, local.offset());
27+
}

0 commit comments

Comments
 (0)