This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit 3ebd20002ef35b690ee0314bac254013df3f85c5 Author: Gary Gregory <[email protected]> AuthorDate: Thu Jul 18 14:34:20 2024 -0400 Add StopWatch.getStopInstant() and deprecate getStopTime() --- src/changes/changes.xml | 1 + .../java/org/apache/commons/lang3/time/StopWatch.java | 15 ++++++++++++++- .../java/org/apache/commons/lang3/time/StopWatchTest.java | 14 +++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index e1cdae1ef..255de4ae1 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -50,6 +50,7 @@ The <action> type attribute can be add,update,fix,remove. <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getSplitDuration() and deprecate getSplitTime().</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getStartInstant() and deprecate getStartTime().</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getStopInstant() and deprecate getStopTime().</action> <!-- UPDATE --> </release> <release version="3.15.0" date="2024-07-13" description="New features and bug fixes (Java 8 or above)."> diff --git a/src/main/java/org/apache/commons/lang3/time/StopWatch.java b/src/main/java/org/apache/commons/lang3/time/StopWatch.java index 5cc28143c..433a54b08 100644 --- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java +++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java @@ -386,13 +386,26 @@ public class StopWatch { return this.startTimeMillis; } + /** + * Gets the Instant this StopWatch was stopped, between the current time and midnight, January 1, 1970 UTC. + * + * @return the Instant this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC. + * @throws IllegalStateException if this StopWatch has not been started + * @since 3.16.0 + */ + public Instant getStopInstant() { + return Instant.ofEpochMilli(getStartTime()); + } + /** * Gets the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC. * - * @return the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC. + * @return the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC. * @throws IllegalStateException if this StopWatch has not been started * @since 3.12.0 + * @deprecated Use {@link #getStopInstant()}. */ + @Deprecated public long getStopTime() { if (this.runningState == State.UNSTARTED) { throw new IllegalStateException("Stopwatch has not been started"); diff --git a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java index 60ce1f872..dab6eccbb 100644 --- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java +++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java @@ -257,6 +257,19 @@ public class StopWatchTest extends AbstractLangTest { assertThat("stopWatch.toSplitString", stopWatch.toSplitString(), startsWith(MESSAGE)); } + @Test + public void testStopInstantSimple() throws InterruptedException { + final StopWatch watch = StopWatch.createStarted(); + final long testStartMillis = System.currentTimeMillis(); + sleep(MILLIS_550); + watch.stop(); + final long testEndMillis = System.currentTimeMillis(); + final Instant stopTime = watch.getStopInstant(); + assertEquals(stopTime, watch.getStopInstant()); + assertThat("stopTime", stopTime, + allOf(greaterThanOrEqualTo(Instant.ofEpochMilli(testStartMillis)), lessThanOrEqualTo(Instant.ofEpochMilli(testEndMillis)))); + } + @Test public void testStopTimeSimple() throws InterruptedException { final StopWatch watch = StopWatch.createStarted(); @@ -266,7 +279,6 @@ public class StopWatchTest extends AbstractLangTest { final long testEndMillis = System.currentTimeMillis(); final long stopTime = watch.getStopTime(); assertEquals(stopTime, watch.getStopTime()); - assertThat("stopTime", stopTime, allOf(greaterThanOrEqualTo(testStartMillis), lessThanOrEqualTo(testEndMillis))); }
