| 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::convert::Infallible; |
| 6 |
|
| 7 |
use icu_calendar::{ |
| 8 |
AsCalendar, Calendar, Date, Iso, |
| 9 |
cal::Hebrew, |
| 10 |
options::{DateAddOptions, DateDifferenceOptions, DateDurationUnit, Overflow}, |
| 11 |
types::{DateDuration, Month}, |
| 12 |
}; |
| 13 |
|
| 14 |
#[rustfmt::skip] |
| 15 |
#[allow(clippy::type_complexity)] |
| 16 |
const ISO_DATE_PAIRS: &[(&str, &str, u32, (u32, u32), (u32, u32), (u32, u32, u32))] = &[ |
| 17 |
// d0, d1, D, (W, D), (M, D), (Y, M, D) |
| 18 |
("2020-01-03", "2020-02-15", 43, (6, 1), (1, 12), (0, 1, 12)), |
| 19 |
("2020-01-31", "2020-06-30", 151, (21, 4), (4, 30), (0, 4, 30)), |
| 20 |
("2020-03-31", "2020-07-30", 121, (17, 2), (3, 30), (0, 3, 30)), |
| 21 |
("2020-03-31", "2020-07-31", 122, (17, 3), (4, 0), (0, 4, 0)), |
| 22 |
("2016-03-20", "2020-03-05", 1446, (206, 4), (47, 14), (3, 11, 14)), |
| 23 |
("2020-02-29", "2022-03-01", 731, (104, 3), (24, 1), (2, 0, 1)), |
| 24 |
|
| 25 |
// Negative direction: |
| 26 |
("2020-02-15", "2020-01-03", 43, (6, 1), (1, 12), (0, 1, 12)), |
| 27 |
("2020-06-30", "2020-01-31", 151, (21, 4), (4, 29), (0, 4, 29)), // DIFF +/- |
| 28 |
("2020-07-30", "2020-03-31", 121, (17, 2), (3, 30), (0, 3, 30)), |
| 29 |
("2020-07-31", "2020-03-31", 122, (17, 3), (4, 0), (0, 4, 0)), |
| 30 |
("2020-03-05", "2016-03-20", 1446, (206, 4), (47, 16), (3, 11, 16)), // DIFF +/- |
| 31 |
("2022-03-01", "2020-02-29", 731, (104, 3), (24, 1), (2, 0, 1)), |
| 32 |
]; |
| 33 |
|
| 34 |
#[allow(clippy::trivially_copy_pass_by_ref)] |
| 35 |
fn check<A>( |
| 36 |
d0: &Date<A>, |
| 37 |
d1: &Date<A>, |
| 38 |
exp0: &u32, |
| 39 |
exp1: &(u32, u32), |
| 40 |
exp2: &(u32, u32), |
| 41 |
exp3: &(u32, u32, u32), |
| 42 |
) where |
| 43 |
A: AsCalendar + Copy, |
| 44 |
<A as AsCalendar>::Calendar: Calendar<DateCompatibilityError = Infallible>, |
| 45 |
<<A as AsCalendar>::Calendar as Calendar>::DateInner: PartialOrd, |
| 46 |
{ |
| 47 |
let is_negative = d0 > d1; |
| 48 |
let mut add_options = DateAddOptions::default(); |
| 49 |
add_options.overflow = Some(Overflow::Constrain); |
| 50 |
let mut until_options0 = DateDifferenceOptions::default(); |
| 51 |
until_options0.largest_unit = Some(DateDurationUnit::Days); |
| 52 |
let mut until_options1 = DateDifferenceOptions::default(); |
| 53 |
until_options1.largest_unit = Some(DateDurationUnit::Weeks); |
| 54 |
let mut until_options2 = DateDifferenceOptions::default(); |
| 55 |
until_options2.largest_unit = Some(DateDurationUnit::Months); |
| 56 |
let mut until_options3 = DateDifferenceOptions::default(); |
| 57 |
until_options3.largest_unit = Some(DateDurationUnit::Years); |
| 58 |
|
| 59 |
let Ok(p0) = d0.try_until_with_options(d1, until_options0); |
| 60 |
assert_eq!( |
| 61 |
p0, |
| 62 |
DateDuration { |
| 63 |
is_negative, |
| 64 |
days: *exp0, |
| 65 |
..Default::default() |
| 66 |
}, |
| 67 |
"{d0:?}/{d1:?}" |
| 68 |
); |
| 69 |
assert_eq!( |
| 70 |
d0.try_added_with_options(p0, add_options).unwrap(), |
| 71 |
*d1, |
| 72 |
"{d0:?}/{d1:?}" |
| 73 |
); |
| 74 |
|
| 75 |
let Ok(p1) = d0.try_until_with_options(d1, until_options1); |
| 76 |
assert_eq!( |
| 77 |
p1, |
| 78 |
DateDuration { |
| 79 |
is_negative, |
| 80 |
weeks: exp1.0, |
| 81 |
days: exp1.1, |
| 82 |
..Default::default() |
| 83 |
}, |
| 84 |
"{d0:?}/{d1:?}" |
| 85 |
); |
| 86 |
assert_eq!( |
| 87 |
d0.try_added_with_options(p1, add_options).unwrap(), |
| 88 |
*d1, |
| 89 |
"{d0:?}/{d1:?}" |
| 90 |
); |
| 91 |
|
| 92 |
let Ok(p2) = d0.try_until_with_options(d1, until_options2); |
| 93 |
assert_eq!( |
| 94 |
p2, |
| 95 |
DateDuration { |
| 96 |
is_negative, |
| 97 |
months: exp2.0, |
| 98 |
days: exp2.1, |
| 99 |
..Default::default() |
| 100 |
}, |
| 101 |
"{d0:?}/{d1:?}" |
| 102 |
); |
| 103 |
assert_eq!( |
| 104 |
d0.try_added_with_options(p2, add_options).unwrap(), |
| 105 |
*d1, |
| 106 |
"{d0:?}/{d1:?}" |
| 107 |
); |
| 108 |
|
| 109 |
let Ok(p3) = d0.try_until_with_options(d1, until_options3); |
| 110 |
assert_eq!( |
| 111 |
p3, |
| 112 |
DateDuration { |
| 113 |
is_negative, |
| 114 |
years: exp3.0, |
| 115 |
months: exp3.1, |
| 116 |
days: exp3.2, |
| 117 |
..Default::default() |
| 118 |
}, |
| 119 |
"{d0:?}/{d1:?}" |
| 120 |
); |
| 121 |
assert_eq!( |
| 122 |
d0.try_added_with_options(p3, add_options).unwrap(), |
| 123 |
*d1, |
| 124 |
"{d0:?}/{d1:?}" |
| 125 |
); |
| 126 |
|
| 127 |
// RataDie addition should be equivalent for largest unit Days and Weeks |
| 128 |
let rd_diff = d1.to_rata_die() - d0.to_rata_die(); |
| 129 |
if is_negative { |
| 130 |
assert!(rd_diff.is_negative()); |
| 131 |
} |
| 132 |
assert_eq!(p0.days, rd_diff.unsigned_abs() as u32); |
| 133 |
assert_eq!(p1.days + p1.weeks * 7, rd_diff.unsigned_abs() as u32); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn test_arithmetic_cases() { |
| 138 |
for (d0, d1, exp0, exp1, exp2, exp3) in ISO_DATE_PAIRS { |
| 139 |
let d0 = Date::try_from_str(d0, Iso).unwrap(); |
| 140 |
let d1 = Date::try_from_str(d1, Iso).unwrap(); |
| 141 |
check(&d0, &d1, exp0, exp1, exp2, exp3); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
#[test] |
| 146 |
fn test_hebrew() { |
| 147 |
let m06z_20 = Date::try_new_hebrew_v2(5783, Month::new(6), 20).unwrap(); |
| 148 |
let m05l_15 = Date::try_new_hebrew_v2(5784, Month::leap(5), 15).unwrap(); |
| 149 |
let m05l_30 = Date::try_new_hebrew_v2(5784, Month::leap(5), 30).unwrap(); |
| 150 |
let m06a_29 = Date::try_new_hebrew_v2(5784, Month::new(6), 29).unwrap(); |
| 151 |
let m07a_10 = Date::try_new_hebrew_v2(5784, Month::new(7), 10).unwrap(); |
| 152 |
let m06b_15 = Date::try_new_hebrew_v2(5785, Month::new(6), 15).unwrap(); |
| 153 |
let m07b_20 = Date::try_new_hebrew_v2(5785, Month::new(7), 20).unwrap(); |
| 154 |
|
| 155 |
#[rustfmt::skip] |
| 156 |
#[allow(clippy::type_complexity)] |
| 157 |
let cases: &[(&Date<Hebrew>, &Date<Hebrew>, u32, (u32, u32), (u32, u32), (u32, u32, u32))] = &[ |
| 158 |
(&m06z_20, &m05l_15, 348, (49, 5), (11, 25), (0, 11, 25)), |
| 159 |
(&m06z_20, &m05l_30, 363, (51, 6), (12, 10), (0, 12, 10)), |
| 160 |
(&m06z_20, &m06a_29, 392, (56, 0), (13, 9), (1, 0, 9)), |
| 161 |
(&m06z_20, &m07a_10, 402, (57, 3), (13, 19), (1, 0, 19)), |
| 162 |
(&m06z_20, &m06b_15, 733, (104, 5), (24, 25), (1, 11, 25)), |
| 163 |
(&m06z_20, &m07b_20, 767, (109, 4), (26, 0), (2, 1, 0)), |
| 164 |
|
| 165 |
(&m05l_15, &m05l_30, 15, (2, 1), (0, 15), (0, 0, 15)), |
| 166 |
(&m05l_15, &m06a_29, 44, (6, 2), (1, 14), (0, 1, 14)), |
| 167 |
(&m05l_15, &m07a_10, 54, (7, 5), (1, 24), (0, 1, 24)), |
| 168 |
(&m05l_15, &m06b_15, 385, (55, 0), (13, 0), (1, 0, 0)), // M05L to M06 common year |
| 169 |
(&m05l_15, &m07b_20, 419, (59, 6), (14, 5), (1, 1, 5)), // M05L to M07 common year |
| 170 |
|
| 171 |
(&m05l_30, &m06a_29, 29, (4, 1), (0, 29), (0, 0, 29)), |
| 172 |
(&m05l_30, &m07a_10, 39, (5, 4), (1, 10), (0, 1, 10)), |
| 173 |
(&m05l_30, &m06b_15, 370, (52, 6), (12, 15), (0, 12, 15)), // M05L to M06 common year |
| 174 |
(&m05l_30, &m07b_20, 404, (57, 5), (13, 20), (1, 0, 20)), // M05L to M07 common year |
| 175 |
|
| 176 |
(&m06a_29, &m07a_10, 10, (1, 3), (0, 10), (0, 0, 10)), |
| 177 |
(&m06a_29, &m06b_15, 341, (48, 5), (11, 16), (0, 11, 16)), // M06 leap year to M06 common year |
| 178 |
(&m06a_29, &m07b_20, 375, (53, 4), (12, 20), (1, 0, 20)), // M06 leap year to M07 common year |
| 179 |
(&m07a_10, &m06b_15, 331, (47, 2), (11, 5), (0, 11, 5)), // M07 leap year to M06 common year |
| 180 |
(&m07a_10, &m07b_20, 365, (52, 1), (12, 10), (1, 0, 10)), // M07 leap year to M06 common year |
| 181 |
(&m06b_15, &m07b_20, 34, (4, 6), (1, 5), (0, 1, 5)), |
| 182 |
]; |
| 183 |
|
| 184 |
for (d0, d1, exp0, exp1, exp2, exp3) in cases { |
| 185 |
check(d0, d1, exp0, exp1, exp2, exp3); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
#[test] |
| 190 |
fn test_gregory_leap_addition() { |
| 191 |
let leap_day = Date::try_new_gregorian(2020, 2, 29).unwrap(); |
| 192 |
let mut options = DateAddOptions::default(); |
| 193 |
options.overflow = Some(Overflow::Reject); |
| 194 |
let y4 = DateDuration::for_years(4); |
| 195 |
let result = leap_day.try_added_with_options(y4, options).unwrap(); |
| 196 |
assert_eq!(result, Date::try_new_gregorian(2024, 2, 29).unwrap()); |
| 197 |
} |
| 198 |
|
| 199 |
#[test] |
| 200 |
fn test_tricky_leap_months() { |
| 201 |
let mut add_options = DateAddOptions::default(); |
| 202 |
add_options.overflow = Some(Overflow::Constrain); |
| 203 |
let mut until_options = DateDifferenceOptions::default(); |
| 204 |
until_options.largest_unit = Some(DateDurationUnit::Years); |
| 205 |
|
| 206 |
// M06 + 1yr = M06 (common to leap) |
| 207 |
let date0 = Date::try_new_hebrew_v2(5783, Month::new(6), 20).unwrap(); |
| 208 |
let duration0 = DateDuration::for_years(1); |
| 209 |
let date1 = date0 |
| 210 |
.try_added_with_options(duration0, add_options) |
| 211 |
.unwrap(); |
| 212 |
assert_eq!( |
| 213 |
date1, |
| 214 |
Date::try_new_hebrew_v2(5784, Month::new(6), 20).unwrap() |
| 215 |
); |
| 216 |
let duration0_actual = date0.try_until_with_options(&date1, until_options).unwrap(); |
| 217 |
assert_eq!(duration0_actual, duration0); |
| 218 |
|
| 219 |
// M02L until M02 = 12mo |
| 220 |
let cdate0 = Date::try_new_chinese_traditional(2023, Month::leap(2), 1).unwrap(); |
| 221 |
let cdate1 = Date::try_new_chinese_traditional(2024, Month::new(2), 1).unwrap(); |
| 222 |
let duration0a = DateDuration::for_months(12); |
| 223 |
let diff0 = cdate0 |
| 224 |
.try_until_with_options(&cdate1, until_options) |
| 225 |
.unwrap(); |
| 226 |
assert_eq!(diff0, duration0a); |
| 227 |
|
| 228 |
// M06 - 1mo = M05L (leap to leap) |
| 229 |
let duration1 = DateDuration::for_months(-1); |
| 230 |
let date2 = date1 |
| 231 |
.try_added_with_options(duration1, add_options) |
| 232 |
.unwrap(); |
| 233 |
assert_eq!( |
| 234 |
date2, |
| 235 |
Date::try_new_hebrew_v2(5784, Month::leap(5), 20).unwrap() |
| 236 |
); |
| 237 |
let duration1_actual = date1.try_until_with_options(&date2, until_options).unwrap(); |
| 238 |
assert_eq!(duration1_actual, duration1); |
| 239 |
|
| 240 |
// M05L until previous M06 = -12mo (leap to common) |
| 241 |
let diff1 = date2.try_until_with_options(&date0, until_options).unwrap(); |
| 242 |
let duration0an = DateDuration::for_months(-12); |
| 243 |
assert_eq!(diff1, duration0an); |
| 244 |
|
| 245 |
// M05L + 1yr1mo = M07 (leap to common) |
| 246 |
let duration2 = DateDuration { |
| 247 |
years: 1, |
| 248 |
months: 1, |
| 249 |
..Default::default() |
| 250 |
}; |
| 251 |
let date3 = date2 |
| 252 |
.try_added_with_options(duration2, add_options) |
| 253 |
.unwrap(); |
| 254 |
assert_eq!( |
| 255 |
date3, |
| 256 |
Date::try_new_hebrew_v2(5785, Month::new(7), 20).unwrap() |
| 257 |
); |
| 258 |
let duration2_actual = date2.try_until_with_options(&date3, until_options).unwrap(); |
| 259 |
assert_eq!(duration2_actual, duration2); |
| 260 |
|
| 261 |
// M06 + 1yr1mo = M07 (leap to common) |
| 262 |
let date4 = date1 |
| 263 |
.try_added_with_options(duration2, add_options) |
| 264 |
.unwrap(); |
| 265 |
assert_eq!( |
| 266 |
date4, |
| 267 |
Date::try_new_hebrew_v2(5785, Month::new(7), 20).unwrap() |
| 268 |
); |
| 269 |
let duration2_actual = date1.try_until_with_options(&date4, until_options).unwrap(); |
| 270 |
assert_eq!(duration2_actual, duration2); |
| 271 |
} |