Check 30 minute interval pair and ignore if pair not found - c#

I'm expecting the following sequence of time data in 30 minute interval from another system. I need to make this 30 minute pair in to one hour interval:
e.g.
Source Data:
Start:7:00
End: 7:30
Start:7:30
End: 8:00
Start:8:00
End: 8:30
Start:8:30
End: 9:00
Convert in to hour data like below:
Start:7:00
End: 8:00
Start:8:00
End: 9:00
If there's no match for 30 minute block this record should be ignored:
e.g
Start:7:00
End: 7:30
Start:7:30
End: 8:00
Start:8:00
End: 8:30
Start:8:30
End: 9:00
Start:9:00 <-- Ignore this
End: 9:30 <-- Ignore this
Can someone please suggest how this can be achieved?

The general idea for my solution is that you simply only take elements at indices:
0, 3, 4, 7, 8, 11, 12, ...
Or in other words - if index % 4 is 0 or 3.
Before doing that, we check if there are leftover dates at the end with count % 4.
If there are - we cut them.
This assumes that the dates will be all valid as the input you illustrated.
Here is my implementation:
public static IEnumerable<DateTime> CutTimes(IEnumerable<DateTime> source)
{
var count = source.Count();
var ignore = count % 4;
if (ignore != 0)
{
source = source.Take(count - ignore);
}
return source.Where((time, index) => index % 4 == 0 || index % 4 == 3);
}
Usage:
var times = new[]
{
new DateTime(2012, 5, 10, 7, 00, 0),
new DateTime(2012, 5, 10, 7, 30, 0),
new DateTime(2012, 5, 10, 7, 30, 0),
new DateTime(2012, 5, 10, 8, 00, 0),
new DateTime(2012, 5, 10, 8, 00, 0),
new DateTime(2012, 5, 10, 8, 30, 0),
new DateTime(2012, 5, 10, 8, 30, 0),
new DateTime(2012, 5, 10, 9, 00, 0),
new DateTime(2012, 5, 10, 9, 00, 0),
new DateTime(2012, 5, 10, 9, 30, 0)
};
var result = CutTimes(times);
foreach (var time in result)
{
Console.WriteLine(time);
}
Output:
10/05/2012 07:00:00
10/05/2012 08:00:00
10/05/2012 08:00:00
10/05/2012 09:00:00

Related

C# - Searching Time between 2 DateTime Objects of different days

i have a list of "event" objects.
In every event i have "EventStartTime" and "EventEndTime" declared as DateTime objects.
I want to be able to search "events" by time , for example 10:00,
the "event" you see below shows that the festival starts at 22:00 on Feb 17th,
and ends at 15:00 the following day. i have a couple more like these.
new EventsManager.Event() //3
{
EventType = EventsManager.EventType.Festival,
EventName = "Twistival",
EventPlace = placeList[4],
EventStartTime =new DateTime(2017,02,17,22,0,0),
EventEndTime = new DateTime(2017,02,18,15,0,0),
EventNumberOfParticipants = 8000
},
So when i search for event that occur, or still occurring at at 10:00
i should get this event.
any suggestions?
Assuming that you have a specific time of day that you want to determine if the event covers regardless of the date it covers it on then there are 4 cases you need to consider. First if the dates are more than 1 day apart they cover all times of day. If the start is before the time of day and the end is after the time of day it will cover the time. The last two cases require that the end date be on the next day from the start date, then either the start date is before the time of day, or the end date is after the time of day. Note that this also assumes that the start date is before the end date.
var events = new List<Tuple<DateTime, DateTime>>
{
// start and end after time of day but on different days
Tuple.Create(
new DateTime(2017, 02, 17, 22, 0, 0),
new DateTime(2017, 02, 18, 15, 0, 0)),
// start and end before time of day but on different days
Tuple.Create(
new DateTime(2017, 02, 17, 9, 0, 0),
new DateTime(2017, 02, 18, 7, 0, 0)),
// start before and end after same day
Tuple.Create(
new DateTime(2017, 02, 17, 9, 0, 0),
new DateTime(2017, 02, 17, 11, 0, 0)),
// covers more than 1 day
Tuple.Create(
new DateTime(2017, 02, 17, 22, 0, 0),
new DateTime(2017, 02, 18, 22, 0, 1)),
// start after and end before on different days
Tuple.Create(
new DateTime(2017, 02, 17, 22, 0, 0),
new DateTime(2017, 02, 18, 10, 0, 0)),
// start and end before on same day
Tuple.Create(
new DateTime(2017, 02, 17, 7, 0, 0),
new DateTime(2017, 02, 17, 8, 0, 0)),
// start and end after on same day
Tuple.Create(
new DateTime(2017, 02, 17, 11, 0, 0),
new DateTime(2017, 02, 17, 12, 0, 0)),
};
var timeOfDay = new TimeSpan(0, 10, 0 ,0);
foreach (var x in events)
{
if (x.Item2 - x.Item1 > TimeSpan.FromDays(1)
|| (x.Item1.TimeOfDay < timeOfDay && x.Item2.TimeOfDay > timeOfDay)
|| (x.Item1.Date < x.Item2.Date
&& (x.Item1.TimeOfDay < timeOfDay || x.Item2.TimeOfDay > timeOfDay)))
{
Console.WriteLine(x);
}
}
Will output
(2/17/2017 10:00:00 PM, 2/18/2017 3:00:00 PM)
(2/17/2017 9:00:00 AM, 2/18/2017 7:00:00 AM)
(2/17/2017 9:00:00 AM, 2/17/2017 11:00:00 AM)
(2/17/2017 10:00:00 PM, 2/18/2017 10:00:01 PM)
Let's say you have a
List<Event> Events;
of your Events. You can create a simple LINQ query to get all events running at a special time with a simple method like
private IEnumerable<Event> GetRunningEvents(DateTime time)
{
return Events.Where(E => E.EventStartTime <= time && E.EventEndTime >= time);
}
Dont forget to add
using System.Linq;
to your file.
EDIT: Without LINQ a possible approach is
private List<Event> GetRunningEvents(DateTime time)
{
List<Event> RunningEvents = new List<Event>();
foreach(Event E in Events)
{
if (E.EventStartTime <= time && E.EventEndTime >= time)
{
RunningEvents.Add(E);
}
}
return RunningEvents;
}
Try Linq Where:
var list = new List<Event>();
var searchTime = DateTime.Now;
var result = list.Where(e => e.EventStartTime <= searchTime && searchTime <= e.EventEndTime).ToList();

Getting a list of date ranges that occur between the ranges of two lists of date ranges

Given I have a list of KeyValuePair<DateTime,DateTime> (which represent date ranges available), and I have another list of same type (which represent date ranges not available), then I need to end up with a list of date ranges that are within the first list's ranges, but with ranges in first list being sliced by ranges in the second list. It may be that second list only has one entry, but it covers the entire available range, in which case the result should be empty.
To illustrate:
available: |~| |~~~~| |~~| |~~~~~~~~~~~~~~~~~~~~~|
unavailab: |~~~~~~~~~| |~| |~~~~~~| |~~~~~~~~|
expected : |~| |~| |~~~~~~~| |~~|
Or, in code, if you prefer (not same as illustration):
IList<KeyValuePair<DateTime, DateTime>> scheduleDates = new List<KeyValuePair<DateTime, DateTime>>()
{
new KeyValuePair<DateTime, DateTime>(new DateTime(2016, 11, 15, 9, 0, 0), new DateTime(2016, 11, 15, 12, 0, 0)),
new KeyValuePair<DateTime, DateTime>(new DateTime(2016, 11, 15, 15, 0, 0), new DateTime(2016, 11, 15, 21, 0, 0))
};
IList<KeyValuePair<DateTime, DateTime>> holidayDates = new List<KeyValuePair<DateTime, DateTime>>()
{
new KeyValuePair<DateTime, DateTime>(new DateTime(2016, 11, 15, 11, 0, 0), new DateTime(2016, 11, 15, 16, 0, 0)),
new KeyValuePair<DateTime, DateTime>(new DateTime(2016, 11, 15, 17, 0, 0), new DateTime(2016, 11, 15, 18, 0, 0)),
new KeyValuePair<DateTime, DateTime>(new DateTime(2016, 11, 15, 20, 0, 0), new DateTime(2016, 11, 15, 24, 0, 0)),
};
IList<KeyValuePair<DateTime, DateTime>> availability = new List<KeyValuePair<DateTime, DateTime>>()
{
new KeyValuePair<DateTime, DateTime>(new DateTime(2016, 11, 15, 9, 0, 0), new DateTime(2016, 11, 15, 11, 0, 0)),
new KeyValuePair<DateTime, DateTime>(new DateTime(2016, 11, 15, 16, 0, 0), new DateTime(2016, 11, 15, 17, 0, 0)),
new KeyValuePair<DateTime, DateTime>(new DateTime(2016, 11, 15, 18, 0, 0), new DateTime(2016, 11, 15, 20, 0, 0))
};
What would be the most efficient method of doing this? I can only think of a brute force, and surely that's not the way to go. Note that both input lists and/or the output list could be empty.

C# datetime array

I have two arrays, array1 has datetime data count by minute from 8am to 2pm and array2 has datetime data count by hour from same date 8am to 1pm.
I want to output the index number of two array that has same datetime.hour. and it should matchup the last available index number of array2 for all of the datetime data from array1 that later than array2.
for example if I have two datetime array like this:
DateTime[] dateTimes1 = new DateTime[]
{
new DateTime(2010, 10, 1, 8, 15, 0),
new DateTime(2010, 10, 1, 8, 30, 1),
new DateTime(2010, 10, 1, 8, 45, 2),
new DateTime(2010, 10, 1, 9, 15, 3),
new DateTime(2010, 10, 1, 9, 30, 4),
new DateTime(2010, 10, 1, 9, 45, 5),
new DateTime(2010, 10, 1, 10, 15, 6),
new DateTime(2010, 10, 1, 10, 30, 7),
new DateTime(2010, 10, 1, 10, 45, 8),
new DateTime(2010, 10, 1, 11, 15, 9),
new DateTime(2010, 10, 1, 11, 30, 10),
new DateTime(2010, 10, 1, 11, 45, 11),
new DateTime(2010, 10, 1, 12, 15, 12),
new DateTime(2010, 10, 1, 12, 30, 13),
new DateTime(2010, 10, 1, 12, 45, 14),
new DateTime(2010, 10, 1, 13, 15, 15),
new DateTime(2010, 10, 1, 13, 30, 16),
new DateTime(2010, 10, 1, 13, 45, 17),
new DateTime(2010, 10, 1, 14, 15, 18),
new DateTime(2010, 10, 1, 14, 30, 19),
new DateTime(2010, 10, 1, 14, 45, 20),
};
DateTime[] dateTimes2 = new DateTime[]
{
new DateTime(2010, 10, 1, 8, 0, 0),
new DateTime(2010, 10, 1, 9, 0, 1),
new DateTime(2010, 10, 1, 10, 0, 2),
new DateTime(2010, 10, 1, 11, 0, 3),
new DateTime(2010, 10, 1, 12, 0, 4),
new DateTime(2010, 10, 1, 13, 0, 5),
};
it should gives me the output:
0, 0
1, 0
2, 0
3, 1
4, 1
5, 1
6, 2
7, 2
8, 2
9, 3
10, 3
11, 3
12, 4
13, 4
14, 4
15, 5
16, 5
17, 5
18, 5
19, 5
20, 5
This is what I have tried:
int i = 0;
int j = 0;
while (i < dateTimes1.Length && j < dateTimes2.Length)
{
if (dateTimes1[i].Date == dateTimes2[j].Date && dateTimes1[i].Hour == dateTimes2[j].Hour)
{
list.Add(i);
list2.Add(j);
i++;
}
else if (dateTimes1[i] < dateTimes2[j])
{
i++;
}
else if (dateTimes1[i] > dateTimes2[j])
{
j++;
}
}
for (int k = 0; k < list.Count; k++)
{
Console.WriteLine(list[k] + " , " + list2[k];
}
but it won't output the index number after 1pm.
Your two lists are not the same length. In your while statement you are trying to iterate two different length lists at the same time.
If I understand your requirements properly you should be doing something like this by using an inner loop:
DateTime[] dateTimes1 = new DateTime[]
{
new DateTime(2010, 10, 1, 8, 15, 0),
new DateTime(2010, 10, 1, 8, 30, 1),
new DateTime(2010, 10, 1, 8, 45, 2),
new DateTime(2010, 10, 1, 9, 15, 3),
new DateTime(2010, 10, 1, 9, 30, 4),
new DateTime(2010, 10, 1, 9, 45, 5),
new DateTime(2010, 10, 1, 10, 15, 6),
new DateTime(2010, 10, 1, 10, 30, 7),
new DateTime(2010, 10, 1, 10, 45, 8),
new DateTime(2010, 10, 1, 11, 15, 9),
new DateTime(2010, 10, 1, 11, 30, 10),
new DateTime(2010, 10, 1, 11, 45, 11),
new DateTime(2010, 10, 1, 12, 15, 12),
new DateTime(2010, 10, 1, 12, 30, 13),
new DateTime(2010, 10, 1, 12, 45, 14),
new DateTime(2010, 10, 1, 13, 15, 15),
new DateTime(2010, 10, 1, 13, 30, 16),
new DateTime(2010, 10, 1, 13, 45, 17),
new DateTime(2010, 10, 1, 14, 15, 18),
new DateTime(2010, 10, 1, 14, 30, 19),
new DateTime(2010, 10, 1, 14, 45, 20),
};
DateTime[] dateTimes2 = new DateTime[]
{
new DateTime(2010, 10, 1, 8, 0, 0),
new DateTime(2010, 10, 1, 9, 0, 1),
new DateTime(2010, 10, 1, 10, 0, 2),
new DateTime(2010, 10, 1, 11, 0, 3),
new DateTime(2010, 10, 1, 12, 0, 4),
new DateTime(2010, 10, 1, 13, 0, 5),
};
int i = 0;
while (i < dateTimes1.Length)
{
int j = 0;
while (j < dateTimes2.Length))
{
if (dateTimes1[i].Date == dateTimes2[j].Date && dateTimes1[i].Hour == dateTimes2[j].Hour)
{
list.Add(i);
list2.Add(j);
i++;
}
else if (dateTimes1[i] < dateTimes2[j])
{
i++;
}
else if (dateTimes1[i] > dateTimes2[j])
{
j++;
}
}
}
for (int k = 0; k < list.Count; k++)
{
Console.WriteLine(list[k] + " , " + list2[k];
}
Here's a pretty basic method using Array.FindIndex and foreach:
EDIT: Updated this answer to handle the "matchup the last available index number of array2 for all of the datetime data from array1 that later than array2." issue.
foreach (DateTime dt in dateTimes1)
{
int currentHour = dt.Hour;
int lastHour = dateTimes2[dateTimes2.GetUpperBound(0)].Hour; //GetUpperBound(0) is the last index
int dt1index = Array.FindIndex(dateTimes1, a => a == dt); //get the index of the current item in dateTimes1
int dt2index = Array.FindIndex(dateTimes2, x => x.Hour == currentHour); //get the index of the item in dateTimes2 matching dateTimes1 hour field
if (currentHour > lastHour)
{
Console.WriteLine("{0}, {1}", dt1index, dateTimes2.GetUpperBound(0));
}
else
{
Console.WriteLine("{0}, {1}", dt1index, dt2index);
}
}
This simply looks at each of the values in dateTimes1 and dateTimes2 and returns the first match it finds (very similar to your loop).
To determine dt1index, we look through dateTimes1 and return the first match where a => a == dt (a is just the predicate, representing the "current" value in dateTimes1 - think of i = 0,1,2,etc in a regular loop ).
Similarly, to determine dt2index, we look for the first match on x => x.Hour == dt.Hour -- that is, where the "current" dt's hour field matches the hour field in dateTimes2.
In both cases, the first match is returned - if no matches are found, -1 is returned.
When we go to write to the console, we check if currentHour is greater than the last hour in dateTimes2 if so, we just write the current index of dateTimes1 and the last index of dateTimes2. Otherwise, we write the current index of dateTimes1 and the index where the hour matches on dateTimes2.
Using Linq:
var hour = new TimeSpan(1, 0, 0);
var dt2MaxValue = dateTimes2.Max();
for (int i = 0; i < dateTimes1.Length; i++)
{
var output = string.Format("{0}, {1}",
i,
dateTimes2
.Select((o, index) => new { index = index, value = o })
.Where(dt2 => (dateTimes1[i] - dt2.value) < hour
|| dt2.value == dt2MaxValue)
.Select(dt2 => dt2.index)
.FirstOrDefault());
Console.WriteLine(output);
}
What the above Linq statement does:
The first Select uses that method's overload which also passes the index of the item. This simply allows that info to cascade through. It uses an anonymous object with both index and the collection item being the index and value properties, respectively.
The Where clause queries the collection of these anonymous objects and compares their value with dateTime1[i]. It gets the one where value is less than dateTime1[i] but not by more than 1 hour, OR if it is the maximum value in the whole collection.
The second Select simply gets the indexes of the items that Where filtered through.
And FirstOrDefault returns just that (ie, the first or default, which is the index of the item selected or 0 if no item was selected).

Calculating "working time" using TimePeriod.NET's CalendarPeriodCollector gives unexpected results

I'm trying to calculate a due date for a service level agreement, and at the same time, I also need to back calculate the service level agreement in the other direction.
I've been struggling with calculations for "working time" (i.e. the time that work is possible during a set of days), and decided to use a third party library called TimePeriodLibrary.NET for the task. I need to be able to do two things:
Given a start DateTime and a TimeSpan, you should receive a DateTime of when a service level agreement date is due (date due).
Given a start DateTime and an end DateTime, you should receive a TimeSpan of how long that service level agreement should take.
All source code (test project is on GitHub). I have a ServiceLevelManager class that does all the work. It take a list of WorkDays and HolidayPeriods, in order to work out which hours are available to be worked. The CalendarPeriodCollector class is giving unexpected results. The expectations that do work in determining the due date from a timespan, do not calculate correctly when I back calculate them.
Can anyone see whether I am doing something wrong, or whether the library has a bug?
namespace ServicePlanner
{
using System;
using System.Collections.Generic;
using Itenso.TimePeriod;
public class ServicePlannerManager
{
public ServicePlannerManager(IEnumerable<WorkDay> workDays, IEnumerable<HolidayPeriod> holidays)
{
this.WorkDays = workDays;
this.Holidays = holidays;
}
public IEnumerable<WorkDay> WorkDays { get; set; }
public IEnumerable<HolidayPeriod> Holidays { get; set; }
public TimeSpan GetRemainingWorkingTime(DateTime start, DateTime dueDate)
{
var filter = new CalendarPeriodCollectorFilter();
foreach (var dayOfWeek in this.WorkDays)
{
filter.CollectingDayHours.Add(new DayHourRange(dayOfWeek.DayOfWeek, new Time(dayOfWeek.StartTime), new Time(dayOfWeek.EndTime)));
}
foreach (var holiday in this.Holidays)
{
filter.ExcludePeriods.Add(new TimeBlock(holiday.StartTime, holiday.EndTime));
}
var range = new CalendarTimeRange(start, dueDate);
var collector = new CalendarPeriodCollector(filter, range);
collector.CollectHours();
var duration = collector.Periods.GetTotalDuration(new TimeZoneDurationProvider(TimeZoneInfo.FindSystemTimeZoneById("UTC")));
return duration;
//var rounded = Math.Round(duration.TotalMinutes, MidpointRounding.AwayFromZero);
//return TimeSpan.FromMinutes(rounded);
}
}
}
The Unit tests that are failing are extracted below:
[TestFixture]
public class ServicePlannerManagerTest
{
[Test, TestCaseSource("LocalSource")]
public void GetRemainingWorkingTimeWithHolidayShouldOnlyEnumerateWorkingTime(DateTime startTime, TimeSpan workingHours, DateTime expectedDueDate, string expectation)
{
// Arrange
var workDays = new List<WorkDay>
{
new WorkDay(DayOfWeek.Monday, new DateTime(1, 1, 1, 9, 0, 0), new DateTime(1, 1, 1, 17, 0, 0)),
new WorkDay(DayOfWeek.Tuesday, new DateTime(1, 1, 1, 9, 0, 0), new DateTime(1, 1, 1, 17, 0, 0)),
new WorkDay(DayOfWeek.Wednesday, new DateTime(1, 1, 1, 9, 0, 0), new DateTime(1, 1, 1, 17, 0, 0)),
new WorkDay(DayOfWeek.Thursday, new DateTime(1, 1, 1, 9, 0, 0), new DateTime(1, 1, 1, 17, 0, 0)),
new WorkDay(DayOfWeek.Friday, new DateTime(1, 1, 1, 9, 0, 0), new DateTime(1, 1, 1, 17, 0, 0)),
};
var holidayPeriods = new List<HolidayPeriod>
{
new HolidayPeriod(new DateTime(2015, 9, 15, 00, 0, 0), new DateTime(2015, 9, 16, 0, 0, 0))
};
var service = new ServicePlannerManager(workDays, holidayPeriods);
// Act
var result = service.GetRemainingWorkingTime(startTime, expectedDueDate);
// Assert -
Assert.AreEqual(workingHours.TotalHours, result.TotalHours, expectation);
}
protected IEnumerable LocalSource()
{
yield return
new TestCaseData(
new DateTime(2015, 9, 14, 9, 0, 0),
new TimeSpan(23, 0, 0),
new DateTime(2015, 9, 17, 16, 0, 0),
"5. Expected 23 hours of working time to end on the 17/09/2015 16:00. Monday to Thursday evening. Just short of 3 full working days by one hour. Tuesday is holiday.");
}
}
Output of this test is
5. Expected 23 hours of working time to end on the 17/09/2015 16:00. Monday to Thursday evening. Just short of 3 full working days by one hour. Tuesday is holiday.
Expected: 23.0d
But was: 15.999999999944444d
I want to know if I am using the collector incorrectly, or if the collector has a bug.
This looks like a great library for solving a familiar problem.
The best thing to do is to output the periods in the period collection to help you debug the problem.
I've rewritten your test to use the base types in the examples from their documentation:
[Test, TestCaseSource("LocalSource")]
public void SO_GetRemainingWorkingTimeWithHolidayShouldOnlyEnumerateWorkingTime(DateTime startTime,
TimeSpan workingHours, DateTime expectedDueDate, string expectation)
{
CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter();
filter.Months.Add(YearMonth.September); // only Januaries
filter.WeekDays.Add(DayOfWeek.Monday); //
filter.WeekDays.Add(DayOfWeek.Tuesday); //
filter.WeekDays.Add(DayOfWeek.Wednesday); //
filter.WeekDays.Add(DayOfWeek.Thursday); //
filter.WeekDays.Add(DayOfWeek.Friday); //
filter.CollectingHours.Add(new HourRange(9, 17)); // working hours
CalendarTimeRange testPeriod = new CalendarTimeRange(startTime, expectedDueDate);//new DateTime(2015, 9, 14, 9, 0, 0), new DateTime(2015, 9, 17, 18, 0, 0));
Console.WriteLine("Calendar period collector of period: " + testPeriod);
filter.ExcludePeriods.Add(new TimeBlock(new DateTime(2015, 9, 15, 00, 0, 0), new DateTime(2015, 9, 16, 0, 0, 0)));
CalendarPeriodCollector collector = new CalendarPeriodCollector(filter, testPeriod);
collector.CollectHours();
foreach (ITimePeriod period in collector.Periods)
{
Console.WriteLine("Period: " + period); // THIS WILL HELP A LOT!
}
var result = collector.Periods.GetTotalDuration(new TimeZoneDurationProvider(TimeZoneInfo.FindSystemTimeZoneById("UTC")));
Console.WriteLine(result);
//
}
This results in:
Calendar period collector of period: 14/09/2015 09:00:00 - 17/09/2015 15:59:59 | 3.06:59
Period: 14/09/2015 09:00:00 - 14/09/2015 16:59:59 | 0.07:59
Period: 16/09/2015 09:00:00 - 16/09/2015 16:59:59 | 0.07:59
15:59:59.9999998
So what I've noticed is that the very last period is missing.
If you change the end time of your period from 4PM to 6PM (and therefore expect an extra hour = 24) it will just about pass. (you will also need to round the result)
So it looks like the periods need to be completely covered by the total duration, partial coverage is not counted. You may be able to change the options of the library, alternatively you may be able to add each hour of the working day as separate CollectingHours (hacky)
Hope that gets you closer to the answer you need!

Given a start and end dates get the most complete intervals in years / month / day / instantaneous

Given a start and end DateTime how can i get the most complete intervals in terms of "natural" years, months, days, instantaneous ?
Thanks for your help!
[TestMethod]
public void MoreThanOneYear()
{
var start = new DateTime(2013, 1, 5, 2, 0, 0);
var end = new DateTime(2015, 5, 5, 19, 0, 0);
var intervals = DateTimeUtis.GetPeriods(start, end).ToList();
Assert.IsTrue(intervals.Count() == 7);
Assert.IsTrue(intervals[0].Granularity == Granularity.Instantaneous);
Assert.IsTrue(intervals[0].Interval.Start == new DateTime(2013, 1, 5, 2, 0, 0));
Assert.IsTrue(intervals[0].Interval.End == new DateTime(2013, 1, 6, 0, 0, 0));
Assert.IsTrue(intervals[1].Granularity == Granularity.Daily);
Assert.IsTrue(intervals[1].Interval.Start == new DateTime(2013, 1, 6, 0, 0, 0));
Assert.IsTrue(intervals[1].Interval.End == new DateTime(2013, 2, 1, 0, 0, 0));
Assert.IsTrue(intervals[2].Granularity == Granularity.Montly);
Assert.IsTrue(intervals[2].Interval.Start == new DateTime(2013, 2, 1, 0, 0, 0));
Assert.IsTrue(intervals[2].Interval.End == new DateTime(2014, 1, 1, 0, 0, 0));
Assert.IsTrue(intervals[3].Granularity == Granularity.Yearly);
Assert.IsTrue(intervals[3].Interval.Start == new DateTime(2014, 1, 1, 0, 0, 0));
Assert.IsTrue(intervals[3].Interval.End == new DateTime(2015, 1, 1, 0, 0, 0));
Assert.IsTrue(intervals[4].Granularity == Granularity.Montly);
Assert.IsTrue(intervals[4].Interval.Start == new DateTime(2015, 1, 1, 0, 0, 0));
Assert.IsTrue(intervals[4].Interval.End == new DateTime(2015, 5, 1, 0, 0, 0));
Assert.IsTrue(intervals[5].Granularity == Granularity.Daily);
Assert.IsTrue(intervals[5].Interval.Start == new DateTime(2015, 5, 1, 0, 0, 0));
Assert.IsTrue(intervals[5].Interval.End == new DateTime(2015, 5, 5, 0, 0, 0));
Assert.IsTrue(intervals[6].Granularity == Granularity.Instantaneous);
Assert.IsTrue(intervals[6].Interval.Start == new DateTime(2015, 5, 5, 0, 0, 0));
Assert.IsTrue(intervals[6].Interval.End == new DateTime(2015, 5, 5, 19, 0, 0));
}
Edit
What I'm trying to achieve is to the get the biggest adjacent intervals possible between the two dates. Complete years interval > Months > Days > Instantaneous.
Edit 2
The business scenario is that I have an SQL table with raw data with a 1 minute frequency and 3 other tables with consolidated data (by year, month and day). So for a start and end date I need to get the date, time intervals that allow me to optimize the queries. If I have full years between the start and end date I can get data from the consolidate year table, if not, I try to get data from the month's table and so on.
Generally, you cannot. Simply because there is no standard for year and month. The best approximation you can do is to decide what will be the most appropriate year in your case (365 days, 365.25 days, or maybe some other rule) and use TimeSpan.TotalDays/numberOfDaysInYear.
See also: Difference in months between two dates

Categories