First crack at this

This commit is contained in:
James Agnew 2019-01-28 17:08:39 -05:00
parent 730ba7138e
commit 4a6d0eed45
3 changed files with 82 additions and 4 deletions

View File

@ -855,6 +855,44 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
public Date dateTimeValue() {
return getValue();
}
/**
* This method implements an equality check using the rules as defined by FHIRPath.
*
* This method returns:
* <ul>
* <li>true if the given datetimes represent the exact same instant with the same precision (irrespective of the timezone)</li>
* <li>true if the given datetimes represent the exact same instant but one includes milliseconds of <code>.[0]+</code> while the other includes only SECONDS precision (irrespecitve of the timezone)</li>
* <li>true if the given datetimes represent the exact same year/year-month/year-month-date (if both operands have the same precision)</li>
* <li>false if the given datetimes have the same precision but do not represent the same instant (irrespective of timezone)</li>
* <li>null otherwise (since these datetimes are not comparable)</li>
* </ul>
*/
public Boolean equalsUsingFhirPathRules(BaseDateTimeType theOther) {
// Same precision
if (getPrecision() == theOther.getPrecision()) {
return getValue().getTime() == theOther.getValue().getTime();
}
// Both represent 0 millis but the millis are optional
if (((Integer)0).equals(getMillis())) {
if (((Integer)0).equals(theOther.getMillis())) {
if (getPrecision().ordinal() >= TemporalPrecisionEnum.SECOND.ordinal()) {
if (theOther.getPrecision().ordinal() >= TemporalPrecisionEnum.SECOND.ordinal()) {
return getValue().getTime() == theOther.getValue().getTime();
}
}
}
}
if (getPrecision() != theOther.getPrecision()) {
return null;
}
return false;
}
}

View File

@ -463,9 +463,7 @@ public class FHIRPathEngine {
DateTimeType dateRight = new DateTimeType(dateRightString);
if (theEquivalenceTest) {
TemporalPrecisionEnum lowestPrecision = dateLeft.getPrecision().ordinal() < dateRight.getPrecision().ordinal() ? dateLeft.getPrecision() : dateRight.getPrecision();
dateLeft.setPrecision(lowestPrecision);
dateRight.setPrecision(lowestPrecision);
return dateLeft.equalsUsingFhirPathRules(dateRight) == Boolean.TRUE ? 0 : 1;
}
if (dateLeft.getPrecision().ordinal() > TemporalPrecisionEnum.DAY.ordinal()) {

View File

@ -0,0 +1,42 @@
package org.hl7.fhir.r4.model;
import org.junit.Test;
import static org.junit.Assert.*;
public class BaseDateTimeTypeTest {
/**
* <ul>
* <li>true if the given datetimes represent the exact same instant with the same precision (irrespective of the timezone)</li>
* <li>true if the given datetimes represent the exact same instant but one includes milliseconds of <code>.[0]+</code> while the other includes only SECONDS precision (irrespecitve of the timezone)</li>
* <li>true if the given datetimes represent the exact same year/year-month/year-month-date (if both operands have the same precision)</li>
* <li>false if the given datetimes have the same precision but do not represent the same instant (irrespective of timezone)</li>
* <li>null otherwise (since these datetimes are not comparable)</li>
* </ul>
*/
@Test
public void equalsUsingFhirPathRules() {
// Exact same - Same timezone
assertTrue(compareDateTimes("2001-01-02T11:22:33.444Z", "2001-01-02T11:22:33.444Z"));
// Exact same - Different timezone
assertTrue(compareDateTimes("2001-01-02T11:22:33.444-03:00", "2001-01-02T10:22:33.444-04:00"));
// Exact same - Dates
assertTrue(compareDateTimes("2001", "2001"));
assertTrue(compareDateTimes("2001-01", "2001-01"));
assertTrue(compareDateTimes("2001-01-02", "2001-01-02"));
// Same precision but different values - Dates
assertFalse(compareDateTimes("2001", "2002"));
assertFalse(compareDateTimes("2001-01", "2001-02"));
assertFalse(compareDateTimes("2001-01-02", "2001-01-03"));
// Different instant - Same timezone
assertFalse(compareDateTimes("2001-01-02T11:22:33.444Z", "2001-01-02T11:22:33.445Z"));
// Different precision
assertNull(compareDateTimes("2001-01-02T11:22:33.444Z", "2001-01-02T11:22:33Z"));
}
private Boolean compareDateTimes(String theLeft, String theRight) {
return new DateTimeType(theLeft).equalsUsingFhirPathRules(new DateTimeType(theRight));
}
}