Skip to content

Commit 7df4237

Browse files
Fix any calendar checks (#6398)
`ZonedDateTime` has not been enforcing `AnyCalendarKind`s since [#5150](https://github.com/unicode-org/icu4x/pull/5150/files#diff-a4f5d62814d01dd02fae0ad0975c4e7a978e185454bced0d1ea4830ff34829f5R114) added formatting for it. #6384 then introduced a bug that wasn't caught by tests.
1 parent 602093a commit 7df4237

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

components/calendar/src/any_calendar.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,12 @@ impl From<Indian> for AnyCalendar {
12061206
impl IntoAnyCalendar for HijriTabular {
12071207
#[inline]
12081208
fn to_any(self) -> AnyCalendar {
1209-
AnyCalendar::HijriTabularCivil(self)
1209+
match self.0 {
1210+
calendrical_calculations::islamic::ISLAMIC_EPOCH_FRIDAY => {
1211+
AnyCalendar::HijriTabularCivil(self)
1212+
}
1213+
_ => AnyCalendar::HijriTabularAstronomical(self),
1214+
}
12101215
}
12111216
#[inline]
12121217
fn kind(&self) -> AnyCalendarKind {
@@ -1219,23 +1224,32 @@ impl IntoAnyCalendar for HijriTabular {
12191224
}
12201225
#[inline]
12211226
fn from_any(any: AnyCalendar) -> Result<Self, AnyCalendar> {
1222-
if let AnyCalendar::HijriTabularCivil(cal) = any {
1227+
if let AnyCalendar::HijriTabularCivil(cal) | AnyCalendar::HijriTabularAstronomical(cal) =
1228+
any
1229+
{
12231230
Ok(cal)
12241231
} else {
12251232
Err(any)
12261233
}
12271234
}
12281235
#[inline]
12291236
fn from_any_ref(any: &AnyCalendar) -> Option<&Self> {
1230-
if let AnyCalendar::HijriTabularCivil(cal) = any {
1237+
if let AnyCalendar::HijriTabularCivil(cal) | AnyCalendar::HijriTabularAstronomical(cal) =
1238+
any
1239+
{
12311240
Some(cal)
12321241
} else {
12331242
None
12341243
}
12351244
}
12361245
#[inline]
12371246
fn date_to_any(&self, d: &Self::DateInner) -> AnyDateInner {
1238-
AnyDateInner::HijriTabularCivil(*d)
1247+
match self.0 {
1248+
calendrical_calculations::islamic::ISLAMIC_EPOCH_FRIDAY => {
1249+
AnyDateInner::HijriTabularCivil(*d)
1250+
}
1251+
_ => AnyDateInner::HijriTabularAstronomical(*d),
1252+
}
12391253
}
12401254
}
12411255

components/datetime/src/scaffold/calendar.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -748,21 +748,17 @@ impl<C: Calendar, A: AsCalendar<Calendar = C>> InSameCalendar for DateTime<A> {
748748
&self,
749749
any_calendar_kind: AnyCalendarKind,
750750
) -> Result<(), MismatchedCalendarError> {
751-
if self.date.calendar().any_calendar_kind() == Some(any_calendar_kind) {
752-
Ok(())
753-
} else {
754-
Err(MismatchedCalendarError {
755-
this_kind: any_calendar_kind,
756-
date_kind: self.date.calendar().any_calendar_kind(),
757-
})
758-
}
751+
self.date.check_any_calendar_kind(any_calendar_kind)
759752
}
760753
}
761754

762755
impl<C: Calendar, A: AsCalendar<Calendar = C>, Z> InSameCalendar for ZonedDateTime<A, Z> {
763756
#[inline]
764-
fn check_any_calendar_kind(&self, _: AnyCalendarKind) -> Result<(), MismatchedCalendarError> {
765-
Ok(())
757+
fn check_any_calendar_kind(
758+
&self,
759+
any_calendar_kind: AnyCalendarKind,
760+
) -> Result<(), MismatchedCalendarError> {
761+
self.date.check_any_calendar_kind(any_calendar_kind)
766762
}
767763
}
768764

components/datetime/tests/datetime.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use icu_calendar::cal::{
1111
HijriUmmAlQura, Indian, Iso, Persian, Roc, {Ethiopian, EthiopianEraStyle},
1212
{Japanese, JapaneseExtended},
1313
};
14+
use icu_calendar::AnyCalendarKind;
1415
use icu_datetime::fieldsets::enums::*;
1516
use icu_datetime::scaffold::CldrCalendar;
1617
use icu_datetime::{
@@ -238,32 +239,38 @@ fn assert_fixture_element<C>(
238239
let any_dtf = DateTimeFormatter::try_new(prefs, field_set).expect(description);
239240

240241
let output = dtf.format(&input);
241-
let any_output = any_dtf.format_same_calendar(&any_input).unwrap();
242-
let iso_any_output = any_dtf.format(&iso_any_input);
243-
244242
assert_writeable_eq!(output, expected.expectation(), "{}", description);
243+
let pattern = output.pattern();
244+
245+
if let Some(expected_pattern) = expected.pattern() {
246+
assert_writeable_eq!(pattern, expected_pattern);
247+
}
248+
249+
if matches!(
250+
input.date.calendar().kind(),
251+
AnyCalendarKind::JapaneseExtended
252+
) {
253+
// Not supported with FormattableAnyCalendar
254+
return;
255+
}
245256

257+
let any_output = any_dtf.format_same_calendar(&any_input).unwrap();
246258
assert_writeable_eq!(
247259
any_output,
248260
expected.expectation(),
249261
"(DateTimeFormatter) {}",
250262
description
251263
);
264+
assert_eq!(pattern, any_output.pattern());
252265

266+
let iso_any_output = any_dtf.format(&iso_any_input);
253267
assert_writeable_eq!(
254268
iso_any_output,
255269
expected.expectation(),
256270
"(DateTimeFormatter iso conversion) {}",
257271
description
258272
);
259-
260-
let pattern = output.pattern();
261-
assert_eq!(pattern, any_output.pattern());
262273
assert_eq!(pattern, iso_any_output.pattern());
263-
264-
if let Some(expected_pattern) = expected.pattern() {
265-
assert_writeable_eq!(pattern, expected_pattern);
266-
}
267274
}
268275

269276
fn test_fixture_with_time_zones(fixture_name: &str, file: &str) {

components/datetime/tests/fixtures/tests/components.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"zh-u-ca-chinese-hc-h23": "2019己亥年腊月27星期二 08:25:07",
3030
"en-u-ca-japanese-hc-h23": "Tuesday, January 21, 2 Reiwa, 08:25:07",
3131
"ja-u-ca-japanese-hc-h23": "令和2年1月21日火曜日 8:25:07",
32+
"en-u-ca-japanese-hc-h23-x-extended": "Tuesday, January 21, 2 Reiwa, 08:25:07",
33+
"ja-u-ca-japanese-hc-h23-x-extended": "令和2年1月21日火曜日 8:25:07",
3234
"en-u-ca-coptic-hc-h23": "Tuesday, Toba 12, 1736 ERA1, 08:25:07",
3335
"fr-u-ca-coptic-hc-h23": "mardi 12 toubah 1736 ap. D., 08:25:07",
3436
"en-u-ca-dangi-hc-h23": "Tuesday, Twelfth Month 27, 2019(ji-hai), 08:25:07",

0 commit comments

Comments
 (0)