moment / luxon

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

Duration::shiftTo does not convert to years correctly #1604

Open wb459 opened 3 months ago

wb459 commented 3 months ago

Describe the bug Duration::shiftTo does not convert to years correctly. A value in milliseconds less than 1 year results in a new duration that is longer than a year.

To Reproduce

var luxon = require("luxon")
const result = luxon.Duration.fromObject({"millisecond": 31535940000});
result.shiftTo('year', 'month', 'day', 'hour', 'minute', 'second', 'millisecond');

Actual vs Expected behavior Actual: {years: 1, months: 0, days: 4, hours: 23, minutes: 59, seconds: 0, milliseconds: 0}

Expected: According to https://github.com/moment/luxon/blob/master/docs/math.md a year is considered to be 365 days or 31536000000 milliseconds (365 24 60 60 1000).

Desktop:

e965 commented 1 month ago

For some reason, I'm also getting a very strange result:

console.log(
  Duration.fromObject({ days: 365 * 2 }) // 2 years
    .shiftToAll()
    .toHuman(), // toObject does return the same results
); // output - 2 years, 2 months, 0 weeks, 2 days, 0 hours, 0 minutes, 0 seconds, 0 milliseconds

I have no idea where another two months and two days came from.