Durations in existing JDK APIs
Stephen Colebourne
scolebourne at joda.org
Wed May 30 17:54:05 UTC 2018
On 30 May 2018 at 18:36, Martin Buchholz <martinrb at google.com> wrote:
> Stephen/Doug: is there any reason we didn't add conversions between Duration
> and TimeUnit when we added conversions to ChronoUnit?
I don't remember the idea being discussed. The proposed implementation
seems reasonable.
thanks
Stephen
> Here's a strawman:
>
> /**
> * Converts the given time duration to this unit.
> *
> * @param duration the time duration
> * @return the converted duration in this unit,
> * or {@code Long.MIN_VALUE} if conversion would negatively overflow,
> * or {@code Long.MAX_VALUE} if it would positively overflow.
> */
> public long convert(Duration duration) {
> long s = convert(duration.getSeconds(), SECONDS);
> if (s == Long.MIN_VALUE) return s;
> long n = convert(duration.getNano(), NANOSECONDS);
> assert n >= 0 && n < 1_000_000_000;
> return (s + n < s) ? Long.MAX_VALUE : s + n;
> }
>
More information about the core-libs-dev
mailing list