Method is implemented now

This commit is contained in:
James Agnew 2019-01-28 21:22:11 -05:00
parent 4a6d0eed45
commit df7f2ca0e6
2 changed files with 67 additions and 19 deletions

View File

@ -857,19 +857,33 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
} }
/** /**
* This method implements an equality check using the rules as defined by FHIRPath. * This method implements a datetime equality check using the rules as defined by FHIRPath.
* *
* This method returns: * This method returns:
* <ul> * <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 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 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>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 both datetimes have equal precision of MINUTE or greater, one has no timezone specified but the other does, and could not represent the same instant in any timezone</li>
* <li>null if both datetimes have equal precision of MINUTE or greater, one has no timezone specified but the other does, and could potentially represent the same instant in any timezone</li>
* <li>false if the given datetimes have the same precision but do not represent the same instant (irrespective of timezone)</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> * <li>null otherwise (since these datetimes are not comparable)</li>
* </ul> * </ul>
*/ */
public Boolean equalsUsingFhirPathRules(BaseDateTimeType theOther) { public Boolean equalsUsingFhirPathRules(BaseDateTimeType theOther) {
if (hasTimezoneIfRequired() != theOther.hasTimezoneIfRequired()) {
if (getPrecision() == theOther.getPrecision()) {
if (getPrecision().ordinal() >= TemporalPrecisionEnum.MINUTE.ordinal() && theOther.getPrecision().ordinal() >= TemporalPrecisionEnum.MINUTE.ordinal()) {
boolean couldBeTheSameTime = couldBeTheSameTime(this, theOther) || couldBeTheSameTime(theOther, this);
if (!couldBeTheSameTime) {
return false;
}
}
}
return null;
}
// Same precision // Same precision
if (getPrecision() == theOther.getPrecision()) { if (getPrecision() == theOther.getPrecision()) {
return getValue().getTime() == theOther.getValue().getTime(); return getValue().getTime() == theOther.getValue().getTime();
@ -886,13 +900,26 @@ public abstract class BaseDateTimeType extends PrimitiveType<Date> {
} }
} }
if (getPrecision() != theOther.getPrecision()) {
return null;
}
return false; return false;
} }
private boolean couldBeTheSameTime(BaseDateTimeType theArg1, BaseDateTimeType theArg2) {
boolean theCouldBeTheSameTime = false;
if (theArg1.getTimeZone() == null && theArg2.getTimeZone() != null) {
long lowLeft = new DateTimeType(theArg1.getValueAsString()+"Z").getValue().getTime() - (14 * DateUtils.MILLIS_PER_HOUR);
long highLeft = new DateTimeType(theArg1.getValueAsString()+"Z").getValue().getTime() + (14 * DateUtils.MILLIS_PER_HOUR);
long right = theArg2.getValue().getTime();
if (right >= lowLeft && right <= highLeft) {
theCouldBeTheSameTime = true;
}
}
return theCouldBeTheSameTime;
}
boolean hasTimezoneIfRequired() {
return getPrecision().ordinal() <= TemporalPrecisionEnum.DAY.ordinal() ||
getTimeZone() != null;
}
} }

View File

@ -32,11 +32,32 @@ public class BaseDateTimeTypeTest {
// Different instant - Same timezone // Different instant - Same timezone
assertFalse(compareDateTimes("2001-01-02T11:22:33.444Z", "2001-01-02T11:22:33.445Z")); assertFalse(compareDateTimes("2001-01-02T11:22:33.444Z", "2001-01-02T11:22:33.445Z"));
// Different precision // Different precision
assertNull(compareDateTimes("2001-01-02T11:22:33.444Z", "2001-01-02T11:22:33Z")); // assertNull(compareDateTimes("2001-01-02T11:22:33.444Z", "2001-01-02T11:22:33Z"));
// FHIRPath tests:
assertFalse(compareDateTimes("1974-12-25", "1974-12-25T12:34:00+10:00"));
assertFalse(compareDateTimes("1974-12-25", "1974-12-25T12:34:00-10:00"));
assertFalse(compareDateTimes("1974-12-25", "1974-12-25T12:34:00Z"));
assertFalse(compareDateTimes("2012-04-15", "2012-04-16"));
assertFalse(compareDateTimes("2012-04-15T15:00:00", "2012-04-15T10:00:00"));
assertFalse(compareDateTimes("2017-11-05T01:30:00.0-04:00", "2017-11-05T01:15:00.0-05:00"));
assertNull(compareDateTimes("1974-12-25", "1974-12-25T12:34:00"));
assertNull(compareDateTimes("2012-04-15", "2012-04-15T10:00:00"));
assertNull(compareDateTimes("2012-04-15T15:00:00Z", "2012-04-15T10:00:00"));
assertTrue(compareDateTimes("1974-12-25", "1974-12-25"));
assertTrue(compareDateTimes("2012-04-15", "2012-04-15"));
assertTrue(compareDateTimes("2012-04-15T15:00:00+02:00", "2012-04-15T16:00:00+03:00"));
assertTrue(compareDateTimes("2012-04-15T15:00:00+02:00", "2012-04-15T16:00:00+03:00"));
assertTrue(compareDateTimes("2017-11-05T01:30:00.0-04:00", "2017-11-05T00:30:00.0-05:00"));
assertFalse(compareDateTimes("2016-12-02T13:00:00Z", "2016-11-02T10:00:00")); // no timezone, but cannot be the same time
assertNull(compareDateTimes("2016-12-02T13:00:00Z", "2016-12-02T10:00:00")); // no timezone, might be the same time
} }
private Boolean compareDateTimes(String theLeft, String theRight) { private Boolean compareDateTimes(String theLeft, String theRight) {
return new DateTimeType(theLeft).equalsUsingFhirPathRules(new DateTimeType(theRight)); DateTimeType leftDt = new DateTimeType(theLeft);
DateTimeType rightDt = new DateTimeType(theRight);
return leftDt.equalsUsingFhirPathRules(rightDt);
} }
} }