title: Find time occurence and duration in certain time range
body: <p>I made a little parking application in C#. There are some different pricings depending on vehicle type and time zone. Day can be divided into time zones (for example morning, day, evening and night). Now if customer stops parking I want to calculate in which time zones customer has parked and how long.</p>
  
  <p>For example morning time zone starts at 6:00 and ends 12:00, day time zone starts at 12:00 and ends 16:00, evening time zone starts at 16:00 and ends at 23:00 and night time zone starts at 23:00 and ends at 6:00. Customer started parking his car at 00:30 and ends parking at 6:32. Currently I have 4 variables for that: parking start time, parking end time and timezone starting time and timezone ending time.</p>
  
  <p>Second example would be like customer parks 24H, then the parking time has all time zones covered.</p>
  
  <p>How is the simplest way to calculate how many hours and minutes customer parked his car in different time zones?</p>
  
  <p>Regards,
  evilone</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Got this answer from MSDN and post it here so others can learn from it too.</p>
  
  <pre><code>public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
              DateTime start = new DateTime(2011, 7, 25, 0, 30, 0);
              DateTime end = new DateTime(2011, 7, 26, 6, 32, 0);
              List&lt;DateTime&gt; listTimeZones = CalculateTotalTime(start, end);
  
              StringBuilder sb = new StringBuilder();
              for (int i = 0; i &lt; listTimeZones.Count; i++)
              {
                  sb.AppendLine(String.Format("{0}. {1}: from {2} to {3}",
                                              i + 1,
                                              GetZoneInWords(listTimeZones[i].Hour),
                                              String.Format("{0:dd.MM.yyyy hh:mm}", listTimeZones[i]),
                                              (i + 1) &lt; listTimeZones.Count
                                                  ? String.Format("{0:dd.MM.yyyy hh:mm}", listTimeZones[i + 1])
                                                  : "Parking ended"));
              }
              MessageBox.Show(sb.ToString());
          }
  
          private List&lt;DateTime&gt; CalculateTotalTime(DateTime start, DateTime end)
          {
              DateTime temp = start;
  
              int hour = start.Hour;
              int minute = start.Minute;
  
              int morning = 6;
              int day = 12;
              int evening = 17;
              int night = 23;
  
              List&lt;DateTime&gt; timeZones = new List&lt;DateTime&gt;();
  
              do
              {
                  temp = temp.AddHours(1);
                  if (temp.Hour == morning || temp.Hour == day ||
                      temp.Hour == evening || temp.Hour == night)
                  {
                      timeZones.Add(temp);
                  }
              } while (temp &lt; end);
  
              return timeZones;
          }
  
          private string GetZoneInWords(int time)
          {
              string timeOfDay = "";
              if (time.Equals(6))
                  timeOfDay = "Morning";
              else if (time.Equals(12))
                  timeOfDay = "Day";
              else if (time.Equals(17))
                  timeOfDay = "Evening";
              else if (time.Equals(23))
                  timeOfDay = "Night";
  
              return timeOfDay + " parking";
          }
      }
  </code></pre>