1 #[cfg(all(test, feature = "wasmbind"))]
2 mod test {
3     extern crate chrono;
4     extern crate wasm_bindgen_test;
5 
6     use self::chrono::prelude::*;
7     use self::wasm_bindgen_test::*;
8 
9     #[wasm_bindgen_test]
now()10     fn now() {
11         let utc: DateTime<Utc> = Utc::now();
12         let local: DateTime<Local> = Local::now();
13 
14         // Ensure time set by the test script is correct
15         let now = env!("NOW");
16         let actual = Utc.datetime_from_str(&now, "%s").unwrap();
17         let diff = utc - actual;
18         assert!(
19             diff < chrono::Duration::minutes(5),
20             "expected {} - {} == {} < 5m (env var: {})",
21             utc,
22             actual,
23             diff,
24             now,
25         );
26 
27         let tz = env!("TZ");
28         eprintln!("testing with tz={}", tz);
29 
30         // Ensure offset retrieved when getting local time is correct
31         let expected_offset = match tz {
32             "ACST-9:30" => FixedOffset::east(19 * 30 * 60),
33             "Asia/Katmandu" => FixedOffset::east(23 * 15 * 60), // No DST thankfully
34             "EDT" | "EST4" | "-0400" => FixedOffset::east(-4 * 60 * 60),
35             "EST" | "-0500" => FixedOffset::east(-5 * 60 * 60),
36             "UTC0" | "+0000" => FixedOffset::east(0),
37             tz => panic!("unexpected TZ {}", tz),
38         };
39         assert_eq!(
40             &expected_offset,
41             local.offset(),
42             "expected: {:?} local: {:?}",
43             expected_offset,
44             local.offset(),
45         );
46     }
47 
48     #[wasm_bindgen_test]
from_is_exact()49     fn from_is_exact() {
50         let now = js_sys::Date::new_0();
51 
52         let dt = DateTime::<Utc>::from(now.clone());
53 
54         assert_eq!(now.get_time() as i64, dt.timestamp_millis());
55     }
56 
57     #[wasm_bindgen_test]
local_from_local_datetime()58     fn local_from_local_datetime() {
59         let now = Local::now();
60         let ndt = now.naive_local();
61         let res = match Local.from_local_datetime(&ndt).single() {
62             Some(v) => v,
63             None => panic! {"Required for test!"},
64         };
65         assert_eq!(now, res);
66     }
67 }
68