Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement elapsedTime to be precise #299

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

leduyquang753
Copy link

@leduyquang753 leduyquang753 commented Dec 31, 2024

(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

The new implementation is consistent with `applyDuration` so that:
if with `elapsedTime`: diff = d2 - d1
then with `applyDuration`: d1 + diff == d2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant