unicode-org / icu4x

Solving i18n for client-side and resource-constrained environments.
https://icu4x.unicode.org
Other
1.37k stars 175 forks source link

Indian calendar can return ISO-date with zero day_of_month #5070

Open anba opened 4 months ago

anba commented 4 months ago

The computed ISO date should be 2000-12-31, but instead it returns 2001-01-00.

Not a regression from release 1.5, also reproducible with release 1.4.

Test case:

use icu::calendar::Date;
use icu::calendar::Calendar;
use icu::calendar::indian::Indian;

fn main() {
  let cal = Indian;

  let iso = Date::try_new_iso_date(2000, 12, 31).unwrap();
  let date = cal.date_from_iso(iso);

  println!("year = {}", cal.year(&date).number);
  println!("month = {}", cal.month(&date).ordinal);
  println!("day = {}", cal.day_of_month(&date).0);

  let iso = cal.date_to_iso(&date);

  println!("iso.year = {}", iso.year().number);
  println!("iso.month = {}", iso.month().ordinal);
  println!("iso.day = {}", iso.day_of_month().0);
}
anba commented 4 months ago

Possibly caused by this line: https://github.com/unicode-org/icu4x/blob/6d65cb511267b5f7b91fd18db120a37f44ddf615/components/calendar/src/indian.rs#L153

Changing the test condition from >= days_in_year to > days_in_year could fix this bug.

Manishearth commented 4 months ago

cc @sffc