Support microseconds in Duration conversions

Closes gh-13624
pull/13710/head
Madhura Bhave 6 years ago
parent 06a8c41943
commit 673a08d228

@ -1263,6 +1263,7 @@ read timeout of 500ms can be specified in any of the following form: `500`, `PT0
You can also use any of the supported unit. These are:
* `ns` for nanoseconds
* `us` for microseconds
* `ms` for milliseconds
* `s` for seconds
* `m` for minutes

@ -180,6 +180,11 @@ public enum DurationStyle {
*/
NANOS(ChronoUnit.NANOS, "ns", Duration::toNanos),
/**
* Microseconds.
*/
MICROS(ChronoUnit.MICROS, "us", (duration) -> duration.toMillis() * 1000L),
/**
* Milliseconds.
*/

@ -71,6 +71,18 @@ public class DurationStyleTests {
.isEqualTo(Duration.ofNanos(-10));
}
@Test
public void detectAndParseWhenSimpleMicrosShouldReturnDuration() {
assertThat(DurationStyle.detectAndParse("10us"))
.isEqualTo(Duration.ofNanos(10000));
assertThat(DurationStyle.detectAndParse("10US"))
.isEqualTo(Duration.ofNanos(10000));
assertThat(DurationStyle.detectAndParse("+10us"))
.isEqualTo(Duration.ofNanos(10000));
assertThat(DurationStyle.detectAndParse("-10us"))
.isEqualTo(Duration.ofNanos(-10000));
}
@Test
public void detectAndParseWhenSimpleMillisShouldReturnDuration() {
assertThat(DurationStyle.detectAndParse("10ms")).isEqualTo(Duration.ofMillis(10));

@ -70,6 +70,14 @@ public class StringToDurationConverterTests {
assertThat(convert("-10ns")).isEqualTo(Duration.ofNanos(-10));
}
@Test
public void convertWhenSimpleMicrosShouldReturnDuration() {
assertThat(convert("10us")).isEqualTo(Duration.ofNanos(10000));
assertThat(convert("10US")).isEqualTo(Duration.ofNanos(10000));
assertThat(convert("+10us")).isEqualTo(Duration.ofNanos(10000));
assertThat(convert("-10us")).isEqualTo(Duration.ofNanos(-10000));
}
@Test
public void convertWhenSimpleMillisShouldReturnDuration() {
assertThat(convert("10ms")).isEqualTo(Duration.ofMillis(10));

Loading…
Cancel
Save