mirror of
https://github.com/hapifhir/org.hl7.fhir.core.git
synced 2025-02-08 22:04:45 +00:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
b58fc0860f
@ -37,6 +37,7 @@ import org.apache.commons.lang3.time.DateUtils;
|
|||||||
import org.apache.commons.lang3.time.FastDateFormat;
|
import org.apache.commons.lang3.time.FastDateFormat;
|
||||||
import org.hl7.fhir.utilities.DateTimeUtil;
|
import org.hl7.fhir.utilities.DateTimeUtil;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@ -150,12 +151,25 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param thePrecision
|
||||||
|
* @return the String value of this instance with the specified precision.
|
||||||
|
*/
|
||||||
|
public String getValueAsString(TemporalPrecisionEnum thePrecision) {
|
||||||
|
return encode(getValue(), thePrecision);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String encode(Date theValue) {
|
protected String encode(Date theValue) {
|
||||||
|
return encode(theValue, myPrecision);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private String encode(Date theValue, TemporalPrecisionEnum thePrecision) {
|
||||||
if (theValue == null) {
|
if (theValue == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
switch (myPrecision) {
|
switch (thePrecision) {
|
||||||
case DAY:
|
case DAY:
|
||||||
return ourYearMonthDayFormat.format(theValue);
|
return ourYearMonthDayFormat.format(theValue);
|
||||||
case MONTH:
|
case MONTH:
|
||||||
@ -199,7 +213,7 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
return ourYearMonthDayTimeMilliFormat.format(theValue);
|
return ourYearMonthDayTimeMilliFormat.format(theValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new IllegalStateException("Invalid precision (this is a bug, shouldn't happen https://xkcd.com/2200/): " + myPrecision);
|
throw new IllegalStateException("Invalid precision (this is a bug, shouldn't happen https://xkcd.com/2200/): " + thePrecision);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package org.hl7.fhir.dstu2.model;
|
package org.hl7.fhir.dstu2.model;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
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;
|
||||||
@ -58,4 +59,23 @@ public class BaseDateTimeTypeTests {
|
|||||||
srcInstance.setValueAsString(param);
|
srcInstance.setValueAsString(param);
|
||||||
assertEquals(param, srcInstance.getValueAsString());
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ import org.apache.commons.lang3.Validate;
|
|||||||
import org.apache.commons.lang3.time.DateUtils;
|
import org.apache.commons.lang3.time.DateUtils;
|
||||||
import org.hl7.fhir.utilities.DateTimeUtil;
|
import org.hl7.fhir.utilities.DateTimeUtil;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
@ -105,8 +106,21 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
myTimeZoneZulu = false;
|
myTimeZoneZulu = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param thePrecision
|
||||||
|
* @return the String value of this instance with the specified precision.
|
||||||
|
*/
|
||||||
|
public String getValueAsString(TemporalPrecisionEnum thePrecision) {
|
||||||
|
return encode(getValue(), thePrecision);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String encode(Date theValue) {
|
protected String encode(Date theValue) {
|
||||||
|
return encode(theValue, myPrecision);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private String encode(Date theValue, TemporalPrecisionEnum thePrecision) {
|
||||||
if (theValue == null) {
|
if (theValue == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
@ -122,21 +136,22 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
|
|
||||||
StringBuilder b = new StringBuilder();
|
StringBuilder b = new StringBuilder();
|
||||||
leftPadWithZeros(cal.get(Calendar.YEAR), 4, b);
|
leftPadWithZeros(cal.get(Calendar.YEAR), 4, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
|
|
||||||
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
|
||||||
b.append('-');
|
b.append('-');
|
||||||
leftPadWithZeros(cal.get(Calendar.MONTH) + 1, 2, b);
|
leftPadWithZeros(cal.get(Calendar.MONTH) + 1, 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
|
||||||
b.append('-');
|
b.append('-');
|
||||||
leftPadWithZeros(cal.get(Calendar.DATE), 2, b);
|
leftPadWithZeros(cal.get(Calendar.DATE), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
|
||||||
b.append('T');
|
b.append('T');
|
||||||
leftPadWithZeros(cal.get(Calendar.HOUR_OF_DAY), 2, b);
|
leftPadWithZeros(cal.get(Calendar.HOUR_OF_DAY), 2, b);
|
||||||
b.append(':');
|
b.append(':');
|
||||||
leftPadWithZeros(cal.get(Calendar.MINUTE), 2, b);
|
leftPadWithZeros(cal.get(Calendar.MINUTE), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
|
||||||
b.append(':');
|
b.append(':');
|
||||||
leftPadWithZeros(cal.get(Calendar.SECOND), 2, b);
|
leftPadWithZeros(cal.get(Calendar.SECOND), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
|
||||||
b.append('.');
|
b.append('.');
|
||||||
b.append(myFractionalSeconds);
|
b.append(myFractionalSeconds);
|
||||||
for (int i = myFractionalSeconds.length(); i < 3; i++) {
|
for (int i = myFractionalSeconds.length(); i < 3; i++) {
|
||||||
|
@ -2,15 +2,22 @@ package org.hl7.fhir.dstu2016may.test;
|
|||||||
|
|
||||||
|
|
||||||
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
|
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
|
||||||
|
import org.hl7.fhir.dstu2016may.model.DateTimeType;
|
||||||
import org.hl7.fhir.dstu2016may.model.DateType;
|
import org.hl7.fhir.dstu2016may.model.DateType;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class BaseDateTimeTypeTest {
|
public class BaseDateTimeTypeTest {
|
||||||
private SimpleDateFormat myDateInstantParser;
|
private SimpleDateFormat myDateInstantParser;
|
||||||
@ -51,4 +58,23 @@ public class BaseDateTimeTypeTest {
|
|||||||
date.setValue(time);
|
date.setValue(time);
|
||||||
Assertions.assertEquals("2012-01-02", date.getValueAsString());
|
Assertions.assertEquals("2012-01-02", date.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));
|
||||||
|
}
|
||||||
}
|
}
|
@ -48,6 +48,8 @@ import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
|
|||||||
import ca.uhn.fhir.parser.DataFormatException;
|
import ca.uhn.fhir.parser.DataFormatException;
|
||||||
import org.hl7.fhir.utilities.DateTimeUtil;
|
import org.hl7.fhir.utilities.DateTimeUtil;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
||||||
|
|
||||||
static final long NANOS_PER_MILLIS = 1000000L;
|
static final long NANOS_PER_MILLIS = 1000000L;
|
||||||
@ -167,8 +169,21 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
myTimeZoneZulu = false;
|
myTimeZoneZulu = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param thePrecision
|
||||||
|
* @return the String value of this instance with the specified precision.
|
||||||
|
*/
|
||||||
|
public String getValueAsString(TemporalPrecisionEnum thePrecision) {
|
||||||
|
return encode(getValue(), thePrecision);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String encode(Date theValue) {
|
protected String encode(Date theValue) {
|
||||||
|
return encode(theValue, myPrecision);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private String encode(Date theValue, TemporalPrecisionEnum thePrecision) {
|
||||||
if (theValue == null) {
|
if (theValue == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
@ -184,21 +199,22 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
|
|
||||||
StringBuilder b = new StringBuilder();
|
StringBuilder b = new StringBuilder();
|
||||||
leftPadWithZeros(cal.get(Calendar.YEAR), 4, b);
|
leftPadWithZeros(cal.get(Calendar.YEAR), 4, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
|
|
||||||
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
|
||||||
b.append('-');
|
b.append('-');
|
||||||
leftPadWithZeros(cal.get(Calendar.MONTH) + 1, 2, b);
|
leftPadWithZeros(cal.get(Calendar.MONTH) + 1, 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
|
||||||
b.append('-');
|
b.append('-');
|
||||||
leftPadWithZeros(cal.get(Calendar.DATE), 2, b);
|
leftPadWithZeros(cal.get(Calendar.DATE), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
|
||||||
b.append('T');
|
b.append('T');
|
||||||
leftPadWithZeros(cal.get(Calendar.HOUR_OF_DAY), 2, b);
|
leftPadWithZeros(cal.get(Calendar.HOUR_OF_DAY), 2, b);
|
||||||
b.append(':');
|
b.append(':');
|
||||||
leftPadWithZeros(cal.get(Calendar.MINUTE), 2, b);
|
leftPadWithZeros(cal.get(Calendar.MINUTE), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
|
||||||
b.append(':');
|
b.append(':');
|
||||||
leftPadWithZeros(cal.get(Calendar.SECOND), 2, b);
|
leftPadWithZeros(cal.get(Calendar.SECOND), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
|
||||||
b.append('.');
|
b.append('.');
|
||||||
b.append(myFractionalSeconds);
|
b.append(myFractionalSeconds);
|
||||||
for (int i = myFractionalSeconds.length(); i < 3; i++) {
|
for (int i = myFractionalSeconds.length(); i < 3; i++) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package org.hl7.fhir.dstu3.model;
|
package org.hl7.fhir.dstu3.model;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
|
||||||
import ca.uhn.fhir.parser.DataFormatException;
|
import ca.uhn.fhir.parser.DataFormatException;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
@ -59,4 +60,23 @@ public class BaseDateTimeTypeTests {
|
|||||||
srcInstance.setValueAsString(param);
|
srcInstance.setValueAsString(param);
|
||||||
assertEquals(param, srcInstance.getValueAsString());
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,8 @@ import ca.uhn.fhir.parser.DataFormatException;
|
|||||||
import org.hl7.fhir.utilities.DateTimeUtil;
|
import org.hl7.fhir.utilities.DateTimeUtil;
|
||||||
import org.hl7.fhir.utilities.Utilities;
|
import org.hl7.fhir.utilities.Utilities;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
||||||
|
|
||||||
static final long NANOS_PER_MILLIS = 1000000L;
|
static final long NANOS_PER_MILLIS = 1000000L;
|
||||||
@ -171,8 +173,21 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
myTimeZoneZulu = false;
|
myTimeZoneZulu = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param thePrecision
|
||||||
|
* @return the String value of this instance with the specified precision.
|
||||||
|
*/
|
||||||
|
public String getValueAsString(TemporalPrecisionEnum thePrecision) {
|
||||||
|
return encode(getValue(), thePrecision);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String encode(Date theValue) {
|
protected String encode(Date theValue) {
|
||||||
|
return encode(theValue, myPrecision);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private String encode(Date theValue, TemporalPrecisionEnum thePrecision) {
|
||||||
if (theValue == null) {
|
if (theValue == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
@ -188,21 +203,22 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
|
|
||||||
StringBuilder b = new StringBuilder();
|
StringBuilder b = new StringBuilder();
|
||||||
leftPadWithZeros(cal.get(Calendar.YEAR), 4, b);
|
leftPadWithZeros(cal.get(Calendar.YEAR), 4, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
|
|
||||||
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
|
||||||
b.append('-');
|
b.append('-');
|
||||||
leftPadWithZeros(cal.get(Calendar.MONTH) + 1, 2, b);
|
leftPadWithZeros(cal.get(Calendar.MONTH) + 1, 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
|
||||||
b.append('-');
|
b.append('-');
|
||||||
leftPadWithZeros(cal.get(Calendar.DATE), 2, b);
|
leftPadWithZeros(cal.get(Calendar.DATE), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
|
||||||
b.append('T');
|
b.append('T');
|
||||||
leftPadWithZeros(cal.get(Calendar.HOUR_OF_DAY), 2, b);
|
leftPadWithZeros(cal.get(Calendar.HOUR_OF_DAY), 2, b);
|
||||||
b.append(':');
|
b.append(':');
|
||||||
leftPadWithZeros(cal.get(Calendar.MINUTE), 2, b);
|
leftPadWithZeros(cal.get(Calendar.MINUTE), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
|
||||||
b.append(':');
|
b.append(':');
|
||||||
leftPadWithZeros(cal.get(Calendar.SECOND), 2, b);
|
leftPadWithZeros(cal.get(Calendar.SECOND), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
|
||||||
b.append('.');
|
b.append('.');
|
||||||
b.append(myFractionalSeconds);
|
b.append(myFractionalSeconds);
|
||||||
for (int i = myFractionalSeconds.length(); i < 3; i++) {
|
for (int i = myFractionalSeconds.length(); i < 3; i++) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package org.hl7.fhir.r4.model;
|
package org.hl7.fhir.r4.model;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -125,4 +126,22 @@ public class BaseDateTimeTypeTest {
|
|||||||
assertEquals(param, srcInstance.getValueAsString());
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
@ -38,6 +38,7 @@ import org.apache.commons.lang3.time.DateUtils;
|
|||||||
import org.hl7.fhir.utilities.DateTimeUtil;
|
import org.hl7.fhir.utilities.DateTimeUtil;
|
||||||
import org.hl7.fhir.utilities.Utilities;
|
import org.hl7.fhir.utilities.Utilities;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
@ -169,8 +170,21 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
myTimeZoneZulu = false;
|
myTimeZoneZulu = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param thePrecision
|
||||||
|
* @return the String value of this instance with the specified precision.
|
||||||
|
*/
|
||||||
|
public String getValueAsString(TemporalPrecisionEnum thePrecision) {
|
||||||
|
return encode(getValue(), thePrecision);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String encode(Date theValue) {
|
protected String encode(Date theValue) {
|
||||||
|
return encode(theValue, myPrecision);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private String encode(Date theValue, TemporalPrecisionEnum thePrecision) {
|
||||||
if (theValue == null) {
|
if (theValue == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
@ -186,21 +200,22 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
|
|
||||||
StringBuilder b = new StringBuilder();
|
StringBuilder b = new StringBuilder();
|
||||||
leftPadWithZeros(cal.get(Calendar.YEAR), 4, b);
|
leftPadWithZeros(cal.get(Calendar.YEAR), 4, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
|
|
||||||
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
|
||||||
b.append('-');
|
b.append('-');
|
||||||
leftPadWithZeros(cal.get(Calendar.MONTH) + 1, 2, b);
|
leftPadWithZeros(cal.get(Calendar.MONTH) + 1, 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
|
||||||
b.append('-');
|
b.append('-');
|
||||||
leftPadWithZeros(cal.get(Calendar.DATE), 2, b);
|
leftPadWithZeros(cal.get(Calendar.DATE), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
|
||||||
b.append('T');
|
b.append('T');
|
||||||
leftPadWithZeros(cal.get(Calendar.HOUR_OF_DAY), 2, b);
|
leftPadWithZeros(cal.get(Calendar.HOUR_OF_DAY), 2, b);
|
||||||
b.append(':');
|
b.append(':');
|
||||||
leftPadWithZeros(cal.get(Calendar.MINUTE), 2, b);
|
leftPadWithZeros(cal.get(Calendar.MINUTE), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
|
||||||
b.append(':');
|
b.append(':');
|
||||||
leftPadWithZeros(cal.get(Calendar.SECOND), 2, b);
|
leftPadWithZeros(cal.get(Calendar.SECOND), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
|
||||||
b.append('.');
|
b.append('.');
|
||||||
b.append(myFractionalSeconds);
|
b.append(myFractionalSeconds);
|
||||||
for (int i = myFractionalSeconds.length(); i < 3; i++) {
|
for (int i = myFractionalSeconds.length(); i < 3; i++) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package org.hl7.fhir.r4b.model;
|
package org.hl7.fhir.r4b.model;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
@ -158,4 +159,22 @@ public class BaseDateTimeTypeTest {
|
|||||||
assertEquals(param, srcInstance.getValueAsString());
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
@ -38,6 +38,7 @@ import org.apache.commons.lang3.time.DateUtils;
|
|||||||
import org.hl7.fhir.utilities.DateTimeUtil;
|
import org.hl7.fhir.utilities.DateTimeUtil;
|
||||||
import org.hl7.fhir.utilities.Utilities;
|
import org.hl7.fhir.utilities.Utilities;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
@ -169,8 +170,21 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
myTimeZoneZulu = false;
|
myTimeZoneZulu = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param thePrecision
|
||||||
|
* @return the String value of this instance with the specified precision.
|
||||||
|
*/
|
||||||
|
public String getValueAsString(TemporalPrecisionEnum thePrecision) {
|
||||||
|
return encode(getValue(), thePrecision);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String encode(Date theValue) {
|
protected String encode(Date theValue) {
|
||||||
|
return encode(theValue, myPrecision);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private String encode(Date theValue, TemporalPrecisionEnum thePrecision) {
|
||||||
if (theValue == null) {
|
if (theValue == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
@ -186,21 +200,22 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
|
|||||||
|
|
||||||
StringBuilder b = new StringBuilder();
|
StringBuilder b = new StringBuilder();
|
||||||
leftPadWithZeros(cal.get(Calendar.YEAR), 4, b);
|
leftPadWithZeros(cal.get(Calendar.YEAR), 4, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
|
|
||||||
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.YEAR.ordinal()) {
|
||||||
b.append('-');
|
b.append('-');
|
||||||
leftPadWithZeros(cal.get(Calendar.MONTH) + 1, 2, b);
|
leftPadWithZeros(cal.get(Calendar.MONTH) + 1, 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.MONTH.ordinal()) {
|
||||||
b.append('-');
|
b.append('-');
|
||||||
leftPadWithZeros(cal.get(Calendar.DATE), 2, b);
|
leftPadWithZeros(cal.get(Calendar.DATE), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {
|
||||||
b.append('T');
|
b.append('T');
|
||||||
leftPadWithZeros(cal.get(Calendar.HOUR_OF_DAY), 2, b);
|
leftPadWithZeros(cal.get(Calendar.HOUR_OF_DAY), 2, b);
|
||||||
b.append(':');
|
b.append(':');
|
||||||
leftPadWithZeros(cal.get(Calendar.MINUTE), 2, b);
|
leftPadWithZeros(cal.get(Calendar.MINUTE), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.MINUTE.ordinal()) {
|
||||||
b.append(':');
|
b.append(':');
|
||||||
leftPadWithZeros(cal.get(Calendar.SECOND), 2, b);
|
leftPadWithZeros(cal.get(Calendar.SECOND), 2, b);
|
||||||
if (myPrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
|
if (thePrecision.ordinal() > TemporalPrecisionEnum.SECOND.ordinal()) {
|
||||||
b.append('.');
|
b.append('.');
|
||||||
b.append(myFractionalSeconds);
|
b.append(myFractionalSeconds);
|
||||||
for (int i = myFractionalSeconds.length(); i < 3; i++) {
|
for (int i = myFractionalSeconds.length(); i < 3; i++) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package org.hl7.fhir.r5.model;
|
package org.hl7.fhir.r5.model;
|
||||||
|
|
||||||
|
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
@ -158,4 +159,22 @@ public class BaseDateTimeTypeTest {
|
|||||||
assertEquals(param, srcInstance.getValueAsString());
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user