Exemple #1
0
 public static DateTimeInterval GetOverlapedPeriod(this DateTimeInterval period1, DateTimeInterval period2)
 {
     if (IsOverlapedPeriods(period1, period2))
     {
         return(new DateTimeInterval(
                    DateTimeHelper.Max(period1.StartInterval, period2.StartInterval),
                    DateTimeHelper.Min(period1.EndInterval, period2.EndInterval)));
     }
     else
     {
         return(null);
     }
 }
Exemple #2
0
        /// <summary>
        /// Determines whether the whole or parts of period1 and period2 are overlaped
        /// </summary>
        /// <param name="period1">The period1.</param>
        /// <param name="period2">The period2.</param>
        /// <returns>
        ///   <c>true</c> if any overlaped periods; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsOverlapedPeriods(this DateTimeInterval period1, DateTimeInterval period2)
        {
            if (period1 == null)
            {
                throw new ArgumentNullException("period1");
            }
            if (period2 == null)
            {
                throw new ArgumentNullException("period2");
            }

            return(period2.StartInterval.InPeriod(period1) || period1.StartInterval.InPeriod(period2));
        }
Exemple #3
0
        //public static bool operator <(DateTimeInterval a, DateTimeInterval b)
        //{
        //    return Comparison(a, b) < 0;
        //}

        //public static bool operator >(DateTimeInterval a, DateTimeInterval b)
        //{
        //    return Comparison(a, b) > 0;
        //}

        //public static int Comparison(DateTimeInterval a, DateTimeInterval b)
        //{

        //    if (a.StartInterval < b.StartInterval)
        //        return -1;

        //    else if (a.StartInterval == b.StartInterval && a.EndInterval == b.EndInterval)
        //        return 0;

        //    else if (a.StartInterval > b.StartInterval)
        //        return 1;
        //    return 0;
        //}

        public int CompareTo(object obj)
        {
            if (obj is DateTimeInterval)
            {
                DateTimeInterval temp = (DateTimeInterval)obj;
                if (temp.StartInterval == this.StartInterval && temp.EndInterval == this.EndInterval)
                {
                    return(0);
                }
                else
                {
                    return(-1);// 1 or -1 => Not important. We would juste like to know if periods are equal
                }
            }

            throw new ArgumentException("object is not a DateTimeInterval");
        }
Exemple #4
0
        public override bool Equals(object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return(false);
            }

            // If parameter cannot be cast to DateTimeInterval return false.
            DateTimeInterval p = obj as DateTimeInterval;

            if ((System.Object)p == null)
            {
                return(false);
            }

            // Return true if the fields match:
            return((StartInterval == p.StartInterval) && (EndInterval == p.EndInterval));
        }
Exemple #5
0
        public List <DateTimeInterval> GetPeriodsByWeeksInMonth()
        {
            List <DateTimeInterval> result = new List <DateTimeInterval>();
            DateTime date = startInterval;

            while (date < endInterval)
            {
                DateTimeInterval dInterval = new DateTimeInterval
                {
                    StartInterval = date,
                    EndInterval   = DateTimeHelper.Min(date.AddDays(7), date.ToEndOfMonth())
                };

                result.Add(dInterval);
                date = date.AddDays(7);
            }

            return(result);
        }
Exemple #6
0
        public List <DateTimeInterval> GetPeriodsByWeek()
        {
            List <DateTimeInterval> result = new List <DateTimeInterval>();
            DateTime date = startInterval;

            while (date < endInterval)
            {
                DateTimeInterval dInterval = new DateTimeInterval
                {
                    StartInterval = date,
                    EndInterval   = date.AddDays(7)
                };

                result.Add(dInterval);
                date = date.AddDays(7);
            }

            return(result);
        }
Exemple #7
0
        public List <DateTimeInterval> GetPeriodsByMonth()
        {
            List <DateTimeInterval> result = new List <DateTimeInterval>();
            DateTime date = startInterval;

            while (date < endInterval)
            {
                date = date.ToEndOfMonth();
                DateTimeInterval dInterval = new DateTimeInterval
                {
                    StartInterval = new DateTime(date.Year, date.Month, 1),
                    EndInterval   = date > endInterval ? endInterval : date
                };
                result.Add(dInterval);
                date = date.AddDays(1);
            }

            return(result);
        }
Exemple #8
0
        public List <DateTimeInterval> GetPeriodsByFullMonth()
        {
            List <DateTimeInterval> result = new List <DateTimeInterval>();
            DateTime date = startInterval;

            while (date < endInterval.ToEndOfMonth())
            {
                date = date.ToEndOfMonth();
                DateTimeInterval dInterval = new DateTimeInterval
                {
                    StartInterval = new DateTime(date.Year, date.Month, 1),
                    EndInterval   = date
                };
                result.Add(dInterval);
                date = date.AddSeconds(2);
            }

            return(result);
        }
Exemple #9
0
 public DateTimeInterval(DateTimeInterval period)
 {
     this.StartInterval = period.StartInterval;
     this.EndInterval   = period.EndInterval;
 }
Exemple #10
0
        public static List <DateTimeInterval> GetOverlapedPeriodByDay(this DateTimeInterval period1, DateTimeInterval period2)
        {
            DateTimeInterval overlapedPeriod = GetOverlapedPeriod(period1, period2);

            if (overlapedPeriod != null)
            {
                return(overlapedPeriod.GetPeriodsByDay());
            }
            return(null);
        }
Exemple #11
0
        /// <summary>
        /// Gets the total periods by month.
        /// </summary>
        /// <param name="period1">The period1.</param>
        /// <param name="period2">The period2.</param>
        /// <param name="ignoreEmptyPeriods">if set to <c>true</c> not consider periods between the smallest end date period and the greatest
        /// start date period.</param>
        /// <returns></returns>
        public static List <DateTimeInterval> GetTotalPeriodsByMonth(this DateTimeInterval period1, DateTimeInterval period2, bool ignoreEmptyPeriods)
        {
            if (IsOverlapedPeriods(period1, period2))
            {
                return(new DateTimeInterval(DateTimeHelper.Min(period1.StartInterval, period2.StartInterval),
                                            DateTimeHelper.Max(period1.EndInterval, period2.EndInterval)).GetPeriodsByFullMonth());
            }
            else
            {
                List <DateTimeInterval> lst = new List <DateTimeInterval>();
                if (ignoreEmptyPeriods)
                {
                    lst.AddRange(new DateTimeInterval(DateTimeHelper.Min(period1.StartInterval, period2.StartInterval),
                                                      DateTimeHelper.Min(period1.EndInterval, period2.EndInterval)).GetPeriodsByFullMonth());
                    lst.AddRange(new DateTimeInterval(DateTimeHelper.Max(period1.StartInterval, period2.StartInterval),
                                                      DateTimeHelper.Max(period1.EndInterval, period2.EndInterval)).GetPeriodsByFullMonth());
                }
                else
                {
                    lst.AddRange(new DateTimeInterval(DateTimeHelper.Min(period1.StartInterval, period2.StartInterval),
                                                      DateTimeHelper.Max(period1.EndInterval, period2.EndInterval)).GetPeriodsByFullMonth());
                }

                return(lst);
            }
        }
Exemple #12
0
 public bool IsIntersects(DateTimeInterval period)
 {
     return(this.StartInterval.IsBetweenDates(period.StartInterval, period.EndInterval) || this.EndInterval.IsBetweenDates(period.StartInterval, period.EndInterval) ||
            period.StartInterval.IsBetweenDates(this.StartInterval, this.EndInterval) || period.EndInterval.IsBetweenDates(this.StartInterval, this.EndInterval));
 }