tam tam
tam tam

Reputation: 1900

Creating a Date range from a list of dates

I am trying to create a set of date ranges from a list of dates.

These dates are present in my object. Basically I would iterate through each row and will keep on iterating and when the time-span difference is greater than 5 minutes I will stop and use the end points as a date range. I have an algorithm below but the problem is it excludes many data rows:

Please see below the sample data and desired output

**Sample Data**

  Start_Date   Start_Date_Time    Replicate
    12.12.2012   8:22:58            10
    12.12.2012   8:22:58            30
    12.12.2012   8:22:58            31
    12.12.2012   8:22:58            32
    12.12.2012   8:22:58            33
    12.12.2012   8:22:58            34 
    12.14.2012   9:49:27            54
    12.14.2012   9:49:27            55
    12.14.2012   9:49:27            78
    12.14.2012   9:49:27            99
    12.14.2012   9:58               120
    12.14.2012   9:58               140
    12.14.2012   9:58               142
    12/12/2012   9:59               144
    12/12/2012   9:59               146
    12/12/2012   9:59               148
    12/12/2012   9:59               150

**Desired Output**
Date Ranges
8:22:58-8:22:58   Replicate10-34
9:49:27-9:49:27   Replicate54-99
9:58-9:59         Replicate120-150

My code gives me results but it excludes many rows:

lf.ReplicateBlocks.OrderBy(x => x.InitiationDate);

The initiationDate above is the StartDate and Start Time. I have sorted the list above in ascending order to start from the minimum date/time:

 DateTime minimumDateTime = DateTime.MinValue;

 foreach (RunLog.Domain.Entities.ReplicateBlock rb in lf.ReplicateBlocks)
 {
   TimeSpan intervalMinutes = rb.InitiationDate.Subtract(minimumDateTime);

   if (intervalMinutes.TotalMinutes >= 5)
   {
     minimumDateTime = rb.InitiationDate;

     //minDates.Add(minimumDateTime);

     UserConfirmationErrors confirmationRun = new UserConfirmationErrors();
     confirmationRun.minDate = rb.InitiationDate;
     confirmationRun.replicateID = rb.ReplicateId;

     uc.userConfirmationList.Add(confirmationRun);
   }
 }

 List<RunLog.Domain.Entities.RunLogEntryDatesDisplay> reDisplay = new List<Domain.Entities.RunLogEntryDatesDisplay>();

 foreach (var minDate in uc.userConfirmationList)
 {
   RunLog.Domain.Entities.RunLogEntryDatesDisplay red = new Domain.Entities.RunLogEntryDatesDisplay();
   reDisplay.Add(new Domain.Entities.RunLogEntryDatesDisplay() { runDate = minDate.minDate, DateRange = string.Format("{0} - {1}", minDate.minDate, minDate.minDate.AddMinutes(5)), MinimumReplicateId = minDate.replicateID.ToString() });
 }

 //return reDisplay.OrderByDescending(t => t.runDate).ToList();
 return reDisplay;

Once the user Confirmation List with Date Ranges is formed, I send it to the view in the form of a checkbox list, users selects those dates and I take the selected dates and look for those records again below:

  var query = from d in selectedDates
                    from o in lf.ReplicateBlocks
                    where (d.Checked &&
                          o.InitiationDate >= d.runDate &&
                          o.InitiationDate <= d.runDate.AddMinutes(5))
                    select o;

Upvotes: 2

Views: 1212

Answers (1)

James
James

Reputation: 82096

I would tidy this up a bit. First create a class to represent your date range. With all the data in the there, you could even override the ToString() method to output the format you need e.g.

public class ReplicationDateRange
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int StartId { get; set; }
    public int EndId { get; set; }
    public override string ToString()
    {
        return String.Format("{0}-{1} Replicate {2}-{3}", StartDate.ToShortDateString(), EndDate.ToShortDateString(), StartId, EndId);
    }
}

Then what you need to do is keep iterating the list until you hit a date which isn't within 5 minutes of the last baseline, but also updating the end date/id of the current range. The following should achieve this:

var dateRanges = new List<ReplicationDateRange>();
DateTime baselineDate = DateTime.MinValue;
ReplicationDateRange currentDateRange = null;
foreach (var block in lf.ReplicationBlocks.OrderBy(x => x.InitiationDate))
{
    if ((block.InitiationDate - baselineDate).TotalMinutes <= 5)
    {
        currentDateRange.EndDate = block.InitiationDate;
        currentDateRange.EndId = block.ReplicateId;
    }
    else
    {
        baselineDate = block.InitiationDate;
        currentDateRange = new ReplicationDateRange()
        {
            StartDate = block.InitiationDate,
            EndDate = block.InitiationDate,
            StartId = block.ReplicateId,
            EndId = block.ReplicateId
        };
        dateRanges.Add(currentDateRange);
    }
}
foreach (var d in dateRanges)
{
    Console.WriteLine(d);
}

Upvotes: 1

Related Questions