-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
276 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.ethlo.time; | ||
|
||
/*- | ||
* #%L | ||
* chronograph | ||
* %% | ||
* Copyright (C) 2019 Morten Haraldsen (ethlo) | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import java.math.RoundingMode; | ||
import java.text.NumberFormat; | ||
import java.time.Duration; | ||
|
||
public class DurationUtil | ||
{ | ||
public static final int SECONDS_PER_HOUR = 3_600; | ||
public static final int SECONDS_PER_MINUTE = 60; | ||
public static final int NANOS_PER_MILLI = 1_000_000; | ||
private static final int NANOS_PER_MICRO = 1_000; | ||
|
||
public static String humanReadable(Duration duration) | ||
{ | ||
final long seconds = duration.getSeconds(); | ||
final int hours = (int) seconds / SECONDS_PER_HOUR; | ||
int remainder = (int) seconds - hours * SECONDS_PER_HOUR; | ||
final int mins = remainder / SECONDS_PER_MINUTE; | ||
remainder = remainder - mins * SECONDS_PER_MINUTE; | ||
final int secs = remainder; | ||
|
||
final long nanos = duration.getNano(); | ||
final int millis = (int) nanos / NANOS_PER_MILLI; | ||
remainder = (int) nanos - millis * NANOS_PER_MILLI; | ||
final int micros = remainder / NANOS_PER_MICRO; | ||
remainder = remainder - micros * NANOS_PER_MICRO; | ||
final int nano = remainder; | ||
|
||
final NumberFormat nf = NumberFormat.getNumberInstance(); | ||
nf.setMinimumIntegerDigits(2); | ||
|
||
final NumberFormat df = NumberFormat.getNumberInstance(); | ||
df.setMinimumFractionDigits(2); | ||
df.setMaximumFractionDigits(2); | ||
df.setRoundingMode(RoundingMode.HALF_UP); | ||
|
||
final StringBuilder sb = new StringBuilder(); | ||
if (hours > 0) | ||
{ | ||
sb.append(nf.format(hours)).append(":"); | ||
} | ||
if (hours > 0 || mins > 0) | ||
{ | ||
sb.append(nf.format(mins)).append(":"); | ||
} | ||
|
||
final boolean hasMinuteOrMore = hours > 0 || mins > 0; | ||
final boolean hasSecondOrMore = hasMinuteOrMore || secs > 0; | ||
if (hasSecondOrMore && !hasMinuteOrMore) | ||
{ | ||
final NumberFormat dfSec = NumberFormat.getNumberInstance(); | ||
dfSec.setMinimumFractionDigits(0); | ||
dfSec.setMaximumFractionDigits(0); | ||
dfSec.setMinimumIntegerDigits(3); | ||
dfSec.setMaximumIntegerDigits(3); | ||
sb.append(seconds).append('.').append(dfSec.format(nanos / (double) NANOS_PER_MILLI)).append("s"); | ||
} | ||
else if (hasSecondOrMore) | ||
{ | ||
sb.append(nf.format(secs)).append(".").append(millis); | ||
} | ||
else | ||
{ | ||
// Sub-second | ||
if (millis > 0) | ||
{ | ||
sb.append(df.format(nanos / (double) NANOS_PER_MILLI)).append("m "); | ||
} | ||
|
||
if (millis == 0 && micros > 0) | ||
{ | ||
sb.append(df.format(nanos / (double) NANOS_PER_MICRO)).append("μ "); | ||
} | ||
|
||
if (millis == 0 && micros == 0 && nano > 0) | ||
{ | ||
sb.append(nano).append("n "); | ||
} | ||
} | ||
|
||
return sb.toString().trim(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.ethlo.time; | ||
|
||
/*- | ||
* #%L | ||
* chronograph | ||
* %% | ||
* Copyright (C) 2019 Morten Haraldsen (ethlo) | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.Duration; | ||
|
||
import org.junit.Test; | ||
|
||
public class DurationUtilTest | ||
{ | ||
@Test | ||
public void humanReadableFormatMoreThanHour() | ||
{ | ||
assertThat(DurationUtil.humanReadable(Duration.ofSeconds(4712).withNanos(123456789))).isEqualTo("01:18:32.123"); | ||
} | ||
|
||
@Test | ||
public void humanReadableFormatLessThanHour() | ||
{ | ||
assertThat(DurationUtil.humanReadable(Duration.ofSeconds(2000).withNanos(123456789))).isEqualTo("33:20.123"); | ||
} | ||
|
||
@Test | ||
public void humanReadableFormatLessThanMinute() | ||
{ | ||
assertThat(DurationUtil.humanReadable(Duration.ofSeconds(8).withNanos(125_956_789))).isEqualTo("8.126s"); | ||
} | ||
|
||
@Test | ||
public void humanReadableFormatLessThanMinute2() | ||
{ | ||
assertThat(DurationUtil.humanReadable(Duration.ofSeconds(8).withNanos(1_000_000))).isEqualTo("8.001s"); | ||
} | ||
|
||
@Test | ||
public void humanReadableFormatLessThanMinute3() | ||
{ | ||
assertThat(DurationUtil.humanReadable(Duration.ofSeconds(8))).isEqualTo("8.000s"); | ||
} | ||
|
||
@Test | ||
public void humanReadableFormatLessThanSecond() | ||
{ | ||
assertThat(DurationUtil.humanReadable(Duration.ofNanos(123_456_789))).isEqualTo("123.46m"); | ||
} | ||
|
||
@Test | ||
public void humanReadableFormatLessThanMillisecond() | ||
{ | ||
assertThat(DurationUtil.humanReadable(Duration.ofNanos(456_789))).isEqualTo("456.79μ"); | ||
} | ||
|
||
@Test | ||
public void humanReadableFormatLessThanMicrosecond() | ||
{ | ||
assertThat(DurationUtil.humanReadable(Duration.ofNanos(489))).isEqualTo("489n"); | ||
} | ||
} |