fullcalendar / temporal-polyfill

A lightweight polyfill for Temporal, successor to the JavaScript Date object
MIT License
369 stars 14 forks source link

Version 0.2.5 breaks unit tests in my app #42

Closed adipascu closed 5 months ago

adipascu commented 5 months ago

Version 0.2.5 breaks unit tests in my app, is this working as intended?

Here is the failing test case:

const calculateAge = (
  birthDay: Temporal.PlainDate,
  now: Temporal.ZonedDateTime,
) => {
  const midnightInstant = birthDay.toZonedDateTime(now.getTimeZone());
  return now.since(midnightInstant).total({
    unit: "years",
    relativeTo: midnightInstant,
  });
};

const birthDate = Temporal.PlainDate.from("2000-02-29");
const now = Temporal.ZonedDateTime.from("2020-02-28T00:00:00Z[UTC]");
expect(calculateAge(birthDate, now)).toBe(20);

https://github.com/adipascu/solid-motivation/blob/f11e818b97c816f5ec5c830dc0f63177bfd064e6/src/calculate-age.test.ts#L47

arshaw commented 5 months ago

Hi @adipascu, yes this is working as intended. The since/until algorithm changed recently in the Temporal spec and temporal-polyfill already caught up to those changes.

The following code will yield the same result in the tc39 temporal "playground" (open your dev console):

const now = Temporal.ZonedDateTime.from("2020-02-28T00:00:00Z[UTC]");
const birthDate = Temporal.PlainDate.from("2000-02-29");
const midnightInstant = birthDate.toZonedDateTime(now.getTimeZone());
const dur = now.since(midnightInstant)
const years = dur.total({
  unit: "years",
  relativeTo: midnightInstant,
});
console.log(dur.toString()); // PT175296H
console.log('years', years); // 19.997267759562842