Skip to content

Commit

Permalink
Merge pull request #203 from ADAPT/develop
Browse files Browse the repository at this point in the history
PAN-UTC Reconciliation Logic
  • Loading branch information
Stuart Rhea authored Dec 7, 2022
2 parents aff2467 + af158e7 commit 1b98d4c
Showing 1 changed file with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ private void SetNumericMeterValue(ISOSpatialRow isoSpatialRow, NumericWorkingDat
{
//There are multiple product allocations for the device element
//Find the product allocation that governs this timestamp
ISOProductAllocation relevantPan = productAllocationsForDeviceElement.FirstOrDefault(p => Offset(p.AllocationStamp.Start) <= spatialRecord.Timestamp &&
(p.AllocationStamp.Stop == null ||
Offset(p.AllocationStamp.Stop) >= spatialRecord.Timestamp));
ISOProductAllocation relevantPan = productAllocationsForDeviceElement.FirstOrDefault(p => GovernsTimestamp(p, spatialRecord));
if (relevantPan == null)
{
//We couldn't correlate strictly based on time. Check for a more general match on date alone before returning null.
Expand Down Expand Up @@ -187,6 +185,50 @@ private void SetNumericMeterValue(ISOSpatialRow isoSpatialRow, NumericWorkingDat
}
}

/// <summary>
/// Return true if the product allocation governs the timestamp for the SpatialRecord
/// </summary>
private bool GovernsTimestamp(ISOProductAllocation p, SpatialRecord spatialRecord)
{
DateTime? allocationStart = Offset(p.AllocationStamp.Start);
DateTime? allocationStop = p.AllocationStamp.Stop != null ? Offset(p.AllocationStamp.Stop) : null;
DateTime spatialRecordTimestampUtc = ToUtc(spatialRecord.Timestamp);

return
ToUtc(allocationStart) <= spatialRecordTimestampUtc &&
(p.AllocationStamp.Stop == null || ToUtc(allocationStop) >= spatialRecordTimestampUtc);
}

// Comparing DateTime values with different Kind values leads to inaccurate results.
// Convert DateTimes to UTC if possible before comparing them
private DateTime? ToUtc(DateTime? nullableDateTime)
{
return nullableDateTime.HasValue ? ToUtc(nullableDateTime.Value) : nullableDateTime;
}

private DateTime ToUtc(DateTime dateTime)
{
if (dateTime.Kind == DateTimeKind.Utc)
return dateTime;

DateTime utc;
if (dateTime.Kind == DateTimeKind.Local)
{
utc = dateTime.ToUniversalTime();
}
else if (dateTime.Kind == DateTimeKind.Unspecified && _taskDataMapper.GPSToLocalDelta.HasValue)
{
utc = new DateTime(dateTime.AddHours(- _taskDataMapper.GPSToLocalDelta.Value).Ticks, DateTimeKind.Utc);
}
else
{
// Nothing left to try; return original value
utc = dateTime;
}

return utc;
}

private DateTime? Offset(DateTime? input)
{
if (_effectiveTimeZoneOffset.HasValue && input.HasValue)
Expand Down

0 comments on commit 1b98d4c

Please sign in to comment.