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

Add Duration methods for days, hours, minutes, seconds, etc. #76

Open
tprochazka opened this issue Sep 14, 2017 · 3 comments
Open

Add Duration methods for days, hours, minutes, seconds, etc. #76

tprochazka opened this issue Sep 14, 2017 · 3 comments
Labels

Comments

@tprochazka
Copy link

Would be great to integrate this to the backport:
https://bugs.openjdk.java.net/browse/JDK-8142936

@jodastephen
Copy link
Member

Seems like a good idea. However, for IP reasons, it needs someone other than me to do it (and whoever does it must not look at the source code of OpenJDK - just the Javadoc.

@sschaap
Copy link
Contributor

sschaap commented Nov 21, 2018

@jodastephen Had a quick look at this. Noticed your comment on the OpenJDK issue that modulo operator might not be appropriate for negative numbers. Behavior of the java.time implementation seems to match that exactly, though. Thoughts?

Sample code:

package com.example;

import java.time.Duration;

public class Issue76 {
    
    public static void main(String[] args) {
        Duration duration = Duration.ofHours(-1).plusSeconds(-1);
        System.out.printf(
                "Duration [%s] seconds = %dh %ds%n%n",
                duration, duration.toHoursPart(), duration.toSecondsPart());

        System.out.printf("-1 mod 60 = %d%n", (-1 % 60));
        System.out.printf("-1 floorMod 60 = %d%n", Math.floorMod(-1, 60));
    }
}

Output:

Duration [PT-1H-1S] seconds = -1h -1s

-1 mod 60 = -1
-1 floorMod 60 = 59

@jodastephen
Copy link
Member

jodastephen commented Nov 21, 2018

I'd expect PT-1H-1S to return:

  • toHoursPart() = -1
  • toSecondsPart() = -1

(it is 1 hour and 1 second less than zero)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants