public static QuarterlyConsumptionResult ComputeQuarterlyConsumption (QuarterlyConsumptionParameters request) { var result = new QuarterlyConsumptionResult { NewReferenceReading = request.StartMeterReading }; var startQuarterNumber = request.StartDate.QuarterNumber(); var endQuarterNumber = request.CurrentMessage.Timestamp.QuarterNumber(); for (int q = startQuarterNumber + 1; q <= endQuarterNumber; q++) { var quarterDate = DateTimeExtensions.GetQuarterDate(q); var reading = GetIntermediateReading(request, quarterDate); result.Quarters.Add(new Tuple <DateTime, decimal>(quarterDate, reading - result.NewReferenceReading)); result.NewReferenceDate = quarterDate; result.NewReferenceReading = reading; result.NewReferenceQuarter = q; } return(result); }
private static decimal GetIntermediateReading(QuarterlyConsumptionParameters request, DateTime quarterDate) { if (quarterDate == request.CurrentMessage.Timestamp) { return(request.CurrentMessage.MeterValue); } var totalMilliSeconds = (decimal)(request.CurrentMessage.Timestamp - request.PreviousMessage.Timestamp).TotalMilliseconds; var quarterMilliSeconds = (decimal)(quarterDate - request.PreviousMessage.Timestamp).TotalMilliseconds; var totalConsumption = request.CurrentMessage.MeterValue - request.PreviousMessage.MeterValue; var quarterConsumption = totalConsumption * (quarterMilliSeconds - totalMilliSeconds); return(request.PreviousMessage.MeterValue + quarterConsumption); }