Skip to content

Commit 9844a29

Browse files
Add full support for U datetime symbol (#7328)
1 parent f16ea51 commit 9844a29

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

components/datetime/src/format/datetime.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,35 @@ where
175175
(FieldSymbol::Year(Year::Cyclic), l) => {
176176
const PART: Part = parts::YEAR_NAME;
177177
input!(PART, Year, year = input.year);
178-
input!(PART, YearCyclic, cyclic = year.cyclic());
178+
179+
let Some(cyclic) = year.cyclic() else {
180+
w.with_part(PART, |w| {
181+
try_write_number(
182+
Part::ERROR,
183+
w,
184+
decimal_formatter,
185+
year.era_year_or_related_iso().into(),
186+
FieldLength::One,
187+
)
188+
.map(|_| ())
189+
})?;
190+
return Ok(Err(FormattedDateTimePatternError::UnsupportedField(
191+
ErrorField(field),
192+
)));
193+
};
179194

180195
match datetime_names.get_name_for_cyclic(l, cyclic.year) {
181196
Ok(name) => Ok(w.with_part(PART, |w| w.write_str(name))?),
182197
Err(e) => {
183198
w.with_part(PART, |w| {
184-
w.with_part(Part::ERROR, |w| {
185-
try_write_number_without_part(
186-
w,
187-
decimal_formatter,
188-
cyclic.related_iso.into(),
189-
FieldLength::One,
190-
)
191-
.map(|_| ())
192-
})
199+
try_write_number(
200+
Part::ERROR,
201+
w,
202+
decimal_formatter,
203+
cyclic.related_iso.into(),
204+
FieldLength::One,
205+
)
206+
.map(|_| ())
193207
})?;
194208
return Ok(Err(match e {
195209
GetNameForCyclicYearError::InvalidYearNumber { max } => {

components/datetime/src/pattern/formatter.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,10 @@ impl TryWriteable for FormattedDateTimePattern<'_> {
257257
#[cfg(test)]
258258
#[cfg(feature = "compiled_data")]
259259
mod tests {
260+
use crate::provider::fields::Field;
261+
260262
use super::super::*;
261-
use icu_calendar::{Date, Gregorian};
263+
use icu_calendar::{cal::KoreanTraditional, Date, Gregorian};
262264
use icu_locale_core::locale;
263265
use icu_time::{DateTime, Time};
264266
use writeable::assert_try_writeable_eq;
@@ -606,4 +608,46 @@ mod tests {
606608
assert_try_writeable_eq!(formatted_pattern, expected, Ok(()), "{cas:?}");
607609
}
608610
}
611+
612+
#[test]
613+
fn test_cyclic_year() {
614+
use crate::provider::fields::{FieldLength, FieldSymbol, Year};
615+
616+
let locale = locale!("uk").into();
617+
let pattern: DateTimePattern = "UUUU".parse().unwrap();
618+
619+
let names: FixedCalendarDateTimeNames<Gregorian> =
620+
FixedCalendarDateTimeNames::try_new(locale).unwrap();
621+
let datetime = DateTime {
622+
date: Date::try_new_gregorian(2023, 11, 17).unwrap(),
623+
time: Time::try_new(13, 41, 28, 0).unwrap(),
624+
};
625+
626+
assert_try_writeable_eq!(
627+
names.with_pattern_unchecked(&pattern).format(&datetime),
628+
"2023",
629+
Err(FormattedDateTimePatternError::UnsupportedField(ErrorField(
630+
Field {
631+
symbol: FieldSymbol::Year(Year::Cyclic),
632+
length: FieldLength::Four
633+
}
634+
))),
635+
);
636+
637+
let mut names: FixedCalendarDateTimeNames<KoreanTraditional> =
638+
FixedCalendarDateTimeNames::try_new(locale).unwrap();
639+
names
640+
.load_year_names(&crate::provider::Baked, YearNameLength::Wide)
641+
.unwrap();
642+
let datetime = DateTime {
643+
date: datetime.date.to_calendar(KoreanTraditional::new()),
644+
time: datetime.time,
645+
};
646+
647+
assert_try_writeable_eq!(
648+
names.with_pattern_unchecked(&pattern).format(&datetime),
649+
"gui-mao",
650+
Ok(()),
651+
);
652+
}
609653
}

0 commit comments

Comments
 (0)