1 //! Run this test with:
2 //! `env TZ="$(date +%z)" NOW="$(date +%s)" wasm-pack test --node -- --features wasmbind`
3 //!
4 //! The `TZ` and `NOW` variables are used to compare the results inside the WASM environment with
5 //! the host system.
6 //! The check will fail if the local timezone does not match one of the timezones defined below.
7 
8 #![cfg(all(
9     target_arch = "wasm32",
10     feature = "wasmbind",
11     feature = "clock",
12     not(any(target_os = "emscripten", target_os = "wasi"))
13 ))]
14 
15 use chrono::prelude::*;
16 use wasm_bindgen_test::*;
17 
18 #[wasm_bindgen_test]
now()19 fn now() {
20     let utc: DateTime<Utc> = Utc::now();
21     let local: DateTime<Local> = Local::now();
22 
23     // Ensure time set by the test script is correct
24     let now = env!("NOW");
25     let actual = NaiveDateTime::parse_from_str(&now, "%s").unwrap().and_utc();
26     let diff = utc - actual;
27     assert!(
28         diff < chrono::TimeDelta::minutes(5),
29         "expected {} - {} == {} < 5m (env var: {})",
30         utc,
31         actual,
32         diff,
33         now,
34     );
35 
36     let tz = env!("TZ");
37     eprintln!("testing with tz={}", tz);
38 
39     // Ensure offset retrieved when getting local time is correct
40     let expected_offset = match tz {
41         "ACST-9:30" => FixedOffset::east_opt(19 * 30 * 60).unwrap(),
42         "Asia/Katmandu" => FixedOffset::east_opt(23 * 15 * 60).unwrap(), // No DST thankfully
43         "EDT" | "EST4" | "-0400" => FixedOffset::east_opt(-4 * 60 * 60).unwrap(),
44         "EST" | "-0500" => FixedOffset::east_opt(-5 * 60 * 60).unwrap(),
45         "UTC0" | "+0000" => FixedOffset::east_opt(0).unwrap(),
46         tz => panic!("unexpected TZ {}", tz),
47     };
48     assert_eq!(
49         &expected_offset,
50         local.offset(),
51         "expected: {:?} local: {:?}",
52         expected_offset,
53         local.offset(),
54     );
55 }
56 
57 #[wasm_bindgen_test]
from_is_exact()58 fn from_is_exact() {
59     let now = js_sys::Date::new_0();
60 
61     let dt = DateTime::<Utc>::from(now.clone());
62 
63     assert_eq!(now.get_time() as i64, dt.timestamp_millis());
64 }
65 
66 #[wasm_bindgen_test]
local_from_local_datetime()67 fn local_from_local_datetime() {
68     let now = Local::now();
69     let ndt = now.naive_local();
70     let res = match Local.from_local_datetime(&ndt).single() {
71         Some(v) => v,
72         None => panic! {"Required for test!"},
73     };
74     assert_eq!(now, res);
75 }
76 
77 #[wasm_bindgen_test]
convert_all_parts_with_milliseconds()78 fn convert_all_parts_with_milliseconds() {
79     let time: DateTime<Utc> = "2020-12-01T03:01:55.974Z".parse().unwrap();
80     let js_date = js_sys::Date::from(time);
81 
82     assert_eq!(js_date.get_utc_full_year(), 2020);
83     assert_eq!(js_date.get_utc_month(), 11); // months are numbered 0..=11
84     assert_eq!(js_date.get_utc_date(), 1);
85     assert_eq!(js_date.get_utc_hours(), 3);
86     assert_eq!(js_date.get_utc_minutes(), 1);
87     assert_eq!(js_date.get_utc_seconds(), 55);
88     assert_eq!(js_date.get_utc_milliseconds(), 974);
89 }
90