Test and new getValueAsString(TemporalPrecisionEnum) method

This commit is contained in:
dotasek 2023-06-19 14:47:39 -04:00
parent 829882773a
commit 92fb21de1e
2 changed files with 40 additions and 10 deletions

View File

@ -38,6 +38,7 @@ import org.apache.commons.lang3.time.DateUtils;
import org.hl7.fhir.utilities.DateTimeUtil;
import org.hl7.fhir.utilities.Utilities;
import javax.annotation.Nullable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@ -169,10 +170,19 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
myTimeZoneZulu = false;
}
public String getValueAsString(TemporalPrecisionEnum thePrecision) {
return encode(getValue(), thePrecision);
}
@Override
protected String encode(Date theValue) {
if (theValue == null) {
return null;
return encode(theValue, myPrecision);
}
@Nullable
private String encode(Date theValue, TemporalPrecisionEnum thePrecision) {
if (theValue == null) {
return null;
} else {
GregorianCalendar cal;
if (myTimeZoneZulu) {
@ -186,21 +196,22 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
StringBuilder b = new StringBuilder();
leftPadWithZeros(cal.get(Calendar.YEAR), 4, b);
if (myPrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
if (thePrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
b.append('-');
leftPadWithZeros(cal.get(Calendar.MONTH) + 1, 2, b);
if (myPrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
if (thePrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
b.append('-');
leftPadWithZeros(cal.get(Calendar.DATE), 2, b);
if (myPrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
if (thePrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
b.append('T');
leftPadWithZeros(cal.get(Calendar.HOUR_OF_DAY), 2, b);
b.append(':');
leftPadWithZeros(cal.get(Calendar.MINUTE), 2, b);
if (myPrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
if (thePrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
b.append(':');
leftPadWithZeros(cal.get(Calendar.SECOND), 2, b);
if (myPrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
if (thePrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
b.append('.');
b.append(myFractionalSeconds);
for (int i = myFractionalSeconds.length(); i < 3; i++) {
@ -230,11 +241,11 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
}
}
}
return b.toString();
return b.toString();
}
}
}
/**
/**
* Returns the month with 1-index, e.g. 1=the first day of the month
*/
public Integer getDay() {

View File

@ -1,5 +1,6 @@
package org.hl7.fhir.r5.model;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@ -158,4 +159,22 @@ public class BaseDateTimeTypeTest {
assertEquals(param, srcInstance.getValueAsString());
}
private static Stream<Arguments> getGetValueAsStringParams() {
return Stream.of(
Arguments.of(new DateTimeType("1933-01-02T12:34:56.789"), TemporalPrecisionEnum.MILLI, "1933-01-02T12:34:56.789"),
Arguments.of(new DateTimeType("1933-01-02T12:34:56.789"), TemporalPrecisionEnum.SECOND, "1933-01-02T12:34:56"),
Arguments.of(new DateTimeType("1933-01-02T12:34:56.789"), TemporalPrecisionEnum.MINUTE, "1933-01-02T12:34"),
Arguments.of(new DateTimeType("1933-01-02T12:34:56.789"), TemporalPrecisionEnum.MINUTE, "1933-01-02T12:34"),
Arguments.of(new DateTimeType("1933-01-02T12:34:56.789"), TemporalPrecisionEnum.DAY, "1933-01-02"),
Arguments.of(new DateTimeType("1933-01-02T12:34:56.789"), TemporalPrecisionEnum.MONTH, "1933-01"),
Arguments.of(new DateTimeType("1933-01-02T12:34:56.789"), TemporalPrecisionEnum.YEAR, "1933")
);
}
@ParameterizedTest
@MethodSource("getGetValueAsStringParams")
public void testGetValueAsString(DateTimeType theType, TemporalPrecisionEnum thePrecision, String expectedStringValue) {
assertEquals(expectedStringValue, theType.getValueAsString(thePrecision));
}
}