Skip to content

Commit

Permalink
Fix case in test CQL file to fix pipeline failure. Cleanup code in Da…
Browse files Browse the repository at this point in the history
…teTime.
  • Loading branch information
lukedegruchy committed Oct 26, 2023
1 parent 221220d commit 4ed57ff
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;
import java.util.Optional;
import java.util.TimeZone;
import java.util.stream.Collectors;

import org.opencds.cqf.cql.engine.exception.InvalidDateTime;

Expand Down Expand Up @@ -49,9 +47,6 @@ public DateTime(OffsetDateTime dateTime) {
public DateTime(OffsetDateTime dateTime, Precision precision) {
setDateTime(dateTime);
this.precision = precision;

final String offsetDateTimeId = dateTime.getOffset().getId();

zoneId = toZoneId(dateTime);
}

Expand Down Expand Up @@ -134,89 +129,18 @@ else if (i == 6) {
precision = Precision.fromDateTimeIndex(stringElements.length - 1);
dateString = new StringBuilder().append(TemporalHelper.autoCompleteDateTimeString(dateString.toString(), precision));

// LUKETODO: normally, the calling method provides an offset, which is supposed to derived from the TZ
// so we shouldn't do anything funky with timezones here: we should just interpret the offset as passed and
// process the minutes correctly

// If the incoming string has an offset specified, use that offset
// Otherwise, parse as a LocalDateTime and then interpret that in the evaluation timezone

if (offset != null) {
// LUKETODO: this is for debugging purposes: remove when testing is done
final ZoneId zoneId = ZoneId.systemDefault();
final int totalSeconds = ZonedDateTime.now().getOffset().getTotalSeconds();
final int totalMinutes = totalSeconds / 60;
final int totalMinutesModulo60 = totalMinutes % 60;

final boolean hoursPositiveNumber = offset.intValue() >= 0;
final boolean minutesPositiveNumber = totalMinutesModulo60 >= 0;

final int oldCalculation = new BigDecimal("60").multiply(offset.remainder(BigDecimal.ONE)).intValue();

// final int minutes = totalMinutesModulo60 == 0
final int minutes = totalMinutesModulo60 != 500000
? oldCalculation
: (hoursPositiveNumber == minutesPositiveNumber) ? totalMinutesModulo60 : Math.negateExact(totalMinutesModulo60); // This is for a half hour or 45 minute timezone, such as Newfoundland, Canada
dateString.append(ZoneOffset.ofHoursMinutes(offset.intValue(),
minutes)
.getId());
dateString.append(ZoneOffset.ofHoursMinutes(offset.intValue(), new BigDecimal("60").multiply(offset.remainder(BigDecimal.ONE)).intValue()).getId());
setDateTime(OffsetDateTime.parse(dateString.toString()));
}
else {
setDateTime(TemporalHelper.toOffsetDateTime(LocalDateTime.parse(dateString.toString())));
}

// This is the number of milliseconds to add to UTC
final int offset1 = TimeZone.getDefault().getOffset(new Date().toInstant().toEpochMilli());
final Integer intValueOfOffset = Optional.ofNullable(offset).map(BigDecimal::intValue).orElse(-1);

zoneId =
// null;
toZoneId(offset);

/*
0 = "+12:00"
1 = "+14:00"
2 = "-01:00"
3 = "-03:00"
4 = "-09:00"
5 = "-07:00"
6 = "-02:30"
7 = "-05:00"
8 = "+03:00"
9 = "+01:00"
10 = "+04:30"
11 = "+07:00"
12 = "+05:45"
13 = "+05:00"
14 = "+06:30"
15 = "+10:00"
16 = "+09:00"
17 = "Z"
18 = "-11:00"
19 = "-06:00"
20 = "+13:45"
21 = "+10:30"
22 = "+11:00"
23 = "+13:00"
24 = "-09:30"
25 = "-08:00"
26 = "-02:00"
27 = "-04:00"
28 = "+02:00"
29 = "+03:30"
30 = "+04:00"
31 = "+06:00"
32 = "+08:45"
33 = "+08:00"
34 = "-12:00"
35 = "+05:30"
36 = "+09:30"
37 = "-10:00"
*/

// zoneOffsetIds.stream()
// .filter(offsetId )
zoneId = toZoneId(offset);
}

private static ZoneId toZoneId(BigDecimal offset) {
Expand Down Expand Up @@ -280,17 +204,7 @@ private static boolean isZoneEquivalentToOffset(ZoneId zoneId, BigDecimal offset

final double zoneDouble = offsetHours.doubleValue();
final double offsetDouble = offset.doubleValue();
final boolean result = zoneDouble == offsetDouble;
return result;

// .map(zoneId -> LocalDateTime.now().atZone(zoneId))
// .map(ZonedDateTime::getOffset)
// .map(zonedDateTimeoffset -> zonedDateTimeoffset.get(ChronoField.OFFSET_SECONDS))

// .map(offsetSeconds -> offsetSeconds / 60)
// .map(offsetMinutes -> BigDecimal.valueOf(offsetMinutes).divide(BigDecimal.valueOf(60), RoundingMode.HALF_UP))
// .map(BigDecimal::doubleValue)
// .filter(bigDecimal -> bigDecimal.equals(offset))
return zoneDouble == offsetDouble;
}

public DateTime expandPartialMinFromPrecision(Precision thePrecision) {
Expand Down Expand Up @@ -363,12 +277,8 @@ public Integer compare(BaseTemporal other, boolean forSort) {
public OffsetDateTime getNormalized(Precision precision) {
return getNormalized(precision, zoneId);
}
public OffsetDateTime getNormalized(Precision precision, ZoneId nullableZoneId) {

// LUKETODO: remove this:
// zoneId = null;
// LUKETODO: for debugging only
final ZoneId aDefault = TimeZone.getDefault().toZoneId();
public OffsetDateTime getNormalized(Precision precision, ZoneId nullableZoneId) {
if (precision.toDateTimeIndex() > Precision.DAY.toDateTimeIndex()) {
if (nullableZoneId != null) {
return dateTime.atZoneSameInstant(nullableZoneId).toOffsetDateTime();
Expand All @@ -392,25 +302,12 @@ public Integer compareToPrecision(BaseTemporal other, Precision thePrecision) {

// adjust dates to evaluation offset
OffsetDateTime leftDateTime = this.getNormalized(thePrecision);
// LUKETODO; normalize to "this" zoneId?
OffsetDateTime rightDateTime = ((DateTime) other).getNormalized(thePrecision, getZoneId());

if (!leftMeetsPrecisionRequirements || !rightMeetsPrecisionRequirements) {
thePrecision = Precision.getLowestDateTimePrecision(this.precision, other.precision);
}


// final ZoneOffset offset1 = ZonedDateTime.now().getOffset();
// final BigDecimal offsetAsBigDecimal = TemporalHelper.zoneToOffset(offset1);

// GOOD: Mountain
// leftDateTime = {OffsetDateTime@3605} "2000-03-15T05:30:25.200-07:00"
// rightDateTime = {OffsetDateTime@3610} "2000-03-15T05:14:47.500-07:00"

// BAD: Newfoundland
// leftDateTime = {OffsetDateTime@3770} "2000-03-15T09:00:25.200-03:30"
// rightDateTime = {OffsetDateTime@3775} "2000-03-15T08:44:47.500-03:30"

for (int i = 0; i < thePrecision.toDateTimeIndex() + 1; ++i) {
int leftComp = leftDateTime.get(Precision.getDateTimeChronoFieldFromIndex(i));
int rightComp = rightDateTime.get(Precision.getDateTimeChronoFieldFromIndex(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static org.testng.Assert.assertTrue;

// LUKETODO: this test suite is failing with performance problems
public class CqlPerformanceIT extends CqlTestBase {

private static final Integer ITERATIONS = 200;
Expand Down

0 comments on commit 4ed57ff

Please sign in to comment.