Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
arshaw opened this issue Apr 16, 2024 · 1 comment
Assignees

Comments

@arshaw
Copy link
Contributor

arshaw commented Apr 16, 2024

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);

@ptomato
Copy link
Collaborator

ptomato commented Jun 13, 2024

Closed by #2838.

@ptomato ptomato closed this as completed Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants