Exemple #1
0
        private static IEnumerable <string> ResolveDateAgainstConstraint(TimexProperty timex, DateRange constraint)
        {
            if (timex.Month != null && timex.DayOfMonth != null)
            {
                var result = new List <string>();
                for (var year = constraint.Start.Year; year <= constraint.End.Year; year++)
                {
                    var t = timex.Clone();
                    t.Year = year;
                    result.AddRange(ResolveDefiniteAgainstConstraint(t, constraint));
                }

                return(result);
            }

            if (timex.DayOfWeek != null)
            {
                // convert between ISO day of week and .NET day of week
                var day    = timex.DayOfWeek == 7 ? DayOfWeek.Sunday : (DayOfWeek)timex.DayOfWeek;
                var dates  = TimexDateHelpers.DatesMatchingDay(day, constraint.Start, constraint.End);
                var result = new List <string>();

                foreach (var d in dates)
                {
                    var t = timex.Clone();
                    t.DayOfWeek  = null;
                    t.Year       = d.Year;
                    t.Month      = d.Month;
                    t.DayOfMonth = d.Day;
                    result.Add(t.TimexValue);
                }

                return(result);
            }

            if (timex.Hour != null)
            {
                var      result = new List <string>();
                DateTime day    = constraint.Start;
                while (day <= constraint.End)
                {
                    var t = timex.Clone();
                    t.Year       = day.Year;
                    t.Month      = day.Month;
                    t.DayOfMonth = day.Day;
                    result.AddRange(ResolveDefiniteAgainstConstraint(t, constraint));
                    day = day.AddDays(1);
                }

                return(result);
            }

            return(Enumerable.Empty <string>());
        }
        public static TimexProperty TimexTimeAdd(TimexProperty start, TimexProperty duration)
        {
            if (duration.Hours != null)
            {
                var result = start.Clone();
                result.Hour += (int)duration.Hours.Value;
                if (result.Hour.Value > 23)
                {
                    var days = Math.Floor(result.Hour.Value / 24m);
                    var hour = result.Hour.Value % 24;
                    result.Hour = hour;

                    if (result.Year != null && result.Month != null && result.DayOfMonth != null)
                    {
                        var d = new DateObject(result.Year.Value, result.Month.Value, result.DayOfMonth.Value, 0, 0, 0);
                        d = d.AddDays((double)days);

                        result.Year       = d.Year;
                        result.Month      = d.Month;
                        result.DayOfMonth = d.Day;

                        return(result);
                    }

                    if (result.DayOfWeek != null)
                    {
                        result.DayOfWeek += (int)days;
                        return(result);
                    }
                }

                return(result);
            }

            if (duration.Minutes != null)
            {
                var result = start.Clone();
                result.Minute += (int)duration.Minutes.Value;

                if (result.Minute.Value > 59)
                {
                    result.Hour++;
                    result.Minute = 0;
                }

                return(result);
            }

            return(start);
        }
        private static IEnumerable <string> ResolveTimeRange(TimexProperty timex, IEnumerable <TimeRange> constraints)
        {
            var candidate = TimexHelpers.TimeRangeFromTimex(timex);

            var result = new List <string>();

            foreach (var constraint in constraints)
            {
                if (TimexConstraintsHelper.IsOverlapping(candidate, constraint))
                {
                    var start = Math.Max(candidate.Start.GetTime(), constraint.Start.GetTime());
                    var time  = new Time(start);

                    // TODO: consider a method on TimexProperty to do this clone/overwrite pattern
                    var resolved = timex.Clone();
                    resolved.PartOfDay = null;
                    resolved.Seconds   = null;
                    resolved.Minutes   = null;
                    resolved.Hours     = null;
                    resolved.Second    = time.Second;
                    resolved.Minute    = time.Minute;
                    resolved.Hour      = time.Hour;

                    result.Add(resolved.TimexValue);
                }
            }

            return(result);
        }
        private static TimexProperty CloneDateTime(TimexProperty timex)
        {
            var result = timex.Clone();

            result.Years   = null;
            result.Months  = null;
            result.Weeks   = null;
            result.Days    = null;
            result.Hours   = null;
            result.Minutes = null;
            result.Seconds = null;
            return(result);
        }
        private static TimexProperty CloneDuration(TimexProperty timex)
        {
            var result = timex.Clone();

            result.Year        = null;
            result.Month       = null;
            result.DayOfMonth  = null;
            result.DayOfWeek   = null;
            result.WeekOfYear  = null;
            result.WeekOfMonth = null;
            result.Season      = null;
            result.Hour        = null;
            result.Minute      = null;
            result.Second      = null;
            result.Weekend     = null;
            result.PartOfDay   = null;
            return(result);
        }
        public static TimexProperty TimexDateAdd(TimexProperty start, TimexProperty duration)
        {
            if (start.DayOfWeek != null)
            {
                var end = start.Clone();
                if (duration.Days != null)
                {
                    end.DayOfWeek += (int)duration.Days;
                }

                return(end);
            }

            if (start.Month != null && start.DayOfMonth != null)
            {
                if (duration.Days != null)
                {
                    if (start.Year != null)
                    {
                        var d = new DateObject(start.Year.Value, start.Month.Value, start.DayOfMonth.Value, 0, 0, 0);
                        d = d.AddDays((double)duration.Days.Value);

                        return(new TimexProperty {
                            Year = d.Year,
                            Month = d.Month,
                            DayOfMonth = d.Day
                        });
                    }
                    else
                    {
                        var d = new DateObject(2001, start.Month.Value, start.DayOfMonth.Value, 0, 0, 0);
                        d = d.AddDays((double)duration.Days.Value);

                        return(new TimexProperty
                        {
                            Month = d.Month,
                            DayOfMonth = d.Day
                        });
                    }
                }

                if (duration.Years != null)
                {
                    if (start.Year != null)
                    {
                        return(new TimexProperty
                        {
                            Year = (int)(start.Year.Value + duration.Years.Value),
                            Month = start.Month,
                            DayOfMonth = start.DayOfMonth
                        });
                    }
                }

                if (duration.Months != null)
                {
                    if (start.Month != null)
                    {
                        return(new TimexProperty
                        {
                            Year = start.Year,
                            Month = (int)(start.Month + duration.Months),
                            DayOfMonth = start.DayOfMonth
                        });
                    }
                }
            }
            return(start);
        }