Find date ranges from a collection of dates C# - c#

Simple question. I have an ordered collection of dates. They are UK dates btw
01/01/10
01/02/10
01/03/10
01/04/10
02/04/10
03/04/10
04/04/10
And I want to convert this into a collection of date ranges
01/01/10 -> 01/01/10
01/02/10 -> 01/02/10
01/03/10 -> 01/03/10
01/04/10 -> 04/04/10
Just to clarify, I'm trying to convert any consecutive dates into a range. so the first 3 dates are stand alone and the last 4 get converted into a range 1st of April to 4th of April.
Now I can do this using loops but it's not very elegant. Does any one have any solutions out there that are?
Thanks

Given that you want to determine ranges of consecutive date ranges, I think your only option is, as you say a loop. You can do it in a single pass though, and put it in an extension method so it'll operate on any IList<DateTime>, for example:
// purely an example, chances are this will have actual, y'know logic in live
public class DateRange
{
private List<DateTime> dates = new List<DateTime>();
public void Add(DateTime date)
{
this.dates.Add(date);
}
public IEnumerable<DateTime> Dates
{
get { return this.dates; }
}
}
public static IEnumerable<DateRange> GetRanges(this IList<DateTime> dates)
{
List<DateRange> ranges = new List<DateRange>();
DateRange currentRange = null;
// this presumes a list of dates ordered by day, if not then the list will need sorting first
for( int i = 0; i < dates.Count; ++i )
{
var currentDate = dates[i];
if( i == 0 || dates[i - 1] != currentDate.AddDays(-1))
{
// it's either the first date or the current date isn't consecutive to the previous so a new range is needed
currentRange = new DateRange();
ranges.Add(currentRange);
}
currentRange.Add(currentDate);
}
return ranges;
}
You could also make it even more generic by passing in an IEnumerable<DateTime>:
public static IEnumerable<DateRange> GetRanges(this IEnumerable<DateTime> dates)
{
List<DateRange> ranges = new List<DateRange>();
DateRange currentRange = null;
DateTime? previousDate = null;
// this presumes a list of dates ordered by day, if not then the list will need sorting first
foreach( var currentDate in dates )
{
if( previousDate == null || previousDate.Value != currentDate.AddDays(-1) )
{
// it's either the first date or the current date isn't consecutive to the previous so a new range is needed
currentRange = new DateRange();
ranges.Add(currentRange);
}
currentRange.Add(currentDate);
previousDate = currentDate;
}
return ranges;
}

dates.Aggregate(new List<DateRange>(), (acc, dt) =>
{
if (acc.Count > 0 && acc.Last().d2 == dt.AddDays(-1))
acc[acc.Count - 1].d2 = dt;
else
acc.Add(new DateRange(dt, dt));
return acc;
}
);
where DateRange is a class like this:
class DateRange
{
public DateTime d1, d2;
public DateRange(DateTime d1, DateTime d2)
{
this.d1 = d1;
this.d2 = d2;
}
}

var stringDates = new List<string> {"01/09/10", "31/08/10", "01/01/10"};
var dates = stringDates.ConvertAll(DateTime.Parse);
dates.Sort();
var lastDateInSequence = new DateTime();
var firstDateInSequence = new DateTime();
foreach (var range in dates.GroupBy(
d => { if ((d - lastDateInSequence).TotalDays != 1)
firstDateInSequence = d;
lastDateInSequence = d;
return firstDateInSequence;
}))
{
var sb = new StringBuilder();
sb.Append(range.First().ToShortDateString());
sb.Append(" => ");
sb.Append(range.Last().ToShortDateString());
Console.WriteLine(sb.ToString());
}

Related

Find next 5 working days starting from today

I use nager.date to know if a day is a holiday day or a weekend day Saturday and Sunday).
I need to extract the date (starting from today or any other date) after 5 working days.
DateTime date1 = new DateTime(2019, 12, 23);
int i = 0;
while ( i < 5)
{
if (DateSystem.IsPublicHoliday(date1, CountryCode.IT) || DateSystem.IsWeekend(date1, CountryCode.IT))
{
date1 = date1.AddDays(1);
}
else
{
date1= date1.AddDays(1);
i++;
}
}
The problem of this code is that if the last else occurs, it add me 1 day but without doing any other check.
For example:
If the start date is 13/07/2020, the result will be at the end 18/07/2020 and as you can see is on Saturday.
How could I modify this code to achieve what I need?
The order is important. The AddDays should be called first, and after it is called we check if the new day matches our criteria.
Note: I have renamed the i variable so it is more clear.
DateTime date1 = new DateTime(2019, 12, 23);
int daysAdded = 0;
while (daysAdded < 5)
{
date1 = date1.AddDays(1);
if (!DateSystem.IsPublicHoliday(date1, CountryCode.IT) && !DateSystem.IsWeekend(date1, CountryCode.IT)) {
// We only consider laboral days
// laboral days: They are not holidays and are not weekends
daysAdded ++;
}
}
I always try to generalize my solutions, so here's one enabling LINQ:
public bool IsWorkingDay(DateTime dt)
=> !DateSystem.IsPublicHoliday(dt) && !DateSystem.IsWeekend(dt);
public DateTime NextWorkingDay(DateTime dt)
{
dt = dt.AddDays(1);
while (!IsWorkingDay(dt))
dt = dt.AddDays(1);
return dt;
}
public IEnumerable<DateTime> WorkingDaysFrom(DateTime dt)
{
if (!IsWorkingDay(dt))
dt = NextWorkingDay(dt); // includes initial dt, remove if unwanted
while (true)
{
yield return dt;
dt = NextWorkingDay(dt);
}
}
This will pump out working days from a given date until end of time, and then use LINQ to grab the number you want:
var next5 = WorkingDaysFrom(DateTime.Today).Take(5).ToList();
here's how to get all the working days in 2020:
var working2020 = WorkingDaysFrom(new DateTime(2020, 1, 1))
.TakeWhile(dt => dt.Year == 2020)
.ToList();
DateTime date1 = new DateTime(2019, 12, 23);
int i = 0;
while ( i < 5)
{
date1 = date1.AddDays(1);
if (!DateSystem.IsPublicHoliday(date1, CountryCode.IT) && !DateSystem.IsWeekend(date1, CountryCode.IT))
{
i++;
}
}
but I think that you need a DateTime[] to store all the five days
This is a better and a faster way to do this without using third party libraries.
DateTime nowDate = DateTime.Now;
DateTime expectedDate;
if (nowDate.DayOfWeek == DayOfWeek.Saturday)
{
expectedDate = nowDate.AddDays(6);
}
else if (nowDate.DayOfWeek == DayOfWeek.Sunday)
{
expectedDate = nowDate.AddDays(5);
}
else
{
expectedDate = nowDate.AddDays(7);
}
I thought about the problem, and based on the LINQ suggestion Lasse-v-Karlsen made, developed this code, which gives you most flexibility:
void Main()
{
// a list of public holidays
var holidays = new List<DateTime>() {new DateTime(2020,1,1),
new DateTime(2020,12,24), new DateTime(2020,12,25), new DateTime(2020,12,26)};
// a function checking if the date is a public holiday
Func<DateTime, bool> isHoliday = (dt) => holidays.Any(a=>a==dt);
// the start date
var dt = new DateTime(2020, 07, 13);
// end date, 5 working days later
var endDate = GetWorkingDay(dt, 5, isHoliday);
// print it
Console.WriteLine(endDate?.ToString("yyyy-mm-dd"));
}
public DateTime? GetWorkingDay(DateTime dt, int skipWorkingDays = 0,
Func<DateTime, bool> holidays=null)
{
if (holidays == null) holidays = (dt) => false;
IEnumerable<DateTime> NextWorkingDay(DateTime dt)
{
while (true)
{
var day = dt.DayOfWeek;
if (day != DayOfWeek.Saturday && day != DayOfWeek.Sunday
&& !holidays.Invoke(dt)) yield return dt;
dt = dt.AddDays(1);
}
}
if (skipWorkingDays<0) return null;
if (skipWorkingDays==0) return NextWorkingDay(dt).First();
var nextXDays = NextWorkingDay(dt).Take(skipWorkingDays).ToList();
var endDate = nextXDays.OrderByDescending(d => d).First();
return endDate;
}
Whether you have a list of public holidays like in this example, or a function coming from a library telling you if a date is a public holiday or not, just feel free to modify the Lambda function isHoliday. In your case, it would be defined as:
Func<DateTime, bool> isHoliday = (dt) => DateSystem.IsPublicHoliday(dt, CountryCode.IT);

Filter or Check Date in A Date Range

It's totally a simple or basic requirement. I am trying to get a date from a list of date using C#. So what I've done, made a function and iterated that with a for loop. I've tried to make the list function into two ranges and passed the value from the DatePicker control as follows:
private void btnClick_Click(object sender, EventArgs e)
{
DateTime theFromDate = dateTimePicker1.Value;
DateTime theToDate = dateTimePicker2.Value;
List<DateRange> lstRange = GetDateRange();
/**Trying To Get The Date From The Range - Starts**/
var dates = new List<DateTime>();
for (var dt = theFromDate; dt <= theToDate; dt = dt.AddDays(1))
{
dates.Add(dt);
//MessageBox.Show(dt.Date.ToString());
}
List<DateRange> lst = GetDateRange();
foreach(var item in lst)
{
if(theFromDate <= item.EndtDate.Date)
{
MessageBox.Show(theFromDate.ToString("dd-MMM-yyyy") + " in the date range!");
}
else
{
MessageBox.Show(theFromDate.ToString("dd-MMM-yyyy") + " not in the date range!");
}
}
/**Trying To Get The Date From The Range - Ends**/
}
public class DateRange
{
public DateTime date { set; get; }
public DateTime EndtDate { set; get; }
}
/**List of Dates Here - Starts**/
public List<DateRange> GetDateRange()
{
List<DateRange> lstDate = new List<DateRange>();
DateRange aDateRange = new DateRange();
aDateRange.StartDate = Convert.ToDateTime("10-Aug-2018");
aDateRange.EndtDate = Convert.ToDateTime("13-Aug-2018");
lstDate.Add(aDateRange);
return lstDate;
}
/**List of Dates Here - Ends**/
Unfortunately this doesn't return the desired output though the list has the specific date.
Update 1:
Expected Output - FromDate and ToDate values are stored in the list.
FromDate ToDate
10-AUG-2018 13-AUG-2018
**in the date range**
FromDate ToDate
13-AUG-2018 16-AUG-2018
**in the date range** //As 13 is the end date in the given list
FromDate ToDate
8-AUG-2018 10-AUG-2018
**in the date range** //As 10 is the start date in the given list
FromDate ToDate
8-AUG-2018 8-AUG-2018
**not in the date range** //As 10 is the start date in the given list
I'm having a bit of trouble figuring out what it is you're trying to do, to be perfectly honest, and I can't help but feel you're "over-engineering" your solution.
First, a "date range" is just two dates - a staring date and an end date, but your GetDateRange method has 4 dates inside it, which it returns as a list. This is incredibly confusing - and I'm not sure if you're trying to get multiple date-ranges (multiple pairs) or a single date-range out of it. Given that all the dates are one after another, I'm going to assume the latter.
public class DateRange
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
public DateRange GetStaticDateRange()
{
//It seems counterproductive to add all 4 dates here,
//given that these are all one after the other
return new DateRange
{
StartDate = new DateTime(2018, 7, 10),
EndDate = new DateTime(2018, 7, 13)
};
//Obviously this can be modified as needed to return whatever combination of
//start-end dates you want, but this method will only ever return ONE range
//However, this method could just as well accept parameters and / or access other resources
}
public bool IsInDateRange(DateTime dateToCheck, DateRange targetRange)
{
//An argument can be made to use non-encompassing comparisons for both checks
//depending on your requirements
return dateToCheck >= targetRange.StartDate && dateToCheck <= targetRange.EndDate;
}
The above has a simple class for storing a "date-range" (aptly called DateRange), and a sample method which checks if a given DateTime is valid inside a specific DateRange.
EDIT:
OK, so from your updated question it seems like you're trying to find if two date-ranges overlap (at all).
In which case, the code below should help.
public static bool DateRangesOverlap(DateRange range1, DateRange range2)
{
return (range1.StartDate >= range2.StartDate && range1.StartDate <= range2.EndDate) ||
(range1.EndDate >= range2.StartDate && range1.EndDate <= range2.EndDate);
}
Here's a working example on .NET Fiddle with your test cases. Note that I'm still using the DateRange class as defined above with a constructor added for brevity.
Also please note that the DateRange class has no sanity-check for the start and end parameters, and it's possible to create a DateRange with the two values reversed (i.e. start > end) which, obviously, would cause errors. But this is just an example so implementations of these things I leave to you. ;)
You have a few bugs in your code.
For example in the GetDateRange(), you are adding only one date to the range, and its date will set to 13-Aug-2018, so that is one thing you need to fix, and if your goal is to find a date in a range of dates, you can use Linq. To compare ranges, I also suggest use DateTime.CompareTo Method. See the code below for corrections of your errors:
public static bool RangeContainsDate(DateTime queriedDateTime)
{
var queriedDateRange = new DateRange { Date = queriedDateTime };
List<DateRange> dates = GetDateRange();
return dates.Where(d => d.CompareTo(queriedDateRange) == 0).Any();
}
/**List of Dates Here - Starts**/
public static List<DateRange> GetDateRange()
{
List<DateRange> lstDate = new List<DateRange>();
DateRange aDateRange1 = new DateRange();
aDateRange1.Date = Convert.ToDateTime("10-Aug-2018");
lstDate.Add(aDateRange1);
DateRange aDateRange2 = new DateRange();
aDateRange2.Date = Convert.ToDateTime("11-Aug-2018");
lstDate.Add(aDateRange2);
DateRange aDateRange3 = new DateRange();
aDateRange3.Date = Convert.ToDateTime("12-Aug-2018");
lstDate.Add(aDateRange3);
DateRange aDateRange4 = new DateRange();
aDateRange4.Date = Convert.ToDateTime("13-Aug-2018");
lstDate.Add(aDateRange4);
return lstDate;
}
}
}
public class DateRange : IComparable<DateRange>
{
public DateTime Date { set; get; }
public int CompareTo(DateRange other)
{
if (ReferenceEquals(other, null))
{
return -1;
}
return DateTime.Compare(Date, other.Date);
}
}
private void btnClick_Click(object sender, EventArgs e)
{
//DateTime theFromDate = dateTimePicker1.Value;
DateTime theToDate = dateTimePicker2.Value;
List<DateRange> lstRange1 = GetDateRange();
List<DateRange> lstRange2 = GetDateRange();
var result = lstRange1.Any(x => x.date >= theToDate && lstRange2.Any(y => y.date < theToDate));
if (result)
{
MessageBox.Show(theToDate.ToString("dd-MMM-yyyy") + " in the date range!");
}
else
{
MessageBox.Show(theToDate.ToString("dd-MMM-yyyy") + " not in the date range!");
}
}
public List<DateRange> GetDateRange()
{
List<DateRange> lstDate = new List<DateRange>();
lstDate.Add(new DateRange { date = Convert.ToDateTime("10-Aug-2018") });
lstDate.Add(new DateRange { date = Convert.ToDateTime("11-Aug-2018") });
lstDate.Add(new DateRange { date = Convert.ToDateTime("12-Aug-2018") });
lstDate.Add(new DateRange { date = Convert.ToDateTime("13-Aug-2018") });
return lstDate;
}

How to get gap in date ranges from a period of time

I have an initial and a final date range = 1/1/2015 - 1/30/2015
I have these date ranges that represent dates of unavailability.
1/5/2015 - 1/10/2015
1/15/2015 - 1/20/2015
1/22/2015 - 1/28/2015
I want this output, mainly the dates of availability from the main range:
A: 1/1/2015 - 1/4/2015
B: 1/11/2015 - 1/14/2015
C: 1/21/2015 - 1/21/2015
D: 1/29/2015 - 1/30/2015
I tried to generate a sequential date range like this in order to get the exception dates with Except() but I think I'm complicating the thing.
//dtStartDate = 1/1/2015
//dtEndDate = 1/30/2015
var days = (int)(dtEndDate - dtStartDate).TotalDays + 1;
var completeSeq = Enumerable.Range(0, days).Select(x => dtStartDate.AddDays(x)).ToArray();
How can I get the gap of date ranges from period of time.
I other words how can I get the A, B, C and D from this picture
http://www.tiikoni.com/tis/view/?id=ebe851c
If these dates overlap, they must not be considered only where is a gap.
----------UPDATE-----------
I think if I do this:
var range = Enumerable.Range(0, (int)(1/10/2015 - 1/5/2015).TotalDays + 1).Select(i => 1/5/2015.AddDays(i));
var missing = completeSeq.Except(range).ToArray();
for each date range I will have the exclusion of each date range given but still cannot get the gap!
I saw your question in my morning today and really liked it, but was busy the whole day. So, got a chance to play with your question and believe me I enjoyed it. Here is my code:-
DateTime startDate = new DateTime(2015, 1, 1);
DateTime endDate = new DateTime(2015, 1, 30);
int totalDays = (int)(endDate - startDate).TotalDays + 1;
availability.Add(new Availability { StartDate = endDate, EndDate = endDate });
var result = from x in Enumerable.Range(0, totalDays)
let d = startDate.AddDays(x)
from a in availability.Select((v, i) => new { Value = v, Index = i })
where (a.Index == availability.Count - 1 ?
d <= a.Value.StartDate : d < a.Value.StartDate)
&& (a.Index != 0 ? d > availability[a.Index - 1].EndDate : true)
group new { d, a } by a.Value.StartDate into g
select new
{
AvailableDates = String.Format("{0} - {1}",g.Min(x => x.d),
g.Max(x => x.d))
};
This, definitely need explanation so here it is:-
Step 1: Create a range of dates from Jan 01 till Jan 30 using Enumerable.Range
Step 2: Since after the second unavailable date range, we need to limit the dates selected from last endate till current object startdate, I have calculated index so that we can get access to the last enddate.
Step 3: Once we get the index, all we need to do is filter the dates except for first date range since we didn't have last object in this case.
Step 4: For the last item since we don't have the max range I am adding the endDate to our unavailable list (hope this makes sense).
Here is the Working Fiddle, if you get confused just remove group by and other filters and debug and see the resulting output it will look fairly easy :)
using System;
using System.Collections.Generic;
using System.Linq;
public static class Program {
public static void Main() {
Tuple<DateTime,DateTime> range=Tuple.Create(new DateTime(2015,1,1),new DateTime(2015,1,30));
Tuple<DateTime,DateTime>[] exclude=new[] {
Tuple.Create(new DateTime(2015,1,5),new DateTime(2015,1,10)),
Tuple.Create(new DateTime(2015,1,15),new DateTime(2015,1,20)),
Tuple.Create(new DateTime(2015,1,22),new DateTime(2015,1,28))
};
foreach(Tuple<DateTime,DateTime> r in ExcludeIntervals(range,exclude)) {
Console.WriteLine("{0} - {1}",r.Item1,r.Item2);
}
}
public static IEnumerable<Tuple<DateTime,DateTime>> ExcludeIntervals(Tuple<DateTime,DateTime> range,IEnumerable<Tuple<DateTime,DateTime>> exclude) {
IEnumerable<Tuple<DateTime,bool>> dates=
new[] { Tuple.Create(range.Item1.AddDays(-1),true),Tuple.Create(range.Item2.AddDays(1),false) }.
Concat(exclude.SelectMany(r => new[] { Tuple.Create(r.Item1,false),Tuple.Create(r.Item2,true) })).
OrderBy(d => d.Item1).ThenBy(d => d.Item2); //Get ordered list of time points where availability can change.
DateTime firstFreeDate=default(DateTime);
int count=1; //Count of unavailability intervals what is currently active. Start from 1 to threat as unavailable before range starts.
foreach(Tuple<DateTime,bool> date in dates) {
if(date.Item2) { //false - start of unavailability interval. true - end of unavailability interval.
if(--count==0) { //Become available.
firstFreeDate=date.Item1.AddDays(1);
}
} else {
if(++count==1) { //Become unavailable.
DateTime lastFreeDate=date.Item1.AddDays(-1);
if(lastFreeDate>=firstFreeDate) { //If next unavailability starts right after previous ended, then no gap.
yield return Tuple.Create(firstFreeDate,lastFreeDate);
}
}
}
}
}
}
ideone.com
Got a little oopy...
public class DateRange
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public bool HasStart
{
get { return Start != DateTime.MinValue; }
}
public bool IsInRange(DateTime date)
{
return (date >= this.Start && date <= this.End);
}
public List<DateRange> GetAvailableDates(DateRange excludedRange)
{
return GetAvailableDates(new List<DateRange>(){excludedRange});
}
public List<DateRange> GetAvailableDates(List<DateRange> excludedRanges)
{
if (excludedRanges == null)
{
return new List<DateRange>() { this };
}
var list = new List<DateRange>();
var aRange = new DateRange();
var date = this.Start;
while (date <= this.End)
{
bool isInARange = excludedRanges.Any(er => er.HasStart && er.IsInRange(date));
if (!isInARange)
{
if (!aRange.HasStart)
{
aRange.Start = date;
}
aRange.End = date;
}
else
{
if (aRange.HasStart)
{
list.Add(aRange);
aRange = new DateRange();
}
}
date = date.AddDays(1);
}
if (aRange.HasStart)
{
list.Add(aRange);
}
return list;
}
}

Generate Service Schedule between two Dates in .net

I am working on an Asp.net project using C#. I am facing a problem in this project. This task based on service scheduled according to condition.
Problem:
Two textboxes for dates, one dropdownlist for type
First I have selected two dates according to type generate scheduled.
For example:
I have selected dates 14/03/2014 to 14/6/2014 and type Monthly
Monthly means increase value by 1 month
So the output should look like this
14/04/2014
14/05/2014
14/06/2014
There are three dates that are scheduled between two date range
Question:
How to achieve this task ?
Sorry for poor English.....
Looping and adding one month at a time should work
public IEnumerable<DateTime> GenerateDates(DateTime start, DateTime end)
{
var dates = new List<DateTime>();
var date = new DateTime(start.Year, start.Month, start.Day);
while (date.Month <= end.Month && date.Year <= end.Year)
{
date = date.AddMonths(1);
dates.Add(date);
}
return dates;
}
var listDates = GetDates(new DateTime(2014, 3, 14), new DateTime(2014, 6, 14), "Day").ToList();
public IEnumerable<DateTime> GetDates(DateTime from, DateTime to,string type)
{
switch (type)
{
case "Month":
{
for (var dt = from.AddMonths(1); dt <= to; dt=dt.AddMonths(1))
{
yield return dt;
}
break;
}
case "Day":
{
for (var dt = from.AddDays(1); dt <= to; dt = dt.AddDays(1))
{
yield return dt;
}
break;
}
}
}
If you need to include the '14/03/2014' in the result, just remove from.AddMonths(1) from the for loop
From your question I assume that there are several modes, not just MONTHLY. Therefore I'd propose to add a registry that contains the possible modes and the function that is used to determine the next date. This registry is easily extensible if you want to offer some more modes. The sample uses a string key, but you can also use a enum.
As it is implemented now, the start date is included in the list of dates whereas the end date isn't. You can change this by tweaking the while loop.
class ScheduleDateProvider
{
private static readonly Dictionary<string, Func<DateTime, DateTime>> modesDict;
static ScheduleDateProvider()
{
// Register modes
modesDict = new Dictionary<string, Func<DateTime, DateTime>>();
modesDict.Add("Quaterly", (dt) => dt.AddMonths(3) );
modesDict.Add("Monthly", (dt) => dt.AddMonths(1) );
modesDict.Add("Weekly", (dt) => dt.AddDays(7) );
modesDict.Add("Daily", (dt) => dt.AddDays(1) );
}
public IEnumerable<DateTime> GetDatesInRange(DateTime startDate, DateTime endDate, string mode)
{
// Assemble dates in a list
var getNextDateFct = modesDict[mode];
var lst = new List<DateTime>();
while(startDate < endDate)
{
lst.Add(startDate);
startDate = getNextDateFct(startDate);
}
return lst.AsReadOnly();
}
}
The following code shows how to use the code:
void Main()
{
DateTime startDate = DateTime.Today;
DateTime endDate = DateTime.Today.AddYears(1);
string selectedMode = "Monthly";
var scheduleDateProv = new ScheduleDateProvider();
var dates = scheduleDateProv.GetDatesInRange(startDate, endDate, selectedMode);
foreach(var dt in dates)
Console.WriteLine(dt.ToShortDateString());
}

In C# is there a function that correlates sequential values on a IEnumerable

I have a IEnumerable. I have a custom Interval class which just has two DateTimes inside it. I want to convert the IEnumerable to IEnumerable where n DateTimes would enumerate to n-1 Intervals.
So if I had 1st Jan, 1st Feb and 1st Mar as the DateTime then I want two intervals out, 1st Jan/1st Feb and 1st Feb/1st March.
Is there an existing C# Linq function that does this. Something like the below Correlate...
IEnumerable<Interval> intervals = dttms.Correlate<DateTime, Interval>((dttm1, dttm2) => new Interval(dttm1, dttm2));
If not I'll just roll my own.
public static IEnumerable<Timespan> Intervals(this IEnumerable<DateTime> source)
{
DateTime last;
bool firstFlag = true;
foreach( DateTime current in source)
{
if (firstFlag)
{
last = current;
firstFlag = false;
continue;
}
yield return current - last;
last = current;
}
}
or
public class Interval {DateTime Start; DateTime End;}
public static IEnumerable<Interval> Intervals(this IEnumerable<DateTime> source)
{
DateTime last;
bool firstFlag = true;
foreach( DateTime current in source)
{
if (firstFlag)
{
last = current;
firstFlag = false;
continue;
}
yield return new Interval {Start = last, End = current};
last = current;
}
}
or very generic:
public static IEnumerable<U> Correlate<T,U>(this IEnumerable<T> source, Func<T,T,U> correlate)
{
T last;
bool firstFlag = true;
foreach(T current in source)
{
if (firstFlag)
{
last = current;
firstFlag = false;
continue;
}
yield return correlate(last, current);
last = current;
}
}
var MyDateTimes = GetDateTimes();
var MyIntervals = MyDateTimes.Correlate((d1, d2) => new Interval {Start = d1, End = d2});
You could also just use Aggregate, Joel's answer would be better if you need in multiple scenarios:
var dates = new List<DateTime>
{
new DateTime(2010, 1, 1),
new DateTime(2010, 2, 1),
new DateTime(2010, 3, 1)
};
var intervals = dates.Aggregate(new List<Interval>(), (ivls, d) =>
{
if (ivls.Count != dates.Count-1)
{
ivls.Add(new Interval(d,dates[ivls.Count + 1]));
}
return ivls;
});
You can write your own extension method that will be able to do what you need.
Here's a slightly maddish solution based on LINQ:
var z = l.Aggregate(new Stack<KeyValuePair<DateTime, TimeSpan>>(),
(s, dt) =>
{
var ts = s.Count > 0 ? dt - s.Peek().Key : TimeSpan.Zero;
s.Push(new KeyValuePair<DateTime, TimeSpan>(dt, ts));
return s;
})
.Where(kv=>!kv.Value.Equals(TimeSpan.Zero))
.Select(kv => kv.Value)
.ToList();
l is an enumerable of DateTimes.
But now that I see that you actually don't have TimeSpans but start and end times, this would look like that:
var z = l.Aggregate(new Stack<Interval>(),
(s, dt) =>
{
s.Push(s.Count > 0 ?
new Interval { Start = s.Peek().End, End = dt } : new Interval { End = dt });
return s;
})
.Where(v=> v.Start != default(DateTime))
.Reverse()
.ToList();

Categories

Resources