-
Notifications
You must be signed in to change notification settings - Fork 15
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
Month doesn't equal itself when one is a result of adding or subtracting to a month before, but only in Node.js #21
Comments
This code right here seems to be the culprit, I'm not sure though: https://github.com/fullcalendar/temporal/blob/8ddf7a709375bf1586587512968094daaa525f43/packages/temporal-polyfill/src/public/plainYearMonth.ts#L189C1-L193C5 The "official" docs state:
So it's probably not a good idea to convert the month to a |
Unfortunately, const a = Temporal.PlainYearMonth.from({ year: 2023, month: 5 }).subtract({ months: 1 });
const b = Temporal.PlainYearMonth.from({ year: 2023, month: 4 });
Temporal.PlainYearMonth.compare(a, b) // => 1 (should be 0)
a.equals(b) // => false (should be true) The problem seems to be that, as you stated, PlainDate is used internally for probably this use case: const a = Temporal.PlainYearMonth.from({ year: 2023, month: 11 });
a.subtract({ days: 29 }) // => "2023-11"
a.subtract({ days: 30 }) // => "2023-10" which would be fine, if the resulting const a = Temporal.PlainYearMonth.from({ year: 2023, month: 5 });
a.getISOFields()
// => { isoYear: 2023, isoMonth: 5, isoDay: 1, calendar: isoCalendar }
a.subtract({ days: 10 }).getISOFields()
// => { isoYear: 2023, isoMonth: 5, isoDay: 21, calendar: isoCalendar } This also seems the difference between PlainYearMonth in this polyfill and the one on the tc39 proposal page: const thisPolyfill = await import('temporal-polyfill');
const otherPolyfill = await import('@js-temporal/polyfill');
const a = thisPolyfill.Temporal.PlainYearMonth.from({ year: 2023, month: 11 }).subtract({ days: 10 }).getISOFields();
const b = otherPolyfill.Temporal.PlainYearMonth.from({ year: 2023, month: 11 }).subtract({ days: 10 }).getISOFields();
console.log(a) // => { isoYear: 2023, isoMonth: 11, isoDay: 20, calendar: <Temporal.Calendar> }
console.log(b) // => { isoYear: 2023, isoMonth: 11, isoDay: 1, calendar: "iso8601" } FixThis should either be an easy fix in PlainYearMonth itself: export class PlainYearMonth extends AbstractISOObj<Temporal.PlainDateISOFields>
implements Temporal.PlainYearMonth {
constructor(
isoYear: number,
isoMonth: number,
calendarArg: Temporal.CalendarLike = createDefaultCalendar(),
referenceISODay = 1,
) {
const constrained = constrainDateISO({
isoYear,
isoMonth,
isoDay: referenceISODay,
}, OVERFLOW_REJECT)
const calendar = ensureObj(Calendar, calendarArg)
validateYearMonth(constrained, calendar.toString())
super({
...constrained,
+ day: 1,
calendar,
})
} Workaround / monkey-patchor can be fixed in userland until the current in-progress-huge-refactor is done: // https://github.com/fullcalendar/temporal/issues/21
export function monkeyPatchTemporalPolyfillIssue21() {
const yearMonth = new Temporal.PlainYearMonth(2000, 1);
if (!yearMonth.equals(yearMonth.subtract({ days: 1 }))) {
const originalGetISOFields = Temporal.PlainYearMonth.prototype.getISOFields;
Temporal.PlainYearMonth.prototype.getISOFields = function patchedGetISOFields(this) {
return { ...originalGetISOFields.call(this), isoDay: 1 };
};
}
} |
@arshaw Would you like/merge a PR for this, or do you prefer I retest it after one refactor to rule them all is finished? |
This is now fixed in v0.2.0. Please confirm when you have a chance. Thanks all! |
Thank you for the release! Was anyone able to find out why or reproduce if this was not an issue in browsers, only in Node.js, like I stated in the OP? Even if this is fixed now, I would still be very very interested how that could've happened... |
@rsstiglitz I'd be very curious to know myself! However I don't think I have the bandwidth right now to spin up the old code for debugging. I can't think of an obvious culprit, so this might remain a mystery. |
This might be a Node bug, running the same code in Chrome DevTools for example yields true.
Months which are a result of going forward are fine:
while going backwards breaks
equals
:Sorry this isn't more detailed, already wasted 2 hours on this nasty bug and now I'm tired. 😝
The text was updated successfully, but these errors were encountered: