mirror of
https://github.com/hapifhir/org.hl7.fhir.core.git
synced 2025-02-07 21:38:15 +00:00
Merge pull request #843 from hapifhir/dotasek-fix-presentduration-millis
Fix presentDuration representation of millis
This commit is contained in:
commit
aa00a59b96
@ -0,0 +1,29 @@
|
|||||||
|
package org.hl7.fhir.utilities;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class DurationUtil {
|
||||||
|
public static String presentDuration(long duration) {
|
||||||
|
duration = duration / 1000000;
|
||||||
|
String res = ""; // ;
|
||||||
|
long days = TimeUnit.MILLISECONDS.toDays(duration);
|
||||||
|
long hours = TimeUnit.MILLISECONDS.toHours(duration) -
|
||||||
|
TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
|
||||||
|
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) -
|
||||||
|
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
|
||||||
|
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) -
|
||||||
|
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
|
||||||
|
long millis = TimeUnit.MILLISECONDS.toMillis(duration) -
|
||||||
|
TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(duration));
|
||||||
|
|
||||||
|
if (days > 0)
|
||||||
|
res = String.format("%dd %02d:%02d:%02d.%03d", days, hours, minutes, seconds, millis);
|
||||||
|
else if (hours > 0)
|
||||||
|
res = String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, millis);
|
||||||
|
else //
|
||||||
|
res = String.format("%02d:%02d.%03d", minutes, seconds, millis);
|
||||||
|
// else
|
||||||
|
// res = String.format("%02d.%04d", seconds, millis);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,7 @@
|
|||||||
package org.hl7.fhir.utilities;
|
package org.hl7.fhir.utilities;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.hl7.fhir.utilities.TimeTracker.Counter;
|
|
||||||
|
|
||||||
public class TimeTracker {
|
public class TimeTracker {
|
||||||
|
|
||||||
@ -72,29 +68,29 @@ public class TimeTracker {
|
|||||||
CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
|
CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
|
||||||
for (Counter c : records) {
|
for (Counter c : records) {
|
||||||
if (c.count == 1) {
|
if (c.count == 1) {
|
||||||
b.append(c.name+": "+Utilities.presentDuration(c.length));
|
b.append(c.name+": "+ DurationUtil.presentDuration(c.length));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Counter c : records) {
|
for (Counter c : records) {
|
||||||
if (c.count > 1) {
|
if (c.count > 1) {
|
||||||
b.append(c.name+": "+Utilities.presentDuration(c.length)+" (#"+c.count+")");
|
b.append(c.name+": "+ DurationUtil.presentDuration(c.length)+" (#"+c.count+")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "Times: "+b.toString();
|
return "Times: "+b.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String clock() {
|
public String clock() {
|
||||||
return Utilities.presentDuration(System.nanoTime() - globalStart);
|
return DurationUtil.presentDuration(System.nanoTime() - globalStart);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String instant() {
|
public String instant() {
|
||||||
return Utilities.presentDuration(System.nanoTime() - globalStart);
|
return DurationUtil.presentDuration(System.nanoTime() - globalStart);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String milestone() {
|
public String milestone() {
|
||||||
long start = milestone == 0 ? globalStart : milestone ;
|
long start = milestone == 0 ? globalStart : milestone ;
|
||||||
milestone = System.nanoTime();
|
milestone = System.nanoTime();
|
||||||
return Utilities.presentDuration(milestone - start);
|
return DurationUtil.presentDuration(milestone - start);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -22,14 +22,12 @@ import java.nio.file.StandardCopyOption;
|
|||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
@ -92,7 +90,7 @@ public class Utilities {
|
|||||||
*
|
*
|
||||||
* @param word the word that is to be pluralized.
|
* @param word the word that is to be pluralized.
|
||||||
* @return the pluralized form of the word, or the word itself if it could not be pluralized
|
* @return the pluralized form of the word, or the word itself if it could not be pluralized
|
||||||
* @see #singularize(Object)
|
* @see Inflector#singularize(Object)
|
||||||
*/
|
*/
|
||||||
public static String pluralizeMe(String word) {
|
public static String pluralizeMe(String word) {
|
||||||
Inflector inf = new Inflector();
|
Inflector inf = new Inflector();
|
||||||
@ -1423,30 +1421,6 @@ public class Utilities {
|
|||||||
|
|
||||||
return byteArrays;
|
return byteArrays;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String presentDuration(long duration) {
|
|
||||||
duration = duration / 1000000;
|
|
||||||
String res = ""; // ;
|
|
||||||
long days = TimeUnit.MILLISECONDS.toDays(duration);
|
|
||||||
long hours = TimeUnit.MILLISECONDS.toHours(duration) -
|
|
||||||
TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
|
|
||||||
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) -
|
|
||||||
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
|
|
||||||
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) -
|
|
||||||
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
|
|
||||||
long millis = TimeUnit.MILLISECONDS.toMillis(duration) -
|
|
||||||
TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(duration));
|
|
||||||
|
|
||||||
if (days > 0)
|
|
||||||
res = String.format("%dd %02d:%02d:%02d.%04d", days, hours, minutes, seconds, millis);
|
|
||||||
else if (hours > 0)
|
|
||||||
res = String.format("%02d:%02d:%02d.%04d", hours, minutes, seconds, millis);
|
|
||||||
else //
|
|
||||||
res = String.format("%02d:%02d.%04d", minutes, seconds, millis);
|
|
||||||
// else
|
|
||||||
// res = String.format("%02d.%04d", seconds, millis);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void unzip(InputStream zip, Path target) throws IOException {
|
public static void unzip(InputStream zip, Path target) throws IOException {
|
||||||
try (ZipInputStream zis = new ZipInputStream(zip)) {
|
try (ZipInputStream zis = new ZipInputStream(zip)) {
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
package org.hl7.fhir.utilities;
|
||||||
|
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
|
||||||
|
public class DurationUtilTest {
|
||||||
|
|
||||||
|
|
||||||
|
public static Stream<Arguments> data() throws ParserConfigurationException, SAXException, IOException {
|
||||||
|
List<Arguments> objects = new ArrayList<>();
|
||||||
|
objects.add(Arguments.of("PT0.001S", "00:00.001"));
|
||||||
|
objects.add(Arguments.of("PT0.012S", "00:00.012"));
|
||||||
|
objects.add(Arguments.of("PT0.123S", "00:00.123"));
|
||||||
|
objects.add(Arguments.of("PT0.999S", "00:00.999"));
|
||||||
|
objects.add(Arguments.of("PT1.001S", "00:01.001"));
|
||||||
|
objects.add(Arguments.of("PT1M1.001S", "01:01.001"));
|
||||||
|
objects.add(Arguments.of("PT59M1.001S", "59:01.001"));
|
||||||
|
objects.add(Arguments.of("PT1H1M1.001S", "01:01:01.001"));
|
||||||
|
objects.add(Arguments.of("PT23H1M1.001S", "23:01:01.001"));
|
||||||
|
objects.add(Arguments.of("P1DT23H1M1.001S", "1d 23:01:01.001"));
|
||||||
|
objects.add(Arguments.of("P12DT23H1M1.001S", "12d 23:01:01.001"));
|
||||||
|
return objects.stream();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("data")
|
||||||
|
public void testPresentDuration(String iso8601String, String expectedPresentation) {
|
||||||
|
assertEquals(expectedPresentation, DurationUtil.presentDuration(Duration.parse(iso8601String).toNanos()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user