moment / luxon

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

Fractional milliseconds lost in unexpected ways #1529

Open arseniybanayev opened 11 months ago

arseniybanayev commented 11 months ago

Describe the bug Fractional milliseconds are dropped by plus and minus.

To Reproduce

const luxon = require("luxon")
const endOfDay = luxon.DateTime.now().endOf("day").plus({ milliseconds: .999 })
assert(endOfDay.toMillis() % 1 !== 0)  // Passes
const prevEndOfDay = endOfDay.minus({ days: 1 })
assert(prevEndOfDay.toMillis() % 1 !== 0)  // Fails

Actual vs Expected behavior Expected the second assertion to pass because I added a fractional amount of milliseconds at the end of the second line, but the fourth line dropped the fractional milliseconds. If fractional milliseconds are unsupported, then plus({ milliseconds: .999 }) should throw an exception or (worse) drop the fractional milliseconds from the return value to be consistent with the later minus({ days: 1}).

diesieben07 commented 11 months ago

Luxon is not designed to work with units smaller than a millisecond or fractional milliseconds. Ideally, this should be enforced and always yield invalid datetimes. There is no support for sub-millisecond accuracy in the underlying platform (JavaScript) anyways. Ideally this should also be stated in the docs clearly, if it isn't in there already.

See also #909, which I honestly forgot about.