Debian Codegraph @

debian/rust-vendor/icu_calendar/src/cal/iso.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::{
6 AbstractGregorian, GregorianYears, impl_with_abstract_gregorian,
7 };
8 use crate::calendar_arithmetic::ArithmeticDate;
9 use crate::error::UnknownEraError;
10 use crate::{Date, RangeError, types};
11 use tinystr::tinystr;
12
13 /// The [ISO-8601 Calendar](https://en.wikipedia.org/wiki/ISO_8601#Dates)
14 ///
15 /// This calendar is identical to the [`Gregorian`](super::Gregorian) calendar,
16 /// except that it uses a single `default` era instead of `bce` and `ce`.
17 ///
18 /// This corresponds to the `"iso8601"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier).
19 ///
20 /// # Era codes
21 ///
22 /// This calendar uses a single era: `default`
23 #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
24 #[allow(clippy::exhaustive_structs)] // this type is stable
25 pub struct Iso;
26
27 impl_with_abstract_gregorian!(Iso, IsoDateInner, IsoEra, _x, IsoEra);
28
29 #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
30 pub(crate) struct IsoEra;
31
32 impl GregorianYears for IsoEra {
33 fn extended_from_era_year(
34 &self,
35 era: Option<&[u8]>,
36 year: i32,
37 ) -> Result<i32, UnknownEraError> {
38 match era {
39 Some(b"default") | None => Ok(year),
40 Some(_) => Err(UnknownEraError),
41 }
42 }
43
44 fn era_year_from_extended(&self, extended_year: i32, _month: u8, _day: u8) -> types::EraYear {
45 types::EraYear {
46 era_index: Some(0),
47 era: tinystr!(16, "default"),
48 year: extended_year,
49 extended_year,
50 ambiguity: types::YearAmbiguity::Unambiguous,
51 }
52 }
53
54 fn debug_name(&self) -> &'static str {
55 "ISO"
56 }
57 }
58
59 impl Date<Iso> {
60 /// Construct a new ISO [`Date`].
61 ///
62 /// Years are arithmetic, meaning there is a year 0 preceded by negative years, with a
63 /// valid range of `-9999..=9999`.
64 ///
65 /// ```rust
66 /// use icu::calendar::Date;
67 ///
68 /// let date_iso = Date::try_new_iso(1970, 1, 2)
69 /// .expect("Failed to initialize ISO Date instance.");
70 ///
71 /// assert_eq!(date_iso.era_year().year, 1970);
72 /// assert_eq!(date_iso.month().ordinal, 1);
73 /// assert_eq!(date_iso.day_of_month().0, 2);
74 /// ```
75 pub fn try_new_iso(year: i32, month: u8, day: u8) -> Result<Date<Iso>, RangeError> {
76 ArithmeticDate::from_year_month_day(year, month, day, &AbstractGregorian(IsoEra))
77 .map(ArithmeticDate::cast)
78 .map(IsoDateInner)
79 .map(|i| Date::from_raw(i, Iso))
80 }
81 }
82
83 impl Iso {
84 /// Construct a new ISO Calendar
85 pub fn new() -> Self {
86 Self
87 }
88 }
89
90 #[cfg(test)]
91 mod test {
92 use super::*;
93 use crate::{
94 calendar_arithmetic::{CONSTRUCTOR_YEAR_RANGE, VALID_RD_RANGE},
95 types::{DateDuration, RataDie, Weekday},
96 };
97
98 #[test]
99 fn iso_overflow() {
100 #[derive(Debug)]
101 struct TestCase {
102 year: i32,
103 month: u8,
104 day: u8,
105 rd: RataDie,
106 invalid_ymd: bool,
107 clamping_rd: bool,
108 }
109 let cases = [
110 // Clamping RD
111 TestCase {
112 year: -999999,
113 month: 1,
114 day: 1,
115 rd: *VALID_RD_RANGE.start() - 100000,
116 invalid_ymd: true,
117 clamping_rd: true,
118 },
119 // Lowest allowed RD
120 TestCase {
121 year: -999999,
122 month: 1,
123 day: 1,
124 rd: *VALID_RD_RANGE.start(),
125 invalid_ymd: true,
126 clamping_rd: false,
127 },
128 // Lowest allowed YMD
129 TestCase {
130 year: *CONSTRUCTOR_YEAR_RANGE.start(),
131 month: 1,
132 day: 1,
133 rd: RataDie::new(-3652424),
134 invalid_ymd: false,
135 clamping_rd: false,
136 },
137 // Highest allowed YMD
138 TestCase {
139 year: *CONSTRUCTOR_YEAR_RANGE.end(),
140 month: 12,
141 day: 31,
142 rd: RataDie::new(3652059),
143 invalid_ymd: false,
144 clamping_rd: false,
145 },
146 // Highest allowed RD
147 TestCase {
148 year: 999999,
149 month: 12,
150 day: 31,
151 rd: *VALID_RD_RANGE.end(),
152 invalid_ymd: true,
153 clamping_rd: false,
154 },
155 // Clamping RD
156 TestCase {
157 year: 999999,
158 month: 12,
159 day: 31,
160 rd: *VALID_RD_RANGE.end() + 100000,
161 invalid_ymd: true,
162 clamping_rd: true,
163 },
164 ];
165
166 for case in cases {
167 let date_from_rd = Date::from_rata_die(case.rd, Iso);
168 let date_from_ymd = Date::try_new_iso(case.year, case.month, case.day);
169
170 if !case.clamping_rd {
171 assert_eq!(date_from_rd.to_rata_die(), case.rd, "{:?}", case);
172 } else {
173 assert_ne!(date_from_rd.to_rata_die(), case.rd, "{:?}", case);
174 }
175
176 if !case.invalid_ymd {
177 assert_eq!(date_from_ymd.unwrap().to_rata_die(), case.rd, "{case:?}");
178 } else {
179 assert_eq!(
180 date_from_ymd,
181 Err(RangeError {
182 field: "year",
183 value: case.year,
184 min: *CONSTRUCTOR_YEAR_RANGE.start(),
185 max: *CONSTRUCTOR_YEAR_RANGE.end()
186 }),
187 "{case:?}"
188 )
189 }
190 assert_eq!(
191 (
192 date_from_rd.era_year().year,
193 date_from_rd.month().number(),
194 date_from_rd.day_of_month().0
195 ),
196 (case.year, case.month, case.day),
197 "{case:?}"
198 );
199 }
200 }
201
202 #[test]
203 fn test_weekday() {
204 // June 23, 2021 is a Wednesday
205 assert_eq!(
206 Date::try_new_iso(2021, 6, 23).unwrap().weekday(),
207 Weekday::Wednesday,
208 );
209 // Feb 2, 1983 was a Wednesday
210 assert_eq!(
211 Date::try_new_iso(1983, 2, 2).unwrap().weekday(),
212 Weekday::Wednesday,
213 );
214 // Jan 21, 2021 was a Tuesday
215 assert_eq!(
216 Date::try_new_iso(2020, 1, 21).unwrap().weekday(),
217 Weekday::Tuesday,
218 );
219 }
220
221 #[test]
222 fn test_day_of_year() {
223 // June 23, 2021 was day 174
224 assert_eq!(Date::try_new_iso(2021, 6, 23).unwrap().day_of_year().0, 174,);
225 // June 23, 2020 was day 175
226 assert_eq!(Date::try_new_iso(2020, 6, 23).unwrap().day_of_year().0, 175,);
227 // Feb 2, 1983 was a Wednesday
228 assert_eq!(Date::try_new_iso(1983, 2, 2).unwrap().day_of_year().0, 33,);
229 }
230
231 #[test]
232 fn test_offset() {
233 let today = Date::try_new_iso(2021, 6, 23).unwrap();
234 let today_plus_5000 = Date::try_new_iso(2035, 3, 2).unwrap();
235 let offset = today
236 .try_added_with_options(DateDuration::for_days(5000), Default::default())
237 .unwrap();
238 assert_eq!(offset, today_plus_5000);
239
240 let today = Date::try_new_iso(2021, 6, 23).unwrap();
241 let today_minus_5000 = Date::try_new_iso(2007, 10, 15).unwrap();
242 let offset = today
243 .try_added_with_options(DateDuration::for_days(-5000), Default::default())
244 .unwrap();
245 assert_eq!(offset, today_minus_5000);
246 }
247
248 #[test]
249 fn test_offset_at_month_boundary() {
250 let today = Date::try_new_iso(2020, 2, 28).unwrap();
251 let today_plus_2 = Date::try_new_iso(2020, 3, 1).unwrap();
252 let offset = today
253 .try_added_with_options(DateDuration::for_days(2), Default::default())
254 .unwrap();
255 assert_eq!(offset, today_plus_2);
256
257 let today = Date::try_new_iso(2020, 2, 28).unwrap();
258 let today_plus_3 = Date::try_new_iso(2020, 3, 2).unwrap();
259 let offset = today
260 .try_added_with_options(DateDuration::for_days(3), Default::default())
261 .unwrap();
262 assert_eq!(offset, today_plus_3);
263
264 let today = Date::try_new_iso(2020, 2, 28).unwrap();
265 let today_plus_1 = Date::try_new_iso(2020, 2, 29).unwrap();
266 let offset = today
267 .try_added_with_options(DateDuration::for_days(1), Default::default())
268 .unwrap();
269 assert_eq!(offset, today_plus_1);
270
271 let today = Date::try_new_iso(2019, 2, 28).unwrap();
272 let today_plus_2 = Date::try_new_iso(2019, 3, 2).unwrap();
273 let offset = today
274 .try_added_with_options(DateDuration::for_days(2), Default::default())
275 .unwrap();
276 assert_eq!(offset, today_plus_2);
277
278 let today = Date::try_new_iso(2019, 2, 28).unwrap();
279 let today_plus_1 = Date::try_new_iso(2019, 3, 1).unwrap();
280 let offset = today
281 .try_added_with_options(DateDuration::for_days(1), Default::default())
282 .unwrap();
283 assert_eq!(offset, today_plus_1);
284
285 let today = Date::try_new_iso(2020, 3, 1).unwrap();
286 let today_minus_1 = Date::try_new_iso(2020, 2, 29).unwrap();
287 let offset = today
288 .try_added_with_options(DateDuration::for_days(-1), Default::default())
289 .unwrap();
290 assert_eq!(offset, today_minus_1);
291 }
292
293 #[test]
294 fn test_offset_handles_negative_month_offset() {
295 let today = Date::try_new_iso(2020, 3, 1).unwrap();
296 let today_minus_2_months = Date::try_new_iso(2020, 1, 1).unwrap();
297 let offset = today
298 .try_added_with_options(DateDuration::for_months(-2), Default::default())
299 .unwrap();
300 assert_eq!(offset, today_minus_2_months);
301
302 let today = Date::try_new_iso(2020, 3, 1).unwrap();
303 let today_minus_4_months = Date::try_new_iso(2019, 11, 1).unwrap();
304 let offset = today
305 .try_added_with_options(DateDuration::for_months(-4), Default::default())
306 .unwrap();
307 assert_eq!(offset, today_minus_4_months);
308
309 let today = Date::try_new_iso(2020, 3, 1).unwrap();
310 let today_minus_24_months = Date::try_new_iso(2018, 3, 1).unwrap();
311 let offset = today
312 .try_added_with_options(DateDuration::for_months(-24), Default::default())
313 .unwrap();
314 assert_eq!(offset, today_minus_24_months);
315
316 let today = Date::try_new_iso(2020, 3, 1).unwrap();
317 let today_minus_27_months = Date::try_new_iso(2017, 12, 1).unwrap();
318 let offset = today
319 .try_added_with_options(DateDuration::for_months(-27), Default::default())
320 .unwrap();
321 assert_eq!(offset, today_minus_27_months);
322 }
323
324 #[test]
325 fn test_offset_handles_out_of_bound_month_offset() {
326 let today = Date::try_new_iso(2021, 1, 31).unwrap();
327 // since 2021/02/31 isn't a valid date, `offset_date` auto-adjusts by constraining to the last day in February
328 let today_plus_1_month = Date::try_new_iso(2021, 2, 28).unwrap();
329 let offset = today
330 .try_added_with_options(DateDuration::for_months(1), Default::default())
331 .unwrap();
332 assert_eq!(offset, today_plus_1_month);
333
334 let today = Date::try_new_iso(2021, 1, 31).unwrap();
335 // since 2021/02/31 isn't a valid date, `offset_date` auto-adjusts by constraining to the last day in February
336 // and then adding the days
337 let today_plus_1_month_1_day = Date::try_new_iso(2021, 3, 1).unwrap();
338 let offset = today
339 .try_added_with_options(
340 DateDuration {
341 months: 1,
342 days: 1,
343 ..Default::default()
344 },
345 Default::default(),
346 )
347 .unwrap();
348 assert_eq!(offset, today_plus_1_month_1_day);
349 }
350
351 #[test]
352 fn test_iso_to_from_rd() {
353 // Reminder: ISO year 0 is Gregorian year 1 BCE.
354 // Year 0 is a leap year due to the 400-year rule.
355 fn check(rd: i64, year: i32, month: u8, day: u8) {
356 let rd = RataDie::new(rd);
357
358 assert_eq!(
359 Date::from_rata_die(rd, Iso),
360 Date::try_new_iso(year, month, day).unwrap(),
361 "RD: {rd:?}"
362 );
363 }
364 check(-1828, -5, 12, 30);
365 check(-1827, -5, 12, 31); // leap year
366 check(-1826, -4, 1, 1);
367 check(-1462, -4, 12, 30);
368 check(-1461, -4, 12, 31);
369 check(-1460, -3, 1, 1);
370 check(-1459, -3, 1, 2);
371 check(-732, -2, 12, 30);
372 check(-731, -2, 12, 31);
373 check(-730, -1, 1, 1);
374 check(-367, -1, 12, 30);
375 check(-366, -1, 12, 31);
376 check(-365, 0, 1, 1); // leap year
377 check(-364, 0, 1, 2);
378 check(-1, 0, 12, 30);
379 check(0, 0, 12, 31);
380 check(1, 1, 1, 1);
381 check(2, 1, 1, 2);
382 check(364, 1, 12, 30);
383 check(365, 1, 12, 31);
384 check(366, 2, 1, 1);
385 check(1459, 4, 12, 29);
386 check(1460, 4, 12, 30);
387 check(1461, 4, 12, 31); // leap year
388 check(1462, 5, 1, 1);
389 }
390
391 #[test]
392 fn test_constructor_roundtrip() {
393 let rds = crate::tests::get_interesting_rds();
394 for rd in rds {
395 let date = Date::from_rata_die(rd, Iso);
396 let reconstructed = Date::try_new_iso(
397 date.year().extended_year(),
398 date.month().ordinal,
399 date.day_of_month().0,
400 )
401 .unwrap();
402 assert_eq!(
403 reconstructed.to_rata_die(),
404 rd,
405 "ISO failed for RD {:?}",
406 rd
407 );
408 }
409 }
410 }