Repository navigation
fix(date): use proleptic Gregorian leap years for BC dates and gregorian-based calendars - #10979
waw666waw666 wants to merge 1 commit into
Conversation
…calendars GregorianCalendar checked leap years against the era year, so BC years were treated as leap when their era year was divisible by 4 (4 BC, 8 BC, ...) and real leap years (1 BC, 5 BC, ...) were not. This broke fromJulianDay, getDaysInMonth and getDaysInYear for BC dates. Buddhist, Taiwan and Japanese calendars also inherited getDaysInYear without converting their year to a gregorian year first.
|
You need to sign the CLA This word "proleptic" is in the title and never mentioned again, please explain. Testing description section isn't about what unit tests to run, it's about how a human can verify the code while reviewing the PR. Please update it accordingly.
you should be able to do this, especially as a human, they don't take very long. good news, the CI ran them, they are passing. |
|
Thanks for the review. I've signed the CLA, added a sentence to the summary explaining "proleptic" (the Gregorian leap-year rule extended back to BC dates with astronomical year numbering, as |
|
try to get CLA to take hold |
Closes N/A (no existing issue)
Summary
GregorianCalendardecides leap years from the era year (date.year) instead of the extended (astronomical) year thatgregorianToJulianDayalready uses. In the BC era those are different numbers: 1 BC is year 0, 5 BC is year -4. So in BC every year whose era number is divisible by 4 (4 BC, 8 BC, ...) is treated as leap, and the real leap years (1 BC, 5 BC, ...) are not."Proleptic" here means extending the Gregorian leap-year rule backwards to dates before the calendar existed, using astronomical year numbering (1 BC = year 0). That is what
gregorianToJulianDayand ISO 8601 / Temporal already do; this PR makes the rest ofGregorianCalendarfollow the same rule.Because
toJulianDaydoes use the extended year, the two directions disagree and BC dates come out wrong:Walking every day from 10 BC to 10 AD,
toJulianDay(fromJulianDay(jd)) !== jdfor about 10 days in each affected BC year, and some days map to the same date twice.Separately,
BuddhistCalendar,TaiwanCalendarandJapaneseCalendaroverridegetDaysInMonthto convert to a gregorian year first, but they inheritgetDaysInYearunchanged, so it runs the leap-year rule on the Buddhist/Minguo/era year:Same for Taiwan (Minguo 113) and Japanese (Reiwa 6).
Fix:
GregorianCalendar.fromJulianDay,getDaysInMonthandgetDaysInYearuse the extended year for the leap-year check, matchinggregorianToJulianDay. AD dates are unchanged since the extended year equals the era year there.BuddhistCalendar,TaiwanCalendarandJapaneseCalendaroverridegetDaysInYearthe same way they already overridegetDaysInMonth.📝 Test Instructions
To verify by hand, check out this branch, run
yarn install, then open a Node REPL or a scratch file that imports frompackages/@internationalized/date/src:new CalendarDate('BC', 1, 2, 28).add({days: 1}).toString(). Onmainthis gives March 1 (1 BC treated as a common year); on this branch it gives February 29, 1 BC.new CalendarDate('BC', 40, 1, 1).add({days: 59}). Onmainthis lands on February 29 of 40 BC, which does not exist; on this branch it lands on March 1.let cal = new GregorianCalendar(),cal.fromJulianDay(cal.toJulianDay(new CalendarDate('BC', 1, 2, 28)) + 1)should return BC 1-02-29, not BC 1-02-28 again.new BuddhistCalendar().getDaysInYear(toCalendar(new CalendarDate(2024, 6, 1), new BuddhistCalendar()))should return 366 (2024 is a leap year). Same check withTaiwanCalendarandJapaneseCalendar.new CalendarDate(2024, 2, 28).add({days: 1})is still 2024-02-29 andnew CalendarDate(2023, 2, 28).add({days: 1})is still 2023-03-01.You can also cross-check any of these against
@js-temporal/polyfill(ISO calendar for BC dates,buddhist/roc/japanesefor the others), which is how I found the bug.New unit tests in
conversion.test.jsandmanipulation.test.jscover the same cases; CI runs the fullyarn testandyarn lint.Release impact
✅ PR Checklist
AI disclosure: I used an AI assistant to help find and write this fix; I reviewed and tested every change.
🧢 Your Project
Personal