Debian Codegraph @

debian/rust-vendor/icu_calendar/src/cal/buddhist.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 crate::cal::abstract_gregorian::AbstractGregorian;
6 use crate::error::UnknownEraError;
7 use crate::preferences::CalendarAlgorithm;
8 use crate::{
9 Date, RangeError,
10 cal::abstract_gregorian::{GregorianYears, impl_with_abstract_gregorian},
11 calendar_arithmetic::ArithmeticDate,
12 types,
13 };
14 use tinystr::tinystr;
15
16 #[derive(Copy, Clone, Debug, Default)]
17 /// The [Thai Solar Buddhist Calendar](https://en.wikipedia.org/wiki/Thai_solar_calendar)
18 ///
19 /// The Thai Solar Buddhist Calendar is a variant of the [`Gregorian`](crate::cal::Gregorian) calendar
20 /// created by the Thai government. It is identical to the Gregorian calendar except that is uses
21 /// the Buddhist Era (-543 CE) instead of the Common Era.
22 ///
23 /// This implementation extends proleptically for dates before the calendar's creation
24 /// in 2484 BE (1941 CE).
25 ///
26 /// This corresponds to the `"buddhist"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier).
27 ///
28 /// # Era codes
29 ///
30 /// This calendar uses a single era code `be`, with 1 Buddhist Era being 543 BCE. Dates before this era use negative years.
31 #[allow(clippy::exhaustive_structs)] // this type is stable
32 pub struct Buddhist;
33
34 impl_with_abstract_gregorian!(Buddhist, BuddhistDateInner, BuddhistEra, _x, BuddhistEra);
35
36 #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
37 pub(crate) struct BuddhistEra;
38
39 impl GregorianYears for BuddhistEra {
40 const EXTENDED_YEAR_OFFSET: i32 = -543;
41
42 fn extended_from_era_year(
43 &self,
44 era: Option<&[u8]>,
45 year: i32,
46 ) -> Result<i32, UnknownEraError> {
47 match era {
48 Some(b"be") | None => Ok(year),
49 _ => Err(UnknownEraError),
50 }
51 }
52
53 fn era_year_from_extended(&self, extended_year: i32, _month: u8, _day: u8) -> types::EraYear {
54 types::EraYear {
55 era: tinystr!(16, "be"),
56 era_index: Some(0),
57 year: extended_year,
58 extended_year,
59 ambiguity: types::YearAmbiguity::CenturyRequired,
60 }
61 }
62
63 fn debug_name(&self) -> &'static str {
64 "Buddhist"
65 }
66
67 fn calendar_algorithm(&self) -> Option<CalendarAlgorithm> {
68 Some(CalendarAlgorithm::Buddhist)
69 }
70 }
71
72 impl Date<Buddhist> {
73 /// Construct a new Buddhist [`Date`].
74 ///
75 /// Years are arithmetic, meaning there is a year 0 preceded by negative years, with a
76 /// valid range of `-9999..=9999`.
77 ///
78 /// ```rust
79 /// use icu::calendar::Date;
80 ///
81 /// let date_buddhist = Date::try_new_buddhist(1970, 1, 2)
82 /// .expect("Failed to initialize Buddhist Date instance.");
83 ///
84 /// assert_eq!(date_buddhist.era_year().year, 1970);
85 /// assert_eq!(date_buddhist.month().ordinal, 1);
86 /// assert_eq!(date_buddhist.day_of_month().0, 2);
87 /// ```
88 pub fn try_new_buddhist(year: i32, month: u8, day: u8) -> Result<Date<Buddhist>, RangeError> {
89 ArithmeticDate::from_year_month_day(year, month, day, &AbstractGregorian(BuddhistEra))
90 .map(ArithmeticDate::cast)
91 .map(BuddhistDateInner)
92 .map(|i| Date::from_raw(i, Buddhist))
93 }
94 }
95
96 #[cfg(test)]
97 mod test {
98 use calendrical_calculations::rata_die::RataDie;
99
100 use super::*;
101
102 #[test]
103 fn test_buddhist_roundtrip_near_rd_zero() {
104 for i in -10000..=10000 {
105 let rd = RataDie::new(i);
106 let buddhist = Date::from_rata_die(rd, Buddhist);
107 let result = buddhist.to_rata_die();
108 assert_eq!(rd, result);
109 }
110 }
111
112 #[test]
113 fn test_buddhist_roundtrip_near_epoch() {
114 // Buddhist epoch start RD: -198326
115 for i in -208326..=-188326 {
116 let rd = RataDie::new(i);
117 let buddhist = Date::from_rata_die(rd, Buddhist);
118 let result = buddhist.to_rata_die();
119 assert_eq!(rd, result);
120 }
121 }
122
123 #[test]
124 fn test_buddhist_directionality_near_rd_zero() {
125 for i in -100..=100 {
126 for j in -100..=100 {
127 let buddhist_i = Date::from_rata_die(RataDie::new(i), Buddhist);
128 let buddhist_j = Date::from_rata_die(RataDie::new(j), Buddhist);
129
130 assert_eq!(
131 i.cmp(&j),
132 buddhist_i.cmp(&buddhist_j),
133 "Buddhist directionality inconsistent with directionality for i: {i}, j: {j}"
134 );
135 }
136 }
137 }
138
139 #[test]
140 fn test_buddhist_directionality_near_epoch() {
141 // Buddhist epoch start RD: -198326
142 for i in -198426..=-198226 {
143 for j in -198426..=-198226 {
144 let buddhist_i = Date::from_rata_die(RataDie::new(i), Buddhist);
145 let buddhist_j = Date::from_rata_die(RataDie::new(j), Buddhist);
146
147 assert_eq!(
148 i.cmp(&j),
149 buddhist_i.cmp(&buddhist_j),
150 "Buddhist directionality inconsistent with directionality for i: {i}, j: {j}"
151 );
152 }
153 }
154 }
155
156 #[derive(Debug)]
157 struct TestCase {
158 rd: RataDie,
159 year: i32,
160 month: u8,
161 day: u8,
162 }
163
164 fn check_test_case(case: TestCase) {
165 let date = Date::from_rata_die(case.rd, Buddhist);
166
167 assert_eq!(date.to_rata_die(), case.rd, "{case:?}");
168
169 assert_eq!(date.era_year().year, case.year, "{case:?}");
170 assert_eq!(date.month().ordinal, case.month, "{case:?}");
171 assert_eq!(date.day_of_month().0, case.day, "{case:?}");
172
173 assert_eq!(
174 Date::try_new_buddhist(
175 date.era_year().extended_year,
176 date.month().ordinal,
177 date.day_of_month().0
178 ),
179 Ok(date),
180 "{case:?}"
181 );
182 }
183
184 #[test]
185 fn test_buddhist_cases_near_rd_zero() {
186 let cases = [
187 TestCase {
188 rd: Date::try_new_iso(-100, 2, 15).unwrap().to_rata_die(),
189 year: 443,
190 month: 2,
191 day: 15,
192 },
193 TestCase {
194 rd: Date::try_new_iso(-3, 10, 29).unwrap().to_rata_die(),
195 year: 540,
196 month: 10,
197 day: 29,
198 },
199 TestCase {
200 rd: Date::try_new_iso(0, 12, 31).unwrap().to_rata_die(),
201 year: 543,
202 month: 12,
203 day: 31,
204 },
205 TestCase {
206 rd: Date::try_new_iso(1, 1, 1).unwrap().to_rata_die(),
207 year: 544,
208 month: 1,
209 day: 1,
210 },
211 TestCase {
212 rd: Date::try_new_iso(4, 2, 29).unwrap().to_rata_die(),
213 year: 547,
214 month: 2,
215 day: 29,
216 },
217 ];
218
219 for case in cases {
220 check_test_case(case);
221 }
222 }
223
224 #[test]
225 fn test_buddhist_cases_near_epoch() {
226 // 1 BE = 543 BCE = -542 ISO
227 let cases = [
228 TestCase {
229 rd: Date::try_new_iso(-554, 12, 31).unwrap().to_rata_die(),
230 year: -11,
231 month: 12,
232 day: 31,
233 },
234 TestCase {
235 rd: Date::try_new_iso(-553, 1, 1).unwrap().to_rata_die(),
236 year: -10,
237 month: 1,
238 day: 1,
239 },
240 TestCase {
241 rd: Date::try_new_iso(-544, 8, 31).unwrap().to_rata_die(),
242 year: -1,
243 month: 8,
244 day: 31,
245 },
246 TestCase {
247 rd: Date::try_new_iso(-543, 5, 12).unwrap().to_rata_die(),
248 year: 0,
249 month: 5,
250 day: 12,
251 },
252 TestCase {
253 rd: Date::try_new_iso(-543, 12, 31).unwrap().to_rata_die(),
254 year: 0,
255 month: 12,
256 day: 31,
257 },
258 TestCase {
259 rd: Date::try_new_iso(-542, 1, 1).unwrap().to_rata_die(),
260 year: 1,
261 month: 1,
262 day: 1,
263 },
264 TestCase {
265 rd: Date::try_new_iso(-541, 7, 9).unwrap().to_rata_die(),
266 year: 2,
267 month: 7,
268 day: 9,
269 },
270 ];
271
272 for case in cases {
273 check_test_case(case);
274 }
275 }
276
277 #[test]
278 fn test_constructor_roundtrip() {
279 let rds = crate::tests::get_interesting_rds();
280 for rd in rds {
281 let date = Date::from_rata_die(rd, Buddhist);
282 let reconstructed = Date::try_new_buddhist(
283 date.year().extended_year(),
284 date.month().ordinal,
285 date.day_of_month().0,
286 )
287 .unwrap();
288 assert_eq!(
289 reconstructed.to_rata_die(),
290 rd,
291 "Buddhist failed for RD {:?}",
292 rd
293 );
294 }
295 }
296 }