Rename again + pass tests

This commit is contained in:
dotasek 2022-06-23 12:39:35 -04:00
parent c6a69e81ae
commit 552127464c
3 changed files with 21 additions and 15 deletions

View File

@ -2,7 +2,7 @@ package org.hl7.fhir.utilities;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class TimeUtil { public class DurationUtil {
public static String presentDuration(long duration) { public static String presentDuration(long duration) {
duration = duration / 1000000; duration = duration / 1000000;
String res = ""; // ; String res = ""; // ;
@ -17,11 +17,11 @@ public class TimeUtil {
TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(duration)); TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(duration));
if (days > 0) if (days > 0)
res = String.format("%dd %02d:%02d:%02d.%04d", days, hours, minutes, seconds, millis); res = String.format("%dd %02d:%02d:%02d.%03d", days, hours, minutes, seconds, millis);
else if (hours > 0) else if (hours > 0)
res = String.format("%02d:%02d:%02d.%04d", hours, minutes, seconds, millis); res = String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, millis);
else // else //
res = String.format("%02d:%02d.%04d", minutes, seconds, millis); res = String.format("%02d:%02d.%03d", minutes, seconds, millis);
// else // else
// res = String.format("%02d.%04d", seconds, millis); // res = String.format("%02d.%04d", seconds, millis);
return res; return res;

View File

@ -68,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+": "+ TimeUtil.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+": "+ TimeUtil.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 TimeUtil.presentDuration(System.nanoTime() - globalStart); return DurationUtil.presentDuration(System.nanoTime() - globalStart);
} }
public String instant() { public String instant() {
return TimeUtil.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 TimeUtil.presentDuration(milestone - start); return DurationUtil.presentDuration(milestone - start);
} }
} }

View File

@ -1,12 +1,8 @@
package org.hl7.fhir.utilities; package org.hl7.fhir.utilities;
import org.hl7.fhir.utilities.xml.XMLUtil;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
@ -19,19 +15,29 @@ import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class TimeUtilTest { public class DurationUtilTest {
public static Stream<Arguments> data() throws ParserConfigurationException, SAXException, IOException { public static Stream<Arguments> data() throws ParserConfigurationException, SAXException, IOException {
List<Arguments> objects = new ArrayList<>(); List<Arguments> objects = new ArrayList<>();
objects.add(Arguments.of("PT0.001S", "00:00.001")); 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(); return objects.stream();
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("data") @MethodSource("data")
public void testPresentDuration(String iso8601String, String expectedPresentation) { public void testPresentDuration(String iso8601String, String expectedPresentation) {
assertEquals(TimeUtil.presentDuration(Duration.parse(iso8601String).toNanos()), expectedPresentation); assertEquals(expectedPresentation, DurationUtil.presentDuration(Duration.parse(iso8601String).toNanos()));
} }