| 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::preferences::CalendarAlgorithm; |
| 11 |
use crate::{Date, RangeError, types}; |
| 12 |
use tinystr::tinystr; |
| 13 |
|
| 14 |
/// The [Republic of China Calendar](https://en.wikipedia.org/wiki/Republic_of_China_calendar) |
| 15 |
/// |
| 16 |
/// The ROC Calendar is a variant of the [`Gregorian`](crate::cal::Gregorian) calendar |
| 17 |
/// created by the government of the Republic of China. It is identical to the Gregorian |
| 18 |
/// calendar except that is uses the ROC/Minguo/民国/民國 Era (1912 CE) instead of the Common Era. |
| 19 |
/// |
| 20 |
/// This implementation extends proleptically for dates before the calendar's creation |
| 21 |
/// in 1 Minguo (1912 CE). |
| 22 |
/// |
| 23 |
/// The ROC calendar should not be confused with the [`ChineseTraditional`](crate::cal::ChineseTraditional) |
| 24 |
/// lunisolar calendar. |
| 25 |
/// |
| 26 |
/// This corresponds to the `"roc"` [CLDR calendar](https://unicode.org/reports/tr35/#UnicodeCalendarIdentifier). |
| 27 |
/// |
| 28 |
/// # Era codes |
| 29 |
/// |
| 30 |
/// This calendar uses two era codes: `roc`, corresponding to years in the 民國 era (CE year 1912 and |
| 31 |
/// after), and `broc`, corresponding to years before the 民國 era (CE year 1911 and before). |
| 32 |
#[derive(Copy, Clone, Debug, Default)] |
| 33 |
#[allow(clippy::exhaustive_structs)] // this type is stable |
| 34 |
pub struct Roc; |
| 35 |
|
| 36 |
impl_with_abstract_gregorian!(Roc, RocDateInner, RocEra, _x, RocEra); |
| 37 |
|
| 38 |
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 39 |
pub(crate) struct RocEra; |
| 40 |
|
| 41 |
impl GregorianYears for RocEra { |
| 42 |
const EXTENDED_YEAR_OFFSET: i32 = 1911; |
| 43 |
|
| 44 |
fn extended_from_era_year( |
| 45 |
&self, |
| 46 |
era: Option<&[u8]>, |
| 47 |
year: i32, |
| 48 |
) -> Result<i32, UnknownEraError> { |
| 49 |
match era { |
| 50 |
None => Ok(year), |
| 51 |
Some(b"roc") => Ok(year), |
| 52 |
Some(b"broc") => Ok(1 - year), |
| 53 |
Some(_) => Err(UnknownEraError), |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
fn era_year_from_extended(&self, extended_year: i32, _month: u8, _day: u8) -> types::EraYear { |
| 58 |
if extended_year > 0 { |
| 59 |
types::EraYear { |
| 60 |
era: tinystr!(16, "roc"), |
| 61 |
era_index: Some(1), |
| 62 |
year: extended_year, |
| 63 |
extended_year, |
| 64 |
ambiguity: types::YearAmbiguity::CenturyRequired, |
| 65 |
} |
| 66 |
} else { |
| 67 |
types::EraYear { |
| 68 |
era: tinystr!(16, "broc"), |
| 69 |
era_index: Some(0), |
| 70 |
year: 1 - extended_year, |
| 71 |
extended_year, |
| 72 |
ambiguity: types::YearAmbiguity::EraAndCenturyRequired, |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
fn debug_name(&self) -> &'static str { |
| 78 |
"ROC" |
| 79 |
} |
| 80 |
|
| 81 |
fn calendar_algorithm(&self) -> Option<CalendarAlgorithm> { |
| 82 |
Some(CalendarAlgorithm::Roc) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
impl Date<Roc> { |
| 87 |
/// Construct a new Republic of China calendar [`Date`]. |
| 88 |
/// |
| 89 |
/// Years are arithmetic, meaning there is a year 0 preceded by negative years, with a |
| 90 |
/// valid range of `-9999..=9999`. |
| 91 |
/// |
| 92 |
/// ```rust |
| 93 |
/// use icu::calendar::Date; |
| 94 |
/// use icu::calendar::cal::Gregorian; |
| 95 |
/// use tinystr::tinystr; |
| 96 |
/// |
| 97 |
/// // Create a new ROC Date |
| 98 |
/// let date_roc = Date::try_new_roc(1, 2, 3) |
| 99 |
/// .expect("Failed to initialize ROC Date instance."); |
| 100 |
/// |
| 101 |
/// assert_eq!(date_roc.era_year().era, "roc"); |
| 102 |
/// assert_eq!(date_roc.era_year().year, 1, "ROC year check failed!"); |
| 103 |
/// assert_eq!(date_roc.month().ordinal, 2, "ROC month check failed!"); |
| 104 |
/// assert_eq!(date_roc.day_of_month().0, 3, "ROC day of month check failed!"); |
| 105 |
/// |
| 106 |
/// // Convert to an equivalent Gregorian date |
| 107 |
/// let date_gregorian = date_roc.to_calendar(Gregorian); |
| 108 |
/// |
| 109 |
/// assert_eq!(date_gregorian.era_year().year, 1912, "Gregorian from ROC year check failed!"); |
| 110 |
/// assert_eq!(date_gregorian.month().ordinal, 2, "Gregorian from ROC month check failed!"); |
| 111 |
/// assert_eq!(date_gregorian.day_of_month().0, 3, "Gregorian from ROC day of month check failed!"); |
| 112 |
pub fn try_new_roc(year: i32, month: u8, day: u8) -> Result<Date<Roc>, RangeError> { |
| 113 |
ArithmeticDate::from_year_month_day(year, month, day, &AbstractGregorian(RocEra)) |
| 114 |
.map(ArithmeticDate::cast) |
| 115 |
.map(RocDateInner) |
| 116 |
.map(|i| Date::from_raw(i, Roc)) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#[cfg(test)] |
| 121 |
mod test { |
| 122 |
|
| 123 |
use super::*; |
| 124 |
use crate::cal::Iso; |
| 125 |
use calendrical_calculations::rata_die::RataDie; |
| 126 |
|
| 127 |
#[derive(Debug)] |
| 128 |
struct TestCase { |
| 129 |
rd: RataDie, |
| 130 |
year: i32, |
| 131 |
era: &'static str, |
| 132 |
month: u8, |
| 133 |
day: u8, |
| 134 |
} |
| 135 |
|
| 136 |
fn check_test_case(case: TestCase) { |
| 137 |
let roc_from_rd = Date::from_rata_die(case.rd, Roc); |
| 138 |
assert_eq!( |
| 139 |
roc_from_rd.era_year().year, |
| 140 |
case.year, |
| 141 |
"Failed year check from RD: {case:?}\nROC: {roc_from_rd:?}" |
| 142 |
); |
| 143 |
assert_eq!( |
| 144 |
roc_from_rd.era_year().era, |
| 145 |
case.era, |
| 146 |
"Failed era check from RD: {case:?}\nROC: {roc_from_rd:?}" |
| 147 |
); |
| 148 |
assert_eq!( |
| 149 |
roc_from_rd.year().extended_year(), |
| 150 |
if case.era == "roc" { |
| 151 |
case.year |
| 152 |
} else { |
| 153 |
1 - case.year |
| 154 |
}, |
| 155 |
"Failed year check from RD: {case:?}\nROC: {roc_from_rd:?}" |
| 156 |
); |
| 157 |
assert_eq!( |
| 158 |
roc_from_rd.month().ordinal, |
| 159 |
case.month, |
| 160 |
"Failed month check from RD: {case:?}\nROC: {roc_from_rd:?}" |
| 161 |
); |
| 162 |
assert_eq!( |
| 163 |
roc_from_rd.day_of_month().0, |
| 164 |
case.day, |
| 165 |
"Failed day_of_month check from RD: {case:?}\nROC: {roc_from_rd:?}" |
| 166 |
); |
| 167 |
|
| 168 |
let roc_from_case = Date::try_new_roc( |
| 169 |
roc_from_rd.year().extended_year(), |
| 170 |
roc_from_rd.month().ordinal, |
| 171 |
roc_from_rd.day_of_month().0, |
| 172 |
) |
| 173 |
.unwrap(); |
| 174 |
assert_eq!( |
| 175 |
roc_from_rd, roc_from_case, |
| 176 |
"ROC date from RD not equal to ROC generated from manually-input ymd\nCase: {case:?}\nRD: {roc_from_rd:?}\nManual: {roc_from_case:?}" |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn test_roc_current_era() { |
| 182 |
// Tests that the ROC calendar gives the correct expected day, month, and year for years >= 1912 |
| 183 |
// (years in the ROC/minguo era) |
| 184 |
// |
| 185 |
// Jan 1. 1912 CE = RD 697978 |
| 186 |
|
| 187 |
let cases = [ |
| 188 |
TestCase { |
| 189 |
rd: Date::try_new_iso(1912, 1, 1).unwrap().to_rata_die(), |
| 190 |
year: 1, |
| 191 |
era: "roc", |
| 192 |
month: 1, |
| 193 |
day: 1, |
| 194 |
}, |
| 195 |
TestCase { |
| 196 |
rd: Date::try_new_iso(1912, 2, 29).unwrap().to_rata_die(), |
| 197 |
year: 1, |
| 198 |
era: "roc", |
| 199 |
month: 2, |
| 200 |
day: 29, |
| 201 |
}, |
| 202 |
TestCase { |
| 203 |
rd: Date::try_new_iso(1913, 6, 30).unwrap().to_rata_die(), |
| 204 |
year: 2, |
| 205 |
era: "roc", |
| 206 |
month: 6, |
| 207 |
day: 30, |
| 208 |
}, |
| 209 |
TestCase { |
| 210 |
rd: Date::try_new_iso(2023, 7, 13).unwrap().to_rata_die(), |
| 211 |
year: 112, |
| 212 |
era: "roc", |
| 213 |
month: 7, |
| 214 |
day: 13, |
| 215 |
}, |
| 216 |
]; |
| 217 |
|
| 218 |
for case in cases { |
| 219 |
check_test_case(case); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
#[test] |
| 224 |
fn test_roc_prior_era() { |
| 225 |
// Tests that the ROC calendar gives the correct expected day, month, and year for years <= 1911 |
| 226 |
// (years in the ROC/minguo era) |
| 227 |
// |
| 228 |
// Jan 1. 1912 CE = RD 697978 |
| 229 |
let cases = [ |
| 230 |
TestCase { |
| 231 |
rd: Date::try_new_iso(1911, 12, 31).unwrap().to_rata_die(), |
| 232 |
year: 1, |
| 233 |
era: "broc", |
| 234 |
month: 12, |
| 235 |
day: 31, |
| 236 |
}, |
| 237 |
TestCase { |
| 238 |
rd: Date::try_new_iso(1911, 1, 1).unwrap().to_rata_die(), |
| 239 |
year: 1, |
| 240 |
era: "broc", |
| 241 |
month: 1, |
| 242 |
day: 1, |
| 243 |
}, |
| 244 |
TestCase { |
| 245 |
rd: Date::try_new_iso(1910, 12, 31).unwrap().to_rata_die(), |
| 246 |
year: 2, |
| 247 |
era: "broc", |
| 248 |
month: 12, |
| 249 |
day: 31, |
| 250 |
}, |
| 251 |
TestCase { |
| 252 |
rd: Date::try_new_iso(1908, 2, 29).unwrap().to_rata_die(), |
| 253 |
year: 4, |
| 254 |
era: "broc", |
| 255 |
month: 2, |
| 256 |
day: 29, |
| 257 |
}, |
| 258 |
TestCase { |
| 259 |
rd: Date::try_new_iso(1, 1, 1).unwrap().to_rata_die(), |
| 260 |
year: 1911, |
| 261 |
era: "broc", |
| 262 |
month: 1, |
| 263 |
day: 1, |
| 264 |
}, |
| 265 |
TestCase { |
| 266 |
rd: Date::try_new_iso(0, 12, 31).unwrap().to_rata_die(), |
| 267 |
year: 1912, |
| 268 |
era: "broc", |
| 269 |
month: 12, |
| 270 |
day: 31, |
| 271 |
}, |
| 272 |
]; |
| 273 |
|
| 274 |
for case in cases { |
| 275 |
check_test_case(case); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
#[test] |
| 280 |
fn test_roc_directionality_near_epoch() { |
| 281 |
// Tests that for a large range of RDs near the beginning of the minguo era (CE 1912), |
| 282 |
// the comparison between those two RDs should be equal to the comparison between their |
| 283 |
// corresponding YMD. |
| 284 |
let rd_epoch_start = 697978; |
| 285 |
for i in (rd_epoch_start - 100)..=(rd_epoch_start + 100) { |
| 286 |
for j in (rd_epoch_start - 100)..=(rd_epoch_start + 100) { |
| 287 |
let iso_i = Date::from_rata_die(RataDie::new(i), Iso); |
| 288 |
let iso_j = Date::from_rata_die(RataDie::new(j), Iso); |
| 289 |
|
| 290 |
let roc_i = Date::from_rata_die(RataDie::new(i), Roc); |
| 291 |
let roc_j = Date::from_rata_die(RataDie::new(j), Roc); |
| 292 |
|
| 293 |
assert_eq!( |
| 294 |
i.cmp(&j), |
| 295 |
iso_i.cmp(&iso_j), |
| 296 |
"ISO directionality inconsistent with directionality for i: {i}, j: {j}" |
| 297 |
); |
| 298 |
assert_eq!( |
| 299 |
i.cmp(&j), |
| 300 |
roc_i.cmp(&roc_j), |
| 301 |
"ROC directionality inconsistent with directionality for i: {i}, j: {j}" |
| 302 |
); |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
fn test_roc_directionality_near_rd_zero() { |
| 309 |
// Same as `test_directionality_near_epoch`, but with a focus around RD 0 |
| 310 |
for i in -100..=100 { |
| 311 |
for j in -100..100 { |
| 312 |
let iso_i = Date::from_rata_die(RataDie::new(i), Iso); |
| 313 |
let iso_j = Date::from_rata_die(RataDie::new(j), Iso); |
| 314 |
|
| 315 |
let roc_i = Date::from_rata_die(RataDie::new(i), Roc); |
| 316 |
let roc_j = Date::from_rata_die(RataDie::new(j), Roc); |
| 317 |
|
| 318 |
assert_eq!( |
| 319 |
i.cmp(&j), |
| 320 |
iso_i.cmp(&iso_j), |
| 321 |
"ISO directionality inconsistent with directionality for i: {i}, j: {j}" |
| 322 |
); |
| 323 |
assert_eq!( |
| 324 |
i.cmp(&j), |
| 325 |
roc_i.cmp(&roc_j), |
| 326 |
"ROC directionality inconsistent with directionality for i: {i}, j: {j}" |
| 327 |
); |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
#[test] |
| 333 |
fn test_constructor_roundtrip() { |
| 334 |
let rds = crate::tests::get_interesting_rds(); |
| 335 |
for rd in rds { |
| 336 |
let date = Date::from_rata_die(rd, Roc); |
| 337 |
let reconstructed = Date::try_new_roc( |
| 338 |
date.year().extended_year(), |
| 339 |
date.month().ordinal, |
| 340 |
date.day_of_month().0, |
| 341 |
) |
| 342 |
.unwrap(); |
| 343 |
assert_eq!( |
| 344 |
reconstructed.to_rata_die(), |
| 345 |
rd, |
| 346 |
"Roc failed for RD {:?}", |
| 347 |
rd |
| 348 |
); |
| 349 |
} |
| 350 |
} |
| 351 |
} |