public decimal GetBalanceFromDate(DateTime date) { FinancialDay closestDay = null; TimeSpan? closestDistance = null; // Find the closest day foreach (var day in Calendar.Days) { TimeSpan distanceToDate; // There is no "absolute value" function for TimeSpan, so we should handle negative numbers by avoiding them. if (day.Date > date) { distanceToDate = day.Date - date; } else { distanceToDate = date - day.Date; } if (!closestDistance.HasValue || distanceToDate < closestDistance.Value) { closestDay = day; closestDistance = distanceToDate; } } if (closestDay == null) { return(0); } // If the day we found is after the date we wanted, we should extrapolate backwards using the starting balance. // If the day we found is either exactly date we wanted or after it, we should use the ending balance as standard or to extrapolate forwards. if (closestDay.Date > date) { return(GetDelta(closestDay.StartingBalance)); } else { return(GetDelta(closestDay.EndingBalance)); } }
private void InsertDay(int index, ref FinancialDay day) { if (index > 0) { var previous = DayCollection[index - 1]; previous.NextDay = day; day.PreviousDay = previous; } if (index < DayCollection.Count) { var next = DayCollection[index]; next.PreviousDay = day; day.NextDay = next; } DayCollection.Insert(index, day); OnDayCollectionChanged(); }
public FinancialDay GetDayForDate(DateTime date) { FinancialDay returnDay; lock (DayCollection) { var existing = FindDayForDate(date, out int index); if (existing != null) { returnDay = existing; } else { returnDay = new FinancialDay(date); InsertDay(index, ref returnDay); } } return(returnDay); }