Skip to content

PlainDateTime::until/since incorrect results near month boundaries when time-diff has opposite sign #2820

@arshaw

Description

@arshaw

Recreation:

pdt1 = Temporal.PlainDateTime.from('2023-02-28T03:00:00')
pdt2 = Temporal.PlainDateTime.from('2023-04-01T02:00:00')
dur = pdt1.until(pdt2, { largestUnit: 'year' })
// ACTUAL: { months: 1, hours: 23 }
// EXPECTED: { months: 1, days: 3, hours: 23 }

// why ACTUAL is wrong?
pdt1.add({ months: 1, hours: 23 }).toString()
// 2023-03-29T02:00:00 (does NOT match pdt2)

// why EXPECTED is correct?
pdt1.add({ months: 1, days: 3, hours: 23 }).toString()
// 2023-04-01T02:00:00 (DOES match pdt2)

Happening because, when there's a time-sign mismatch, the origin date is incorrectly progressed 1 day forward instead of the destination date being regressed 1 day back before proceeding with the date-diff.

const timeSign = timeDuration.sign();
const dateSign = CompareISODate(y2, mon2, d2, y1, mon1, d1);
if (dateSign === -timeSign) {
({ year: y1, month: mon1, day: d1 } = BalanceISODate(y1, mon1, d1 - timeSign));
timeDuration = timeDuration.add24HourDays(-timeSign);
}
const date1 = CreateTemporalDate(y1, mon1, d1, calendarRec.receiver);
const date2 = CreateTemporalDate(y2, mon2, d2, calendarRec.receiver);

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions