relative-time-element
relative-time-element copied to clipboard
Reimplement `elapsedTime` to be precise
(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