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 1 commit
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
2 changes: 1 addition & 1 deletion components/calendar/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ 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> {
pub fn get_normal_if_leap(&self) -> Option<MonthCode> {
Copy link
Member

Choose a reason for hiding this comment

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

nit: MonthCode is Copy, this should just take self

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

let bytes = self.0.all_bytes();
if bytes[3] == b'L' {
Some(MonthCode(TinyAsciiStr::from_bytes(&bytes[0..3]).ok()?))
Expand Down
18 changes: 17 additions & 1 deletion components/datetime/src/format/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,26 @@ where
.month()
.ok_or(Error::MissingInputField(Some("month")))?
.code;

let symbols = date_symbols
.ok_or(Error::MissingDateSymbols)?
.get_symbols_for_month(month, length, code)?;
let symbol = symbols.get(code).ok_or(Error::MissingMonthSymbol(code))?;

let symbol_option = symbols.get(code); //.ok_or(Error::MissingMonthSymbol(code))?;
let symbol = if symbol_option.is_none() {
Copy link
Member

Choose a reason for hiding this comment

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

I would use if let Some(symbol) = symbol_option {} else {}

I would also put the write_str call inside the if let, so no need to let symbol = . This way the else branch can pick which order to print things in (and we probably want to output the (leap) after symbol)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I put write_str directly inside the if statement, so there is no more need for symbol or an if let

let code = code
.get_normal_if_leap()
.ok_or(Error::MissingMonthSymbol(code))?;
let symbols = date_symbols
.ok_or(Error::MissingDateSymbols)?
.get_symbols_for_month(month, length, code)?;
let symbol = symbols.get(code).ok_or(Error::MissingMonthSymbol(code))?;
w.write_str("(leap)")?;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Add a // TODO(#1234) comment pointing to the issue number

symbol
} else {
symbol_option.ok_or(Error::MissingMonthSymbol(code))?
};

w.write_str(symbol)?
}
},
Expand Down
23 changes: 23 additions & 0 deletions components/datetime/tests/fixtures/tests/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@
}
}
},
{
"input": {
"value": "2023-03-23T14:15:07.123",
"options": {
"components": {
"weekday": "long",
"month": "long",
"day": "two-digit-day-of-month",
"era": "long",
"year": "numeric",
"hour": "numeric",
"minute": "numeric",
"second": "numeric"
}
}
},
"output": {
"values": {
"en-u-ca-chinese": "Thursday, (leap)Second Month 02, 2023((cyclic year: 40)), 14:15:07",
"zh-u-ca-chinese": "2023(cyclic year: 40)年(leap)二月02星期四 14:15:07"
}
}
},
{
"input": {
"value": "2022-05-03T14:15:07.123",
Expand Down
30 changes: 1 addition & 29 deletions provider/datagen/src/transform/cldr/datetime/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,11 @@ fn get_month_code_map(calendar: &str) -> &'static [TinyStr4] {
tinystr!(4, "M13"),
];

static LUNAR_MONTH_CODES: &[TinyStr4] = &[
tinystr!(4, "M01"),
tinystr!(4, "M02"),
tinystr!(4, "M03"),
tinystr!(4, "M04"),
tinystr!(4, "M05"),
tinystr!(4, "M06"),
tinystr!(4, "M07"),
tinystr!(4, "M08"),
tinystr!(4, "M09"),
tinystr!(4, "M10"),
tinystr!(4, "M11"),
tinystr!(4, "M12"),
tinystr!(4, "M01L"),
tinystr!(4, "M02L"),
tinystr!(4, "M03L"),
tinystr!(4, "M04L"),
tinystr!(4, "M05L"),
tinystr!(4, "M06L"),
tinystr!(4, "M07L"),
tinystr!(4, "M08L"),
tinystr!(4, "M09L"),
tinystr!(4, "M10L"),
tinystr!(4, "M11L"),
tinystr!(4, "M12L"),
];

match calendar {
"gregory" | "buddhist" | "japanese" | "japanext" | "indian" | "persian" | "roc" => {
&SOLAR_MONTH_CODES[0..12]
}
"coptic" | "ethiopic" => SOLAR_MONTH_CODES,
"chinese" => LUNAR_MONTH_CODES,
"coptic" | "ethiopic" | "chinese" => SOLAR_MONTH_CODES,
Manishearth marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Please add a TODO comment here with the issue number for fixing leap month formatting

_ => panic!("Month map unknown for {calendar}"),
}
}
Expand Down