luxon
luxon copied to clipboard
Fractional milliseconds lost in unexpected ways
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})
.
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.