mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 22:34:27 +02:00
8331202
: Support for Duration until another Instant
Reviewed-by: joehw, scolebourne, rriggs
This commit is contained in:
parent
6f7ddbec7d
commit
259915168d
2 changed files with 58 additions and 3 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -1162,6 +1162,30 @@ public final class Instant
|
|||
return unit.between(this, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the {@code Duration} until another {@code Instant}.
|
||||
* <p>
|
||||
* The start and end points are {@code this} and the specified instant.
|
||||
* The result will be negative if the end is before the start. Calling
|
||||
* this method is equivalent to
|
||||
* {@link Duration#between(Temporal, Temporal) Duration.between(this,
|
||||
* endExclusive)}.
|
||||
* <p>
|
||||
* This instance is immutable and unaffected by this method call.
|
||||
*
|
||||
* @param endExclusive the end {@code Instant}, exclusive, not null
|
||||
* @return the {@code Duration} from this {@code Instant} until the
|
||||
* specified {@code endExclusive} {@code Instant}
|
||||
* @see Duration#between(Temporal, Temporal)
|
||||
* @since 23
|
||||
*/
|
||||
public Duration until(Instant endExclusive) {
|
||||
Objects.requireNonNull(endExclusive, "endExclusive");
|
||||
long secsDiff = Math.subtractExact(endExclusive.seconds, seconds);
|
||||
int nanosDiff = endExclusive.nanos - nanos;
|
||||
return Duration.ofSeconds(secsDiff, nanosDiff);
|
||||
}
|
||||
|
||||
private long nanosUntil(Instant end) {
|
||||
long secsDiff = Math.subtractExact(end.seconds, seconds);
|
||||
long totalNanos = Math.multiplyExact(secsDiff, NANOS_PER_SECOND);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue