Skip to content

Commit

Permalink
Handle dates in year 99 rolling over into year 100 behaving as if yea…
Browse files Browse the repository at this point in the history
…r 100 was a leap year (#1390)

Fix #1389
  • Loading branch information
diesieben07 committed Feb 27, 2023
1 parent e781052 commit c5a6b0a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/impl/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ export function objToLocalTS(obj) {
// for legacy reasons, years between 0 and 99 are interpreted as 19XX; revert that
if (obj.year < 100 && obj.year >= 0) {
d = new Date(d);
d.setUTCFullYear(d.getUTCFullYear() - 1900);
// set the month and day again, this is necessary because year 2000 is a leap year, but year 100 is not
// so if obj.year is in 99, but obj.day makes it roll over into year 100,
// the calculations done by Date.UTC are using year 2000 - which is incorrect
d.setUTCFullYear(obj.year, obj.month - 1, obj.day);
}
return +d;
}
Expand Down
7 changes: 7 additions & 0 deletions test/datetime/math.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ test("DateTime#plus works across the 100 barrier", () => {
expect(d.day).toBe(2);
});

test("DateTime#plus works across the 100 barrier when passing through February", () => {
const d = DateTime.fromISO("0099-12-31").plus({ day: 61 });
expect(d.year).toBe(100);
expect(d.month).toBe(3);
expect(d.day).toBe(2);
});

test("DateTime#plus renders invalid when out of max. datetime range using days", () => {
const d = DateTime.utc(1970, 1, 1, 0, 0, 0, 0).plus({ day: 1e8 + 1 });
expect(d.isValid).toBe(false);
Expand Down

0 comments on commit c5a6b0a

Please sign in to comment.