Skip to content

Commit 32889e9

Browse files
committed
Merge pull request #27149 from marckchr
* pr/27149: Polish "Fix duration to microseconds conversion" Fix duration to microseconds conversion Closes gh-27149
2 parents 877f618 + 5ec0c7e commit 32889e9

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationStyle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -183,7 +183,7 @@ enum Unit {
183183
/**
184184
* Microseconds.
185185
*/
186-
MICROS(ChronoUnit.MICROS, "us", (duration) -> duration.toMillis() * 1000L),
186+
MICROS(ChronoUnit.MICROS, "us", (duration) -> duration.toNanos() / 1000L),
187187

188188
/**
189189
* Milliseconds.

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/DurationStyleTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -231,9 +231,15 @@ void printSimpleWithoutUnitShouldPrintInMs() {
231231
}
232232

233233
@Test
234-
void printSimpleWithUnitShouldPrintInUnit() {
234+
void printSimpleWithSecondsUnitShouldPrintInUnit() {
235235
Duration duration = Duration.ofMillis(1000);
236236
assertThat(DurationStyle.SIMPLE.print(duration, ChronoUnit.SECONDS)).isEqualTo("1s");
237237
}
238238

239+
@Test
240+
void printSimpleWithMicrosUnitShouldPrintInUnit() {
241+
Duration duration = Duration.ofNanos(2000);
242+
assertThat(DurationStyle.SIMPLE.print(duration, ChronoUnit.MICROS)).isEqualTo("2us");
243+
}
244+
239245
}

0 commit comments

Comments
 (0)