Debian Codegraph @

debian/rust-vendor/icu_datetime/tests/hijri_iran.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use std::collections::HashMap;
6 use std::sync::LazyLock;
7
8 use icu_calendar::Date;
9 use icu_calendar::cal::{Hijri, hijri};
10
11 #[derive(Clone, Copy, Debug)]
12 struct IranSighting;
13
14 impl icu_calendar::cal::scaffold::UnstableSealed for IranSighting {}
15 impl hijri::Rules for IranSighting {
16 fn year(&self, extended_year: i32) -> hijri::HijriYear {
17 QAMARI.get(&extended_year).copied().unwrap_or_else(|| {
18 hijri::TabularAlgorithm::new(
19 hijri::TabularAlgorithmLeapYears::TypeII,
20 hijri::TabularAlgorithmEpoch::Friday,
21 )
22 .year(extended_year)
23 })
24 }
25
26 type DateCompatibilityError = core::convert::Infallible;
27
28 fn check_date_compatibility(&self, &Self: &Self) -> Result<(), Self::DateCompatibilityError> {
29 Ok(())
30 }
31 }
32
33 static QAMARI: LazyLock<HashMap<i32, hijri::HijriYear>> = LazyLock::new(|| {
34 let response = ureq::get(
35 "https://raw.githubusercontent.com/roozbehp/qamari/575d275ef169c0a18012506d473ba1008a1c10d0/consolidated.txt",
36 )
37 .call()
38 .unwrap();
39
40 use std::io::{BufRead, BufReader};
41
42 let mut parsed: HashMap<i32, HashMap<u8, _>> = HashMap::new();
43
44 for line in BufReader::new(response.into_body().into_reader())
45 .lines()
46 .map_while(Result::ok)
47 {
48 let line = line
49 .split('#')
50 .next()
51 .unwrap()
52 .split('*')
53 .next_back()
54 .unwrap()
55 .trim();
56
57 let (ym, greg) = line.split_once(' ').unwrap();
58
59 let mut greg = greg.split('-');
60 let start = Date::try_new_gregorian(
61 greg.next().unwrap().parse().unwrap(),
62 greg.next().unwrap().parse().unwrap(),
63 greg.next().unwrap().parse().unwrap(),
64 )
65 .unwrap()
66 .to_rata_die();
67
68 let (y, m) = ym.split_once('/').unwrap();
69
70 parsed
71 .entry(y.parse().unwrap())
72 .or_default()
73 .insert(m.parse().unwrap(), start);
74 }
75
76 let mut data = HashMap::new();
77
78 for (&y, months) in &parsed {
79 let Some(next) = parsed.get(&(y + 1)) else {
80 continue;
81 };
82
83 data.insert(
84 y,
85 hijri::HijriYear::try_new(
86 y,
87 core::array::from_fn(|i| {
88 if i == 12 {
89 next[&1]
90 } else {
91 months[&(i as u8 + 1)]
92 }
93 }),
94 )
95 .unwrap(),
96 );
97 }
98
99 data
100 });
101
102 // Use the same display names as for TabularAlgorithm
103 impl icu_datetime::scaffold::UnstableSealed for IranSighting {}
104 impl icu_datetime::scaffold::FormattableHijriRules for IranSighting {
105 type MonthNamesV1 =
106 <hijri::TabularAlgorithm as icu_datetime::scaffold::FormattableHijriRules>::MonthNamesV1;
107 type YearNamesV1 =
108 <hijri::TabularAlgorithm as icu_datetime::scaffold::FormattableHijriRules>::YearNamesV1;
109 type SkeletaV1 =
110 <hijri::TabularAlgorithm as icu_datetime::scaffold::FormattableHijriRules>::SkeletaV1;
111 type RangeSkeletaV1 =
112 <hijri::TabularAlgorithm as icu_datetime::scaffold::FormattableHijriRules>::RangeSkeletaV1;
113 }
114
115 #[test]
116 #[ignore]
117 fn test_conversion() {
118 for ((y, m, d), rd) in [
119 (
120 (1412, 9, 12),
121 Date::try_new_gregorian(1992, 3, 17).unwrap().to_rata_die(),
122 ),
123 (
124 (1416, 10, 6),
125 Date::try_new_gregorian(1996, 2, 25).unwrap().to_rata_die(),
126 ),
127 ] {
128 let date = Date::try_new_hijri_with_calendar(y, m, d, Hijri(IranSighting)).unwrap();
129 let date_rd = Date::from_rata_die(rd, Hijri(IranSighting));
130
131 assert_eq!(date, date_rd, "{:?}", (y, m, d));
132 assert_eq!(date.to_rata_die(), rd, "{:?}", (y, m, d));
133 }
134 }
135
136 #[test]
137 #[ignore]
138 fn test_format() {
139 use icu_datetime::{FixedCalendarDateTimeFormatter, fieldsets};
140 use icu_locale_core::locale;
141
142 let formatter =
143 FixedCalendarDateTimeFormatter::try_new(locale!("fa").into(), fieldsets::YMD::long())
144 .unwrap();
145
146 assert_eq!(
147 formatter
148 .format(
149 &Date::try_new_gregorian(2022, 10, 12)
150 .unwrap()
151 .to_calendar(Hijri(IranSighting))
152 )
153 .to_string(),
154 "۱۵ ربیع\u{200c}الاول ۱۴۴۴ ه\u{200d}.ق."
155 );
156 }