/// <summary>
 /// Gets the time period representing the intersection of the two periods.
 /// If the periods does not intersect then null is returned
 /// </summary>
 /// <param name="period">The period.</param>
 /// <returns></returns>
 /// <remarks>
 /// Created by: micke
 /// Created date: 4.12.2007
 /// </remarks>
 public TimePeriod? Intersection(TimePeriod period)
 {
     if (!Intersect(period))
         return null;
     TimeSpan start = StartTime;
     if (period.StartTime > start)
         start = period.StartTime;
     TimeSpan end = EndTime;
     if (period.EndTime < end)
         end = period.EndTime;
     return new TimePeriod(start,end);
 }
        public TimePeriod FitHours(int hoursToAddAtBeginningAndEnd)
        {
            TimePeriod tempPeriod = FitHours();
            if(hoursToAddAtBeginningAndEnd > 0)
            {
                TimeSpan adder = new TimeSpan(1, 0, 0);
                if(tempPeriod == this)
                {
                    tempPeriod = new TimePeriod(StartTime.Subtract(adder), EndTime.Add(adder));
                }
                else
                {
                    if (tempPeriod.StartTime == StartTime)
                        tempPeriod = new TimePeriod(StartTime.Subtract(adder), tempPeriod.EndTime);

                    if (tempPeriod.EndTime == EndTime)
                        tempPeriod = new TimePeriod(tempPeriod.StartTime, EndTime.Add(adder));
                }
                if (hoursToAddAtBeginningAndEnd > 1)
                {
                    adder = new TimeSpan(hoursToAddAtBeginningAndEnd - 1, 0, 0);
                    tempPeriod = new TimePeriod(tempPeriod.StartTime.Subtract(adder), tempPeriod.EndTime.Add(adder));
                }
            }
            return tempPeriod;
        }
 /// <summary>
 /// Does the two periods intersect.
 /// </summary>
 /// <param name="period">The period.</param>
 /// <returns></returns>
 /// <remarks>
 /// Created by: micke
 /// Created date: 4.12.2007
 /// </remarks>
 public bool Intersect(TimePeriod period)
 {
     if (Contains(period.StartTime)||Contains(period.EndTime.Subtract(new TimeSpan(1))))
         return true;
     if (period.Contains(this))
         return true;
     //if (Contains(period)) this is the same as the row before
     //    return true;
     return false;
 }
 /// <summary>
 /// Indicates whether the current object is equal to another object of the same type.
 /// </summary>
 /// <param name="other">An object to compare with this object.</param>
 /// <returns>
 /// true if the current object is equal to the other parameter; otherwise, false.
 /// </returns>
 public bool Equals(TimePeriod other)
 {
     return _period == other._period;
 }
        /// <summary>
        /// Determines whether the specified period contains part.
        /// </summary>
        /// <param name="period">The period.</param>
        /// <returns>
        /// 	<c>true</c> if the specified period contains part; otherwise, <c>false</c>.
        /// </returns>
        /// <remarks>
        /// Created by: peterwe
        /// Created date: 2007-11-13
        /// </remarks>
        public bool ContainsPart(TimePeriod period)
        {
            if (ContainsPart(period.StartTime) || ContainsPart(period.EndTime))
                return true;

            if (period.Contains(this))
                return true;

            return false;
        }
 /// <summary>
 /// Determines whether [contains] [the specified period].
 /// </summary>
 /// <param name="period">The period.</param>
 /// <returns>
 /// 	<c>true</c> if [contains] [the specified period]; otherwise, <c>false</c>.
 /// </returns>
 /// <remarks>
 /// Created by: peterwe
 /// Created date: 2007-11-13
 /// </remarks>
 public bool Contains(TimePeriod period)
 {
     return (ContainsPart(period.StartTime) && ContainsPart(period.EndTime));
 }
        /// <summary>
        /// Returns an list of ordered by StartTime
        /// </summary>
        /// <param name="periods">The periods.</param>
        /// <returns></returns>
        /// <remarks>
        /// Created by: henrika
        /// Created date: 2008-04-23
        /// </remarks>
        public static IList<TimePeriod> Combine(IList<TimePeriod> periods)
        {
            List<TimePeriod> retList = new List<TimePeriod>();
            List<TimePeriod> orderedList = (List<TimePeriod>)periods;
            orderedList.Sort();
            foreach (TimePeriod period in orderedList)
            {
                if (retList.Count != 0)
                {
                    if (retList[retList.Count - 1].Intersect(period))
                    {

                        if (retList[retList.Count - 1].EndTime < period.EndTime)
                        {
                            TimePeriod newTimePeriod = new TimePeriod(retList[retList.Count - 1].StartTime, period.EndTime);
                            retList.Remove(retList[retList.Count - 1]);
                            retList.Add(newTimePeriod);
                        }
                    }
                    else retList.Add(period);
                }
                else retList.Add(period);
            }

            return retList;
        }
 /// <summary>
 /// Aligns to minutes.
 /// </summary>
 /// <param name="timePeriod">The time period.</param>
 /// <param name="minutes">The minutes.</param>
 /// <returns></returns>
 /// <remarks>
 /// Created by: HenryG
 /// Created date: 2009-01-21
 /// </remarks>
 public static TimePeriod AlignToMinutes(TimePeriod timePeriod, int minutes)
 {
     long minutesTicks = new TimeSpan(0, minutes, 0).Ticks;
     TimeSpan startTimeSpan = TimeSpan.FromTicks(divideAndMultiply(timePeriod.StartTime.Ticks, minutesTicks));
     TimeSpan endTimeSpan;
     if (timePeriod.EndTime.Minutes == 0)
     {
         endTimeSpan = timePeriod.EndTime;
     }
     else
     {
         endTimeSpan = TimeSpan.FromTicks(divideAndMultiply(timePeriod.EndTime.Ticks, minutesTicks) + minutesTicks);
     }
     TimePeriod newTimePeriod = new TimePeriod(startTimeSpan, endTimeSpan);
     return newTimePeriod;
 }