wareki_conv/
lib.rs

1#![doc(html_root_url = "https://docs.rs/wareki-conv/0.1.0")]
2//! Converts Wareki (JIS X 0301) based date into ISO 8601 based one
3
4#![feature(test)]
5extern crate test;
6
7pub mod conv;
8
9/// tests
10#[cfg(test)]
11mod tests {
12    use super::conv::*;
13    use chrono::prelude::*;
14    use std::collections::HashMap;
15    use test::Bencher;
16
17    #[test]
18    fn assert() {
19        fn test_assert(s: &str, ymd: (i32, u32, u32)) {
20            assert_eq!(
21                convert(s).unwrap(),
22                Some(Utc.with_ymd_and_hms(ymd.0, ymd.1, ymd.2, 0, 0, 0).unwrap())
23            )
24        }
25
26        // DateType::JisX0301Basic
27        test_assert("01.02.03", (2019, 2, 3));
28        test_assert("1.2.3", (2019, 2, 3));
29        test_assert("10.02.03", (2028, 2, 3));
30        test_assert("06.2.3", (2024, 2, 3));
31        test_assert("06.02.03", (2024, 2, 3));
32
33        // DateType::JisX0301Extended
34        test_assert("R01.02.03", (2019, 2, 3));
35        test_assert("R10.2.3", (2028, 2, 3));
36        test_assert("M01.02.03", (1868, 2, 3));
37        test_assert("M45.2.3", (1912, 2, 3));
38        test_assert("T01.02.03", (1912, 2, 3));
39        test_assert("S01.2.3", (1926, 2, 3));
40        test_assert("H01.02.03", (1989, 2, 3));
41        test_assert("H01.02.03", (1989, 2, 3));
42
43        // DateType::JisX0301ExtendedWithKanji
44        test_assert("令01.02.03", (2019, 2, 3));
45        test_assert("令1.2.3", (2019, 2, 3));
46        test_assert("明01.02.03", (1868, 2, 3));
47        test_assert("大01.2.3", (1912, 2, 3));
48        test_assert("昭01.02.03", (1926, 2, 3));
49        test_assert("平01.2.3", (1989, 2, 3));
50        test_assert("平01.02.03", (1989, 2, 3));
51
52        // DateType::SeparatedWithKanji
53        test_assert("令和1年2月3日", (2019, 2, 3));
54        test_assert("明治1年2月3日", (1868, 2, 3));
55        test_assert("大正1年2月3日", (1912, 2, 3));
56        test_assert("昭和1年2月3日", (1926, 2, 3));
57        test_assert("平成1年2月3日", (1989, 2, 3));
58        test_assert("平成1年2月3日", (1989, 2, 3));
59        test_assert("平成元年2月3日", (1989, 2, 3));
60    }
61
62    #[test]
63    #[ignore]
64    // cargo test -- --ignored --show-output
65    fn _test_perf() {
66        let test_count = 100;
67        let map = HashMap::from([
68            (0, 'M'),
69            (1, 'T'),
70            (2, 'S'),
71            (3, 'H'),
72            (4, 'R'),
73            (5, '明'),
74            (6, '大'),
75            (7, '昭'),
76            (8, '平'),
77            (9, '令'),
78        ]);
79
80        let start = Utc::now();
81        println!("Start: {}", start);
82
83        (0..test_count).into_iter().for_each(|i| {
84            convert(&format!("{}{}.1.2", map.get(&(i % 10)).unwrap(), i % 20)).unwrap();
85        });
86
87        let end = Utc::now();
88        let dur = end.signed_duration_since(start);
89        println!("End: {}", end);
90        println!(
91            "Duration per sec: {}",
92            test_count as f64 / (dur.num_milliseconds() as f64 / 1000.0)
93        );
94    }
95    #[bench]
96    fn bench_convert(b: &mut Bencher) {
97        let test_count = 100;
98        let map = HashMap::from([
99            (0, 'M'),
100            (1, 'T'),
101            (2, 'S'),
102            (3, 'H'),
103            (4, 'R'),
104            (5, '明'),
105            (6, '大'),
106            (7, '昭'),
107            (8, '平'),
108            (9, '令'),
109        ]);
110        b.iter(|| {
111            (0..test_count).into_iter().for_each(|i| {
112                convert(&format!("{}{}.1.2", map.get(&(i % 10)).unwrap(), i % 20)).unwrap();
113            })
114        });
115    }
116}