Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Windows.Forms.DataVisualization.Charting;
- using System.Diagnostics;
- using System.Text;
- using System.Linq;
- using System.Net;
- using System.Collections.Generic;
- namespace Test_1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- List<Capacities> _list = new List<Capacities>{
- new Capacities(DateTime.Parse("01/01/2013"), DateTime.Parse("01/01/2013 06:00"), 100),
- new Capacities(DateTime.Parse("01/01/2013 04:00"), DateTime.Parse("01/02/2013 00:00"), 120),
- new Capacities(DateTime.Parse("01/04/2013"), DateTime.Parse("01/04/2013 15:00"), 100),
- new Capacities(DateTime.Parse("01/04/2013 15:00"), DateTime.Parse("01/04/2013 18:00"), 150)
- };
- SortedSet<DateTime> splitdates = new SortedSet<DateTime>();
- foreach (var item in _list)
- {
- splitdates.Add(item.Period.Start);
- splitdates.Add(item.Period.End);
- }
- var list = splitdates.ToList();
- var ranges = new List<DateRange>();
- for (int i = 0; i < list.Count - 1; i++)
- ranges.Add(new DateRange() { Start = list[i], End = list[i + 1] });
- var result = from range in ranges
- from c in _list
- where c.Period.Intersect(range) != null
- group c by range into r
- select new Capacities(r.Key.Start, r.Key.End, r.Sum(a => a.Capacity));
- foreach (var item in result)
- Debug.WriteLine(item);
- /* Writes:
- 01.01.2013 00:00:00 - 01.01.2013 04:00:00 100
- 01.01.2013 04:00:00 - 01.01.2013 06:00:00 220
- 01.01.2013 06:00:00 - 01.02.2013 00:00:00 120
- 01.04.2013 00:00:00 - 01.04.2013 15:00:00 100
- 01.04.2013 15:00:00 - 01.04.2013 18:00:00 150
- */
- }
- private bool IsSubrange(DateTime start1, DateTime end1, DateTime start2, DateTime end2)
- {
- return start1 <= start2 && end1 >= end2;
- }
- }
- public class DateRange : IEquatable<DateRange>
- {
- public DateTime Start { get; set; }
- public DateTime End { get; set; }
- public DateRange Intersect(DateRange d)
- {
- var s = (d.Start > this.Start) ? d.Start : this.Start; // Later Start
- var e = (d.End < this.End) ? d.End : this.End; // Earlier ending
- if (s < e)
- return new DateRange() { Start = s, End = e };
- else
- return null;
- }
- public bool Contains(DateTime d)
- {
- return d >= Start && d <= End;
- }
- public bool Equals(DateRange obj)
- {
- return Start.Equals(obj.Start) && End.Equals(obj.End);
- }
- }
- public class Capacities
- {
- public DateRange Period { get; set; }
- public int Capacity { get; set; }
- public Capacities()
- {
- Period = new DateRange();
- }
- public Capacities(DateTime start, DateTime end, int c)
- : this()
- {
- Period.Start = start;
- Period.End = end;
- Capacity = c;
- }
- public override string ToString()
- {
- return string.Format("{0} - {1} \t {2}", Period.Start, Period.End, Capacity);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement