Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2013
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Windows.Forms.DataVisualization.Charting;
  5. using System.Diagnostics;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Collections.Generic;
  10.  
  11. namespace Test_1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.  
  19.         }
  20.  
  21.  
  22.  
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.             List<Capacities> _list = new List<Capacities>{
  26.                 new Capacities(DateTime.Parse("01/01/2013"), DateTime.Parse("01/01/2013 06:00"), 100),
  27.                 new Capacities(DateTime.Parse("01/01/2013 04:00"), DateTime.Parse("01/02/2013 00:00"), 120),
  28.                 new Capacities(DateTime.Parse("01/04/2013"), DateTime.Parse("01/04/2013 15:00"), 100),
  29.                 new Capacities(DateTime.Parse("01/04/2013 15:00"), DateTime.Parse("01/04/2013 18:00"), 150)
  30.             };
  31.  
  32.             SortedSet<DateTime> splitdates = new SortedSet<DateTime>();
  33.             foreach (var item in _list)
  34.             {
  35.                 splitdates.Add(item.Period.Start);
  36.                 splitdates.Add(item.Period.End);
  37.             }
  38.  
  39.             var list = splitdates.ToList();
  40.             var ranges = new List<DateRange>();
  41.             for (int i = 0; i < list.Count - 1; i++)
  42.                 ranges.Add(new DateRange() { Start = list[i], End = list[i + 1] });
  43.  
  44.             var result = from range in ranges
  45.                          from c in _list
  46.                          where c.Period.Intersect(range) != null
  47.                          group c by range into r
  48.                          select new Capacities(r.Key.Start, r.Key.End, r.Sum(a => a.Capacity));
  49.  
  50.             foreach (var item in result)
  51.                 Debug.WriteLine(item);
  52.  
  53.             /* Writes:
  54.                 01.01.2013 00:00:00 - 01.01.2013 04:00:00    100
  55.                 01.01.2013 04:00:00 - 01.01.2013 06:00:00    220
  56.                 01.01.2013 06:00:00 - 01.02.2013 00:00:00    120
  57.                 01.04.2013 00:00:00 - 01.04.2013 15:00:00    100
  58.                 01.04.2013 15:00:00 - 01.04.2013 18:00:00    150
  59.              */
  60.         }
  61.  
  62.         private bool IsSubrange(DateTime start1, DateTime end1, DateTime start2, DateTime end2)
  63.         {
  64.             return start1 <= start2 && end1 >= end2;
  65.         }
  66.     }
  67.  
  68.     public class DateRange : IEquatable<DateRange>
  69.     {
  70.         public DateTime Start { get; set; }
  71.         public DateTime End { get; set; }
  72.  
  73.         public DateRange Intersect(DateRange d)
  74.         {
  75.             var s = (d.Start > this.Start) ? d.Start : this.Start; // Later Start
  76.             var e = (d.End < this.End) ? d.End : this.End; // Earlier ending
  77.  
  78.             if (s < e)
  79.                 return new DateRange() { Start = s, End = e };
  80.             else
  81.                 return null;
  82.         }
  83.  
  84.         public bool Contains(DateTime d)
  85.         {
  86.             return d >= Start && d <= End;
  87.         }
  88.  
  89.         public bool Equals(DateRange obj)
  90.         {
  91.             return Start.Equals(obj.Start) && End.Equals(obj.End);
  92.         }
  93.     }
  94.  
  95.     public class Capacities
  96.     {
  97.         public DateRange Period { get; set; }
  98.         public int Capacity { get; set; }
  99.  
  100.         public Capacities()
  101.         {
  102.             Period = new DateRange();
  103.         }
  104.  
  105.         public Capacities(DateTime start, DateTime end, int c)
  106.             : this()
  107.         {
  108.             Period.Start = start;
  109.             Period.End = end;
  110.             Capacity = c;
  111.         }
  112.  
  113.         public override string ToString()
  114.         {
  115.             return string.Format("{0} - {1} \t {2}", Period.Start, Period.End, Capacity);
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement