<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1570</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1570" rel="self"/>
  <title>MRuston</title>
  <updated>Tue Jul 07 18:46:31 -0700 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor191892</id>
    <published>2009-07-07T18:46:31-07:00</published>
    <title>[C#] On Split DateTime Range Around Midnights</title>
    <content type="html">&lt;p&gt;Two awesome solutions. Thank you Ants.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>MRuston</name>
      <email>artanis0@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/942-split-datetime-range-around-midnights/refactors/191892" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code942</id>
    <published>2009-07-01T14:42:21-07:00</published>
    <updated>2009-07-07T18:46:33-07:00</updated>
    <title>[C#] Split DateTime Range Around Midnights</title>
    <content type="html">&lt;p&gt;I have this little class called MidnightPartitioner which I use to split schedules (represented by a DateTime starting and ending) around midnight. I'm pretty sure my logic is correct in my code, but I have the feeling there is a cleaner and more concise way to split my schedule start and end times into 'blocks' around midnight. Any ideas on how I can clean up this code would be appreciated.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;If scheduleStart and scheduleEnd spans 3 days, then the amount of time spent in each day would make give us 3 SchedulePartition objects. Thus, the TimeSpan length of all schedule partition objects should equal the TimeSpan of scheduleEnd.Subtract(scheduleStart)&lt;/p&gt;

&lt;pre&gt;public static class MidnightPartitioner
    {
        public struct SchedulePartition
        {
            public DateTime Start { get; set; }
            public DateTime End { get; set; }
        }
        
        /// &amp;lt;summary&amp;gt;
        /// Splits a starting and ending DateTime range into
        /// SchedulePartition objects. Each SchedulePartition
        /// represents a span of time spent in a specific date.
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name=&amp;quot;scheduleStart&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name=&amp;quot;scheduleEnd&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        public static IEnumerable&amp;lt;SchedulePartition&amp;gt; Break(DateTime scheduleStart, DateTime scheduleEnd)
        {
            List&amp;lt;SchedulePartition&amp;gt; dayBlocks = new List&amp;lt;SchedulePartition&amp;gt;();
            if (scheduleStart &amp;lt;= scheduleEnd)
            {
                DateTime tomorrow = scheduleStart.Date.AddDays(1);      // Determine when tomorrow is.
                if (tomorrow &amp;lt; scheduleEnd) /* Schedule ends after tomorrow */
                {
                    // Calculates the time spent spent in the scheduleStart date.
                    TimeSpan timeSpentInScheduleStartDate = tomorrow.Subtract(scheduleStart);
                    // Calculates the time spent in the day after the schedule start date
                    TimeSpan timeSpentInDayAfterScheudleStart = scheduleEnd.Subtract(tomorrow);
                    dayBlocks.Add(
                        new SchedulePartition { Start = scheduleStart, End = scheduleStart.Add(timeSpentInScheduleStartDate) }
                        );
                    if (timeSpentInDayAfterScheudleStart.TotalDays &amp;gt; 1)
                    {
                        dayBlocks.AddRange(
                            Break(tomorrow, scheduleEnd)
                            );
                    }
                    else
                    {
                        dayBlocks.Add(
                            new SchedulePartition { Start = tomorrow, End = tomorrow.Add(timeSpentInDayAfterScheudleStart) }
                            );
                    }
                }
                else
                {
                    /* Schedule ends before tomorrow. Cannot be partitioned around midnight.
                     * Return the uncut schedule as a SchedulePartition */
                    return new[]
                    {
                        new SchedulePartition
                        {
                            Start = scheduleStart,
                            End = scheduleEnd
                        }
                    };
                }
            }
            else
                throw new ArgumentException(&amp;quot;Chronological error with scheduleStart and scheduleEnd arguments&amp;quot;);
            return dayBlocks;
        }
    }&lt;/pre&gt;</content>
    <author>
      <name>MRuston</name>
      <email>artanis0@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/942-split-datetime-range-around-midnights" rel="alternate"/>
  </entry>
</feed>

