Skip to content

Commit

Permalink
Fix conversion of YearMonth/MonthDay in DateTimeFormat polyfill
Browse files Browse the repository at this point in the history
Since MonthDay.withYear(year) is not guaranteed to be sufficient to
convert to a Temporal.Date if the calendar uses eras as well as years.
Instead of picking the arbitrary day 1 or year 2004 to convert, use the
refISODay or refISOYear, respectively.
  • Loading branch information
ptomato committed Jun 10, 2020
1 parent 81bbc75 commit 1e1416f
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions polyfill/lib/intl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,13 @@ function extractOverrides(datetime, main) {
formatter = main[TIME];
}
if (datetime instanceof YearMonth) {
datetime = datetime.withDay(1);
const { year, month, day } = datetime.getISOCalendarFields();
datetime = new Date(year, month, day, datetime.calendar);
formatter = main[YM];
}
if (datetime instanceof MonthDay) {
datetime = datetime.withYear(2004); // use a leap-year for maximum range
const { year, month, day } = datetime.getISOCalendarFields();
datetime = new Date(year, month, day, datetime.calendar);
formatter = main[MD];
}
if (datetime instanceof Date) {
Expand Down

0 comments on commit 1e1416f

Please sign in to comment.