-
Notifications
You must be signed in to change notification settings - Fork 223
Update era codes #6525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update era codes #6525
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ use crate::cal::hijri_ummalqura_data::{UMMALQURA_DATA, UMMALQURA_DATA_STARTING_Y | |
use crate::cal::iso::{Iso, IsoDateInner}; | ||
use crate::calendar_arithmetic::PrecomputedDataSource; | ||
use crate::calendar_arithmetic::{ArithmeticDate, CalendarArithmetic}; | ||
use crate::error::DateError; | ||
use crate::error::{year_check, DateError}; | ||
use crate::provider::hijri::PackedHijriYearInfo; | ||
use crate::provider::hijri::{CalendarHijriSimulatedMeccaV1, HijriData}; | ||
use crate::types::EraYear; | ||
|
@@ -36,19 +36,28 @@ use icu_provider::prelude::*; | |
use tinystr::tinystr; | ||
|
||
fn era_year(year: i32) -> EraYear { | ||
types::EraYear { | ||
era: tinystr!(16, "ah"), | ||
era_index: Some(0), | ||
year, | ||
ambiguity: types::YearAmbiguity::CenturyRequired, | ||
if year > 0 { | ||
types::EraYear { | ||
era: tinystr!(16, "ah"), | ||
era_index: Some(0), | ||
year, | ||
ambiguity: types::YearAmbiguity::CenturyRequired, | ||
} | ||
} else { | ||
types::EraYear { | ||
era: tinystr!(16, "bh"), | ||
era_index: Some(1), | ||
year: 1 - year, | ||
ambiguity: types::YearAmbiguity::CenturyRequired, | ||
} | ||
} | ||
} | ||
|
||
/// The [simulated Hijri Calendar](https://en.wikipedia.org/wiki/Islamic_calendar) | ||
/// | ||
/// # Era codes | ||
/// | ||
/// This calendar uses a single era code `ah`, Anno Hegirae. Dates before this era use negative years. | ||
/// This calendar uses two era codes: `ah`, and `bh`, corresponding to the Anno Hegirae and Before Hijrah eras | ||
/// | ||
/// # Month codes | ||
/// | ||
|
@@ -79,7 +88,7 @@ impl HijriSimulatedLocation { | |
/// | ||
/// # Era codes | ||
/// | ||
/// This calendar uses a single era code `ah`, Anno Hegirae. Dates before this era use negative years. | ||
/// This calendar uses two era codes: `ah`, and `bh`, corresponding to the Anno Hegirae and Before Hijrah eras | ||
/// | ||
/// # Month codes | ||
/// | ||
|
@@ -97,7 +106,7 @@ pub struct HijriUmmAlQura; | |
/// | ||
/// # Era codes | ||
/// | ||
/// This calendar uses a single era code `ah`, Anno Hegirae. Dates before this era use negative years. | ||
/// This calendar uses two era codes: `ah`, and `bh`, corresponding to the Anno Hegirae and Before Hijrah eras | ||
/// | ||
/// # Month codes | ||
/// | ||
|
@@ -374,7 +383,8 @@ impl Calendar for HijriSimulated { | |
day: u8, | ||
) -> Result<Self::DateInner, DateError> { | ||
let year = match era { | ||
Some("islamic-rgsa" | "ah") | None => year, | ||
Some("ah") | None => year_check(year, 1..)?, | ||
Some("bh") => 1 - year_check(year, 1..)?, | ||
Some(_) => return Err(DateError::UnknownEra), | ||
}; | ||
let Some((month, false)) = month_code.parsed() else { | ||
|
@@ -656,7 +666,8 @@ impl Calendar for HijriUmmAlQura { | |
day: u8, | ||
) -> Result<Self::DateInner, DateError> { | ||
let year = match era { | ||
Some("islamic-umalqura" | "ah") | None => year, | ||
Some("ah") | None => year_check(year, 1..)?, | ||
Some("bh") => 1 - year_check(year, 1..)?, | ||
Comment on lines
+669
to
+670
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a comment explaining why the year is being checked against 1.. and 1-year respectively. This will help future developers understand the logic behind the code. Some("ah") | None => year_check(year, 1..)?, // Years must be positive in AH era
Some("bh") => 1 - year_check(year, 1..)?, // Years must be positive in BH era |
||
Some(_) => return Err(DateError::UnknownEra), | ||
}; | ||
let Some((month, false)) = month_code.parsed() else { | ||
|
@@ -881,9 +892,8 @@ impl Calendar for HijriTabular { | |
day: u8, | ||
) -> Result<Self::DateInner, DateError> { | ||
let year = match era { | ||
Some("ah") | None => year, | ||
Some("islamic-civil" | "islamicc") if self.epoch == HijriTabularEpoch::Friday => year, | ||
Some("islamic-tbla") if self.epoch == HijriTabularEpoch::Thursday => year, | ||
Some("ah") | None => year_check(year, 1..)?, | ||
Some("bh") => 1 - year_check(year, 1..)?, | ||
Comment on lines
+895
to
+896
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a comment explaining why the year is being checked against 1.. and 1-year respectively. This will help future developers understand the logic behind the code. Some("ah") | None => year_check(year, 1..)?, // Years must be positive in AH era
Some("bh") => 1 - year_check(year, 1..)?, // Years must be positive in BH era |
||
Some(_) => return Err(DateError::UnknownEra), | ||
}; | ||
|
||
|
@@ -1893,11 +1903,9 @@ mod test { | |
#[test] | ||
fn test_regression_4914() { | ||
// https://github.com/unicode-org/icu4x/issues/4914 | ||
let cal = HijriUmmAlQura::new(); | ||
let era = "islamic-umalqura"; | ||
let year = -6823; | ||
let month_code = MonthCode(tinystr!(4, "M01")); | ||
let dt = cal.from_codes(Some(era), year, month_code, 1).unwrap(); | ||
let dt = HijriUmmAlQura::new() | ||
.from_codes(Some("bh"), 6824, MonthCode::new_normal(1).unwrap(), 1) | ||
.unwrap(); | ||
assert_eq!(dt.0.day, 1); | ||
assert_eq!(dt.0.month, 1); | ||
assert_eq!(dt.0.year.value, -6823); | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -39,8 +39,8 @@ const ROC_ERA_OFFSET: i32 = 1911; | |||||||||
/// | ||||||||||
/// # Era codes | ||||||||||
/// | ||||||||||
/// This calendar uses two era codes: `minguo`, corresponding to years in the 民國 era (CE year 1912 and | ||||||||||
/// after), and `minguo-qian`, corresponding to years before the 民國 era (CE year 1911 and before). | ||||||||||
/// This calendar uses two era codes: `roc`, corresponding to years in the 民國 era (CE year 1912 and | ||||||||||
/// after), and `broc`, corresponding to years before the 民國 era (CE year 1911 and before). | ||||||||||
/// | ||||||||||
/// | ||||||||||
/// # Month codes | ||||||||||
|
@@ -67,8 +67,8 @@ impl Calendar for Roc { | |||||||||
day: u8, | ||||||||||
) -> Result<Self::DateInner, DateError> { | ||||||||||
let year = match era { | ||||||||||
Some("minguo") | None => ROC_ERA_OFFSET + year_check(year, 1..)?, | ||||||||||
Some("minguo-qian") => ROC_ERA_OFFSET + 1 - year_check(year, 1..)?, | ||||||||||
Some("roc") | None => ROC_ERA_OFFSET + year_check(year, 1..)?, | ||||||||||
Some("broc") => ROC_ERA_OFFSET + 1 - year_check(year, 1..)?, | ||||||||||
Comment on lines
+70
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a comment explaining why the year is being checked against 1.. and 1-year respectively. This will help future developers understand the logic behind the code.
Suggested change
|
||||||||||
Some(_) => return Err(DateError::UnknownEra), | ||||||||||
}; | ||||||||||
|
||||||||||
|
@@ -129,14 +129,14 @@ impl Calendar for Roc { | |||||||||
let extended_year = self.extended_year(date); | ||||||||||
if extended_year > ROC_ERA_OFFSET { | ||||||||||
types::EraYear { | ||||||||||
era: tinystr!(16, "minguo"), | ||||||||||
era: tinystr!(16, "roc"), | ||||||||||
era_index: Some(1), | ||||||||||
year: extended_year.saturating_sub(ROC_ERA_OFFSET), | ||||||||||
ambiguity: types::YearAmbiguity::CenturyRequired, | ||||||||||
} | ||||||||||
} else { | ||||||||||
types::EraYear { | ||||||||||
era: tinystr!(16, "minguo-qian"), | ||||||||||
era: tinystr!(16, "broc"), | ||||||||||
era_index: Some(0), | ||||||||||
year: (ROC_ERA_OFFSET + 1).saturating_sub(extended_year), | ||||||||||
ambiguity: types::YearAmbiguity::EraAndCenturyRequired, | ||||||||||
|
@@ -185,7 +185,7 @@ impl Date<Roc> { | |||||||||
/// let date_roc = Date::try_new_roc(1, 2, 3) | ||||||||||
/// .expect("Failed to initialize ROC Date instance."); | ||||||||||
/// | ||||||||||
/// assert_eq!(date_roc.era_year().era, tinystr!(16, "minguo")); | ||||||||||
/// assert_eq!(date_roc.era_year().era, tinystr!(16, "roc")); | ||||||||||
/// assert_eq!(date_roc.era_year().year, 1, "ROC year check failed!"); | ||||||||||
/// assert_eq!(date_roc.month().ordinal, 2, "ROC month check failed!"); | ||||||||||
/// assert_eq!(date_roc.day_of_month().0, 3, "ROC day of month check failed!"); | ||||||||||
|
@@ -264,7 +264,7 @@ mod test { | |||||||||
iso_month: 1, | ||||||||||
iso_day: 1, | ||||||||||
expected_year: 1, | ||||||||||
expected_era: "minguo", | ||||||||||
expected_era: "roc", | ||||||||||
expected_month: 1, | ||||||||||
expected_day: 1, | ||||||||||
}, | ||||||||||
|
@@ -274,7 +274,7 @@ mod test { | |||||||||
iso_month: 2, | ||||||||||
iso_day: 29, | ||||||||||
expected_year: 1, | ||||||||||
expected_era: "minguo", | ||||||||||
expected_era: "roc", | ||||||||||
expected_month: 2, | ||||||||||
expected_day: 29, | ||||||||||
}, | ||||||||||
|
@@ -284,7 +284,7 @@ mod test { | |||||||||
iso_month: 6, | ||||||||||
iso_day: 30, | ||||||||||
expected_year: 2, | ||||||||||
expected_era: "minguo", | ||||||||||
expected_era: "roc", | ||||||||||
expected_month: 6, | ||||||||||
expected_day: 30, | ||||||||||
}, | ||||||||||
|
@@ -294,7 +294,7 @@ mod test { | |||||||||
iso_month: 7, | ||||||||||
iso_day: 13, | ||||||||||
expected_year: 112, | ||||||||||
expected_era: "minguo", | ||||||||||
expected_era: "roc", | ||||||||||
expected_month: 7, | ||||||||||
expected_day: 13, | ||||||||||
}, | ||||||||||
|
@@ -318,7 +318,7 @@ mod test { | |||||||||
iso_month: 12, | ||||||||||
iso_day: 31, | ||||||||||
expected_year: 1, | ||||||||||
expected_era: "minguo-qian", | ||||||||||
expected_era: "broc", | ||||||||||
expected_month: 12, | ||||||||||
expected_day: 31, | ||||||||||
}, | ||||||||||
|
@@ -328,7 +328,7 @@ mod test { | |||||||||
iso_month: 1, | ||||||||||
iso_day: 1, | ||||||||||
expected_year: 1, | ||||||||||
expected_era: "minguo-qian", | ||||||||||
expected_era: "broc", | ||||||||||
expected_month: 1, | ||||||||||
expected_day: 1, | ||||||||||
}, | ||||||||||
|
@@ -338,7 +338,7 @@ mod test { | |||||||||
iso_month: 12, | ||||||||||
iso_day: 31, | ||||||||||
expected_year: 2, | ||||||||||
expected_era: "minguo-qian", | ||||||||||
expected_era: "broc", | ||||||||||
expected_month: 12, | ||||||||||
expected_day: 31, | ||||||||||
}, | ||||||||||
|
@@ -348,7 +348,7 @@ mod test { | |||||||||
iso_month: 2, | ||||||||||
iso_day: 29, | ||||||||||
expected_year: 4, | ||||||||||
expected_era: "minguo-qian", | ||||||||||
expected_era: "broc", | ||||||||||
expected_month: 2, | ||||||||||
expected_day: 29, | ||||||||||
}, | ||||||||||
|
@@ -358,7 +358,7 @@ mod test { | |||||||||
iso_month: 1, | ||||||||||
iso_day: 1, | ||||||||||
expected_year: 1911, | ||||||||||
expected_era: "minguo-qian", | ||||||||||
expected_era: "broc", | ||||||||||
expected_month: 1, | ||||||||||
expected_day: 1, | ||||||||||
}, | ||||||||||
|
@@ -368,7 +368,7 @@ mod test { | |||||||||
iso_month: 12, | ||||||||||
iso_day: 31, | ||||||||||
expected_year: 1912, | ||||||||||
expected_era: "minguo-qian", | ||||||||||
expected_era: "broc", | ||||||||||
expected_month: 12, | ||||||||||
expected_day: 31, | ||||||||||
}, | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a comment explaining why the year is being checked against 1.. and 1-year respectively. This will help future developers understand the logic behind the code.