Skip to content
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

Implement formatting for Chinese calendar with some todos #3760

Merged
merged 18 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions components/calendar/src/any_calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ macro_rules! match_cal_and_date {
) => $e,
(&Self::Ethiopian(ref $cal_matched), &AnyDateInner::Ethiopian(ref $date_matched)) => $e,
(&Self::Indian(ref $cal_matched), &AnyDateInner::Indian(ref $date_matched)) => $e,
(&Self::Chinese(ref $cal_matched), &AnyDateInner::Chinese(ref $date_matched)) => $e,
(&Self::Persian(ref $cal_matched), &AnyDateInner::Persian(ref $date_matched)) => $e,
(&Self::Coptic(ref $cal_matched), &AnyDateInner::Coptic(ref $date_matched)) => $e,
(&Self::Roc(ref $cal_matched), &AnyDateInner::Roc(ref $date_matched)) => $e,
Expand Down Expand Up @@ -254,6 +255,9 @@ impl Calendar for AnyCalendar {
(Self::Indian(c), &mut AnyDateInner::Indian(ref mut d)) => {
c.offset_date(d, offset.cast_unit())
}
(Self::Chinese(c), &mut AnyDateInner::Chinese(ref mut d)) => {
c.offset_date(d, offset.cast_unit())
}
(Self::Coptic(c), &mut AnyDateInner::Coptic(ref mut d)) => {
c.offset_date(d, offset.cast_unit())
}
Expand Down Expand Up @@ -333,6 +337,14 @@ impl Calendar for AnyCalendar {
) => c1
.until(d1, d2, c2, largest_unit, smallest_unit)
.cast_unit(),
(
Self::Chinese(c1),
Self::Chinese(c2),
AnyDateInner::Chinese(d1),
AnyDateInner::Chinese(d2),
) => c1
.until(d1, d2, c2, largest_unit, smallest_unit)
.cast_unit(),
(
Self::Persian(c1),
Self::Persian(c2),
Expand Down Expand Up @@ -724,6 +736,7 @@ impl AnyCalendarKind {
b"japanese" => AnyCalendarKind::Japanese,
b"japanext" => AnyCalendarKind::JapaneseExtended,
b"indian" => AnyCalendarKind::Indian,
b"chinese" => AnyCalendarKind::Chinese,
b"coptic" => AnyCalendarKind::Coptic,
b"iso" => AnyCalendarKind::Iso,
b"ethiopic" => AnyCalendarKind::Ethiopian,
Expand Down Expand Up @@ -752,6 +765,8 @@ impl AnyCalendarKind {
AnyCalendarKind::JapaneseExtended
} else if *x == value!("indian") {
AnyCalendarKind::Indian
} else if *x == value!("chinese") {
AnyCalendarKind::Chinese
} else if *x == value!("coptic") {
AnyCalendarKind::Coptic
} else if *x == value!("iso") {
Expand Down Expand Up @@ -953,6 +968,18 @@ impl IntoAnyCalendar for Indian {
}
}

impl IntoAnyCalendar for Chinese {
fn to_any(self) -> AnyCalendar {
AnyCalendar::Chinese(Chinese)
}
fn to_any_cloned(&self) -> AnyCalendar {
AnyCalendar::Chinese(Chinese)
}
fn date_to_any(&self, d: &Self::DateInner) -> AnyDateInner {
AnyDateInner::Chinese(*d)
}
}

impl IntoAnyCalendar for Coptic {
fn to_any(self) -> AnyCalendar {
AnyCalendar::Coptic(Coptic)
Expand Down Expand Up @@ -1082,6 +1109,7 @@ mod tests {
let ethioaa = AnyCalendar::new(AnyCalendarKind::EthiopianAmeteAlem);
let gregorian = AnyCalendar::new(AnyCalendarKind::Gregorian);
let indian = AnyCalendar::new(AnyCalendarKind::Indian);
let chinese = AnyCalendar::new(AnyCalendarKind::Chinese);
let japanese = AnyCalendar::new(AnyCalendarKind::Japanese);
let japanext = AnyCalendar::new(AnyCalendarKind::JapaneseExtended);
let persian = AnyCalendar::new(AnyCalendarKind::Persian);
Expand All @@ -1092,6 +1120,7 @@ mod tests {
let ethioaa = Ref(&ethioaa);
let gregorian = Ref(&gregorian);
let indian = Ref(&indian);
let chinese = Ref(&chinese);
let japanese = Ref(&japanese);
let japanext = Ref(&japanext);
let persian = Ref(&persian);
Expand Down Expand Up @@ -1188,6 +1217,19 @@ mod tests {
1,
CalendarError::UnknownMonthCode("M13".parse().unwrap(), "Indian"),
);

single_test_roundtrip(chinese, "chinese", 400, "M02", 5);
single_test_roundtrip(chinese, "chinese", 4660, "M07", 29);
single_test_roundtrip(chinese, "chinese", -100, "M11", 12);
single_test_error(
chinese,
"chinese",
4658,
"M13",
1,
CalendarError::UnknownMonthCode("M13".parse().unwrap(), "Chinese"),
);

single_test_roundtrip(japanese, "reiwa", 3, "M03", 1);
single_test_roundtrip(japanese, "heisei", 6, "M12", 1);
single_test_roundtrip(japanese, "meiji", 10, "M03", 1);
Expand Down
30 changes: 30 additions & 0 deletions components/calendar/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use core::convert::TryInto;
use core::fmt;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::str::FromStr;
use tinystr::TinyAsciiStr;
use tinystr::{TinyStr16, TinyStr4};
use zerovec::maps::ZeroMapKV;
use zerovec::ule::AsULE;
Expand Down Expand Up @@ -92,6 +93,35 @@ impl FormattableYear {
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct MonthCode(pub TinyStr4);

impl MonthCode {
/// Returns an option which is Some containing the non-month version of a leap month
/// if the MonthCode this method is called upon is a leap month, and None otherwise.
/// This method assumes the MonthCode is valid.
pub fn get_normal_if_leap(self) -> Option<MonthCode> {
let bytes = self.0.all_bytes();
if bytes[3] == b'L' {
Some(MonthCode(TinyAsciiStr::from_bytes(&bytes[0..3]).ok()?))
} else {
None
}
Comment on lines +102 to +106
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: how does this code not create a clippy::indexing_slicing failure? Is Clippy smart enough to know that this is a [u8; 4] and that the indices are in range?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

}
}

#[test]
fn test_get_normal_month_code_if_leap() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: thanks for writing a test!

let mc1 = MonthCode(tinystr::tinystr!(4, "M01L"));
let result1 = mc1.get_normal_if_leap();
assert_eq!(result1, Some(MonthCode(tinystr::tinystr!(4, "M01"))));

let mc2 = MonthCode(tinystr::tinystr!(4, "M11L"));
let result2 = mc2.get_normal_if_leap();
assert_eq!(result2, Some(MonthCode(tinystr::tinystr!(4, "M11"))));

let mc_invalid = MonthCode(tinystr::tinystr!(4, "M10"));
let result_invalid = mc_invalid.get_normal_if_leap();
assert_eq!(result_invalid, None);
}

impl AsULE for MonthCode {
type ULE = TinyStr4;
fn to_unaligned(self) -> TinyStr4 {
Expand Down
2 changes: 2 additions & 0 deletions components/datetime/data/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"Explicit": [
"datetime/buddhist/datelengths@1",
"datetime/buddhist/datesymbols@1",
"datetime/chinese/datelengths@1",
"datetime/chinese/datesymbols@1",
"datetime/coptic/datelengths@1",
"datetime/coptic/datesymbols@1",
"datetime/ethiopic/datelengths@1",
Expand Down
10 changes: 10 additions & 0 deletions components/datetime/data/data/macros.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading