Exemple #1
0
        /// <summary>
        /// Adds the given calendar day and returns one or more new calendar entities representing the new data.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable <Calendar> Add(this Calendar calendar, DateTime day)
        {
            if (calendar.CoversDate(day))
            { // no work needs to be done.
                return(new Calendar[] { calendar });
            }

            // naively add another calendar entity representing one day.
            if ((calendar.EndDate - calendar.StartDate).Days <= 7 &&
                calendar.StartDate <= day &&
                calendar.EndDate >= day)
            {
                var newCalendar = new Calendar()
                {
                    EndDate   = calendar.EndDate,
                    StartDate = calendar.StartDate,
                    Mask      = calendar.Mask,
                    ServiceId = calendar.ServiceId
                };
                newCalendar.Set(day, true);
                return(new Calendar[] { newCalendar });
            }
            return(new Calendar[] { calendar, day.CreateCalendar(calendar.ServiceId) });
        }
Exemple #2
0
        /// <summary>
        /// Subtracts the given calendar day and returns one or more new calendar entities representing the same data.
        /// </summary>
        public static IEnumerable <Calendar> Subtract(this Calendar calendar, DateTime day)
        {
            if (!calendar.CoversDate(day))
            { // no work needs to be done.
                return(new Calendar[] { calendar });
            }

            var firstDayOfWeek = day.FirstDayOfWeek();
            var lastDayOfWeek  = day.LastDayOfWeek();

            if (firstDayOfWeek <= calendar.StartDate &&
                lastDayOfWeek >= calendar.EndDate)
            { // yay! this is exceptional but let's take advantage of this.
                calendar[day.DayOfWeek] = false;
                return(new Calendar[] { calendar });
            }

            // possibly split in two or three pieces.
            if (firstDayOfWeek <= calendar.StartDate)
            { // two pieces, a first week with the day substracted and the rest.
                var subtracted = new Calendar()
                {
                    StartDate = calendar.StartDate,
                    EndDate   = lastDayOfWeek
                };
                subtracted.CopyWeekPatternFrom(calendar);
                subtracted[day.DayOfWeek] = false;

                var rest = new Calendar()
                {
                    StartDate = lastDayOfWeek.AddDays(1),
                    EndDate   = calendar.EndDate
                };
                rest.CopyWeekPatternFrom(calendar);
                return(new Calendar[] { subtracted, rest });
            }
            else if (lastDayOfWeek >= calendar.EndDate)
            { // two pieces, a last week with the day substracted and the rest.
                var rest = new Calendar()
                {
                    StartDate = calendar.StartDate,
                    EndDate   = firstDayOfWeek.AddDays(-1)
                };
                rest.CopyWeekPatternFrom(calendar);

                var subtracted = new Calendar()
                {
                    StartDate = firstDayOfWeek,
                    EndDate   = calendar.EndDate
                };
                subtracted.CopyWeekPatternFrom(calendar);
                subtracted[day.DayOfWeek] = false;
                return(new Calendar[] { rest, subtracted });
            }
            else
            { // three pieces, a first period, a week with the day subtracted and a last period.
                var rest1 = new Calendar()
                {
                    StartDate = calendar.StartDate,
                    EndDate   = firstDayOfWeek.AddDays(-1)
                };
                rest1.CopyWeekPatternFrom(calendar);

                var subtracted = new Calendar()
                {
                    StartDate = firstDayOfWeek,
                    EndDate   = lastDayOfWeek
                };
                subtracted.CopyWeekPatternFrom(calendar);
                subtracted[day.DayOfWeek] = false;

                var rest2 = new Calendar()
                {
                    StartDate = lastDayOfWeek.AddDays(1),
                    EndDate   = calendar.EndDate
                };
                rest2.CopyWeekPatternFrom(calendar);
                return(new Calendar[] { rest1, subtracted, rest2 });
            }
        }