moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.12k stars 728 forks source link

Duration subtraction regression #1482

Closed Maxim-Mazurok closed 11 months ago

Maxim-Mazurok commented 11 months ago

Describe the bug Previously subtracting "P1W" from "P14D" resulted in "P1W" which makes sense. Now it's "P0.651W2DT10H17M45.6S"

To Reproduce You can run this at https://npm.runkit.com/luxon

const { DateTime, Duration } = require("luxon@3.4.0");

const purchaseDate = DateTime.fromObject(
  { day: 1, month: 1, year: 2020 },
  { zone: "UTC" }
);
const warranty = Duration.fromObject(
  { days: 14 },
  { conversionAccuracy: "longterm" }
);
const now = DateTime.fromObject(
  { day: 8, month: 1, year: 2020 },
  { zone: "UTC" }
);
const age = now.diff(purchaseDate).shiftToAll();
const warrantyLeft = warranty.minus(age).shiftToAll();

console.log({
  purchaseDate: purchaseDate.toString(),
  warranty: warranty.toString(),
  now: now.toString(),
  age: age.toString(),
  warrantyLeft: warrantyLeft.toString(),
});

it will produce

purchaseDate: "2020-01-01T00:00:00.000Z"
warranty: "P14D"
now: "2020-01-08T00:00:00.000Z"
age: "P1W"
warrantyLeft: "P0.651W2DT10H17M45.6S"

Actual vs Expected behavior Running luxon 3.3.0 at https://npm.runkit.com/luxon:

const { DateTime, Duration } = require("luxon@3.3.0");

const purchaseDate = DateTime.fromObject(
  { day: 1, month: 1, year: 2020 },
  { zone: "UTC" }
);
const warranty = Duration.fromObject(
  { days: 14 },
  { conversionAccuracy: "longterm" }
);
const now = DateTime.fromObject(
  { day: 8, month: 1, year: 2020 },
  { zone: "UTC" }
);
const age = now.diff(purchaseDate).shiftToAll();
const warrantyLeft = warranty.minus(age).shiftToAll();

console.log({
  purchaseDate: purchaseDate.toString(),
  warranty: warranty.toString(),
  now: now.toString(),
  age: age.toString(),
  warrantyLeft: warrantyLeft.toString(),
});

results in

purchaseDate: "2020-01-01T00:00:00.000Z"
warranty: "P14D"
now: "2020-01-08T00:00:00.000Z"
age: "P1W"
warrantyLeft: "P1W"

as expected.

Basically I have 14 days warranty, 7 days age of product, I want to figure out how much warranty left. It should be one week, as it was with luxon 3.3.0

Desktop (please complete the following information):

icambron commented 11 months ago

Thanks. This is likely introduced by https://github.com/moment/luxon/pull/1467. I'll take a look soon.

icambron commented 11 months ago

Fixed in 3.4.1. Thanks for the report