summaryrefslogtreecommitdiffstats
path: root/vendor/chrono/tests/wasm.rs
blob: f003d4db9d97c1753ba1509fab05f3946000a231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#![cfg(all(
    target_arch = "wasm32",
    feature = "wasmbind",
    not(any(target_os = "emscripten", target_os = "wasi"))
))]

use self::chrono::prelude::*;
use self::wasm_bindgen_test::*;

#[wasm_bindgen_test]
fn now() {
    let utc: DateTime<Utc> = Utc::now();
    let local: DateTime<Local> = Local::now();

    // Ensure time set by the test script is correct
    let now = env!("NOW");
    let actual = Utc.datetime_from_str(&now, "%s").unwrap();
    let diff = utc - actual;
    assert!(
        diff < chrono::Duration::minutes(5),
        "expected {} - {} == {} < 5m (env var: {})",
        utc,
        actual,
        diff,
        now,
    );

    let tz = env!("TZ");
    eprintln!("testing with tz={}", tz);

    // Ensure offset retrieved when getting local time is correct
    let expected_offset = match tz {
        "ACST-9:30" => FixedOffset::east_opt(19 * 30 * 60).unwrap(),
        "Asia/Katmandu" => FixedOffset::east_opt(23 * 15 * 60).unwrap(), // No DST thankfully
        "EDT" | "EST4" | "-0400" => FixedOffset::east_opt(-4 * 60 * 60).unwrap(),
        "EST" | "-0500" => FixedOffset::east_opt(-5 * 60 * 60).unwrap(),
        "UTC0" | "+0000" => FixedOffset::east_opt(0).unwrap(),
        tz => panic!("unexpected TZ {}", tz),
    };
    assert_eq!(
        &expected_offset,
        local.offset(),
        "expected: {:?} local: {:?}",
        expected_offset,
        local.offset(),
    );
}

#[wasm_bindgen_test]
fn from_is_exact() {
    let now = js_sys::Date::new_0();

    let dt = DateTime::<Utc>::from(now.clone());

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

#[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);
}

#[wasm_bindgen_test]
fn convert_all_parts_with_milliseconds() {
    let time: DateTime<Utc> = "2020-12-01T03:01:55.974Z".parse().unwrap();
    let js_date = js_sys::Date::from(time);

    assert_eq!(js_date.get_utc_full_year(), 2020);
    assert_eq!(js_date.get_utc_month(), 12);
    assert_eq!(js_date.get_utc_date(), 1);
    assert_eq!(js_date.get_utc_hours(), 3);
    assert_eq!(js_date.get_utc_minutes(), 1);
    assert_eq!(js_date.get_utc_seconds(), 55);
    assert_eq!(js_date.get_utc_milliseconds(), 974);
}