Exemple #1
0
 public static bool CanCollapse(AbstractPeriod basePeriod, AbstractPeriod coverPeriod)
 {
     if ((basePeriod.Fixed & coverPeriod.Fixed) != 0)
     {
         return(false);
     }
     return(basePeriod.SpanDirection != -coverPeriod.SpanDirection || basePeriod.SpanDirection == 0);
 }
Exemple #2
0
        public static bool CollapseTwo(AbstractPeriod basePeriod, AbstractPeriod coverPeriod, bool isLinked)
        {
            if (!CanCollapse(basePeriod, coverPeriod))
            {
                return(false);
            }

            // if span
            if (basePeriod.SpanDirection != 0)
            {
                if (coverPeriod.SpanDirection != 0)
                {
                    // if another date is Span, just add spans together
                    basePeriod.Span += coverPeriod.Span;
                }
            }

            // take year if it is not here, but is in other date
            if (!basePeriod.IsFixed(FixPeriod.Year) && coverPeriod.IsFixed(FixPeriod.Year))
            {
                basePeriod.Date = new DateTime(coverPeriod.Date.Year, basePeriod.Date.Month, basePeriod.Date.Day);
                basePeriod.Fix(FixPeriod.Year);
            }

            // take month if it is not here, but is in other date
            if (!basePeriod.IsFixed(FixPeriod.Month) && coverPeriod.IsFixed(FixPeriod.Month))
            {
                basePeriod.Date = new DateTime(basePeriod.Date.Year, coverPeriod.Date.Month, basePeriod.Date.Day);
                basePeriod.Fix(FixPeriod.Month);
            }

            // week and day
            if (!basePeriod.IsFixed(FixPeriod.Week) && coverPeriod.IsFixed(FixPeriod.Week))
            {
                // the week is in another date, check where is a day
                if (basePeriod.IsFixed(FixPeriod.Day))
                {
                    // set day of week, take date
                    basePeriod.Date = TakeDayOfWeekFrom(coverPeriod.Date, basePeriod.Date);
                    basePeriod.Fix(FixPeriod.Week);
                }
                else if (!coverPeriod.IsFixed(FixPeriod.Day))
                {
                    // only week here, take it by taking a day
                    basePeriod.Date = new DateTime(basePeriod.Date.Year, basePeriod.Date.Month, coverPeriod.Date.Day);
                    basePeriod.Fix(FixPeriod.Week);
                }
            }
            else if (basePeriod.IsFixed(FixPeriod.Week) && coverPeriod.IsFixed(FixPeriod.Day))
            {
                // here is a week, but day of week in other date
                basePeriod.Date = TakeDayOfWeekFrom(basePeriod.Date, coverPeriod.Date);
                basePeriod.Fix(FixPeriod.Week, FixPeriod.Day);
            }

            // day
            if (!basePeriod.IsFixed(FixPeriod.Day) && coverPeriod.IsFixed(FixPeriod.Day))
            {
                if (coverPeriod.FixDayOfWeek)
                {
                    // take only day of week from cover
                    basePeriod.Date = TakeDayOfWeekFrom(
                        new DateTime(
                            basePeriod.Date.Year, basePeriod.Date.Month,
                            basePeriod.IsFixed(FixPeriod.Week) ? basePeriod.Date.Day : 1
                            ),
                        coverPeriod.Date,
                        !basePeriod.IsFixed(FixPeriod.Week)
                        );
                    basePeriod.Fix(FixPeriod.Week, FixPeriod.Day);
                }
                else
                {
                    // take day from cover
                    basePeriod.Date = new DateTime(basePeriod.Date.Year, basePeriod.Date.Month, coverPeriod.Date.Day);
                    basePeriod.Fix(FixPeriod.Week, FixPeriod.Day);
                }
            }

            // time
            var timeGot = false;

            if (!basePeriod.IsFixed(FixPeriod.Time) && coverPeriod.IsFixed(FixPeriod.Time))
            {
                basePeriod.Fix(FixPeriod.Time);
                if (!basePeriod.IsFixed(FixPeriod.TimeUncertain))
                {
                    basePeriod.Time = coverPeriod.Time;
                }
                else
                {
                    if (basePeriod.Time.Hours <= 12 && coverPeriod.Time.Hours > 12)
                    {
                        // first period is uncertain and second is certain
                        if (!isLinked)
                        {
                            // add 12 hours if these to periods are NOT part of 'from-two' chain
                            basePeriod.Time += new TimeSpan(12, 0, 0);
                        }
                    }
                }

                timeGot = true;
            }

            if (!basePeriod.IsFixed(FixPeriod.TimeUncertain) && coverPeriod.IsFixed(FixPeriod.TimeUncertain))
            {
                basePeriod.Fix(FixPeriod.TimeUncertain);
                if (basePeriod.IsFixed(FixPeriod.Time))
                {
                    // take time from cover, but day part from base
                    var offset = coverPeriod.Time.Hours <= 12 && basePeriod.Time.Hours > 12 ? 12 : 0;
                    basePeriod.Time = new TimeSpan(coverPeriod.Time.Hours + offset, coverPeriod.Time.Minutes, 0);
                }
                else
                {
                    basePeriod.Time = coverPeriod.Time;
                    timeGot         = true;
                }
            }

            // if this date is Span and we just got time from another non-span date, add this time to Span
            if (timeGot && basePeriod.SpanDirection != 0 && coverPeriod.SpanDirection == 0)
            {
                basePeriod.Span += basePeriod.SpanDirection == 1 ? basePeriod.Time : -basePeriod.Time;
            }

            // set tokens edges
            basePeriod.Start = Math.Min(basePeriod.Start, coverPeriod.Start);
            basePeriod.End   = Math.Max(basePeriod.End, coverPeriod.End);

            return(true);
        }