Skip to content

Commit

Permalink
Date Roller Test Fixes (#520)
Browse files Browse the repository at this point in the history
Migrating to LocalDate for parsing and date arithmetic to avoid Java Date DST quirks
  • Loading branch information
c-schuler authored Mar 22, 2024
1 parent 772d1ee commit 42871a7
Showing 1 changed file with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Operation(name = "RollTestDates")
Expand Down Expand Up @@ -190,7 +190,7 @@ public boolean getAllDateElements(FhirContext fhirContext, IBaseResource resourc
thePathToElement.remove(thePathToElement.size() - 1);
thePathToElement.add(s);
}
int daysToAdd = getDaysBetweenDates(getLastUpdatedDate(resource), new Date());
int daysToAdd = getDaysBetweenDates(getLastUpdatedDate(resource), LocalDate.now());
if (theElement instanceof org.hl7.fhir.dstu3.model.BaseDateTimeType) {
TimeZone timeZone = ((org.hl7.fhir.dstu3.model.BaseDateTimeType) theElement).getTimeZone();
((org.hl7.fhir.dstu3.model.BaseDateTimeType) theElement).setValue(DateUtils.addDays(
Expand Down Expand Up @@ -249,22 +249,19 @@ public boolean getAllDateElements(FhirContext fhirContext, IBaseResource resourc
return false;
}

private int getDaysBetweenDates(Date start, Date end) {
long startInMs = start.getTime();
long endInMs = end.getTime();
long timeDiff = Math.abs(endInMs - startInMs);
return (int) TimeUnit.DAYS.convert(timeDiff, TimeUnit.MILLISECONDS);
private int getDaysBetweenDates(LocalDate start, LocalDate end) {
return end.getDayOfYear() - start.getDayOfYear();
}

private Date getLastUpdatedDate(IBaseResource resource) {
private LocalDate getLastUpdatedDate(IBaseResource resource) {
IBaseDatatype dateLastUpdated = ExtensionUtil.getExtensionByUrl(
ExtensionUtil.getExtensionByUrl(resource, DATEROLLER_EXT_URL), "dateLastUpdated").getValue();
if (dateLastUpdated instanceof org.hl7.fhir.dstu3.model.BaseDateTimeType) {
return ((org.hl7.fhir.dstu3.model.BaseDateTimeType) dateLastUpdated).getValue();
return LocalDate.parse(((org.hl7.fhir.dstu3.model.BaseDateTimeType) dateLastUpdated).getValueAsString().split("T")[0]);
} else if (dateLastUpdated instanceof org.hl7.fhir.r4.model.BaseDateTimeType) {
return ((org.hl7.fhir.r4.model.BaseDateTimeType) dateLastUpdated).getValue();
return LocalDate.parse(((org.hl7.fhir.r4.model.BaseDateTimeType) dateLastUpdated).getValueAsString().split("T")[0]);
} else if (dateLastUpdated instanceof org.hl7.fhir.r5.model.BaseDateTimeType) {
return ((org.hl7.fhir.r5.model.BaseDateTimeType) dateLastUpdated).getValue();
return LocalDate.parse(((org.hl7.fhir.r5.model.BaseDateTimeType) dateLastUpdated).getValueAsString().split("T")[0]);
} else {
throw new IllegalArgumentException("Unsupported type/version found for dateLastUpdated extension: "
+ dateLastUpdated.fhirType());
Expand Down Expand Up @@ -304,8 +301,7 @@ private int getFrequencyInDays(IBaseResource resource) {
}

private boolean doUpdate(IBaseResource resource) {
Date today = new Date();
return DateUtils.addDays(getLastUpdatedDate(resource), getFrequencyInDays(resource)).before(today);
return getLastUpdatedDate(resource).isBefore(LocalDate.now());
}

private void updateDateRollerExtension(FhirContext fhirContext, IBaseResource resource) {
Expand Down

0 comments on commit 42871a7

Please sign in to comment.