relative-time-element icon indicating copy to clipboard operation
relative-time-element copied to clipboard

Reimplement `elapsedTime` to be precise

Open leduyquang753 opened this issue 1 year ago • 0 comments

(This is part of a set of multiple pull requests looking to overhaul the calculation functions.)

The current implementation of elapsedTime just assumes there are 30 days in a month, which causes overshoots when roundToSingleUnit takes its result and, in a rather roundabout way, tries to compute the original date back.

This pull request reimplements the function so that it is consistent with applyDuration: applying the duration to the reference date always gives the original input date.

In math form:

If diff = date2 - date1 (elapsedTime)
then date2 = date1 + diff (applyDuration)

In code:

applyDuration(date1, elapsedTime(date2, 'second', date1)) === date2

leduyquang753 avatar Dec 31 '24 07:12 leduyquang753