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

Util.getNumberOfSecondFromNow is calculating the delta incorrectly #1236

Closed
Milton-Ch opened this issue Apr 20, 2022 · 3 comments
Closed

Util.getNumberOfSecondFromNow is calculating the delta incorrectly #1236

Milton-Ch opened this issue Apr 20, 2022 · 3 comments
Assignees
Labels
kind-bug Issue or PR is a bug in existing functionality

Comments

@Milton-Ch
Copy link
Contributor

Util.getNumberOfSecondsFromNow is calculating incorrectly the difference between current and param dates. After testing this locally these were the results:

    @Test
    public void calcTimeDiff() {
        Date time = DateUtils.setYears(new Date(), 2023);
        time = DateUtils.setMonths(time, 3);
        time = DateUtils.setDays(time, 20);
        System.out.println(time);
        int result = Util.getNumberOfSecondFromNow(time);
        System.out.println("Difference calculated using Util.getNumberOfSecondFromNow = " + result);

        System.out.println("Difference using Duration class = " + Duration.between(new Date().toInstant(), time.toInstant()).getSeconds());
    }

Result:

Thu Apr 20 14:48:34 BOT 2023
Difference calculated using Util.getNumberOfSecondFromNow = 1471228
Difference using Duration class = 31535999

@yuriyz Do you agree that we could use java.time.Duration instead?

@Milton-Ch Milton-Ch assigned moabu and Milton-Ch and unassigned moabu Apr 20, 2022
@Milton-Ch Milton-Ch added the kind-bug Issue or PR is a bug in existing functionality label Apr 20, 2022
@yuriyz
Copy link
Contributor

yuriyz commented Apr 20, 2022

@Milton-Ch yes, please. Check also tests why it didn't flag this bug.

@yuriyz
Copy link
Contributor

yuriyz commented Apr 20, 2022

It is casts problem, this gives correct result:

final long diff = time - now;
return (int) (diff / 1000L);

Anyway, better to stick to standard classes like Duration which are well tested.

@yuriyz
Copy link
Contributor

yuriyz commented May 10, 2022

Closing, this is fixed already.

@yuriyz yuriyz closed this as completed May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind-bug Issue or PR is a bug in existing functionality
Projects
None yet
Development

No branches or pull requests

3 participants