private static IDateRange GetDateRangeOfKind(DateRangeKind rangeKind, DayOfWeek firstDayOfWeek)
        {
            var firstDayOfWeekProvider = new FirstDayOfWeekProvider()
            {
                Value = firstDayOfWeek
            };
            var list = BuildDateRangeList(firstDayOfWeekProvider);

            list.CurrentKindOfRange = rangeKind;
            return(list.Current);
        }
        public static DateRangeValue GetDateRangeDates(DateRangeKind rangeKind, DateTime from, DateTime to, DayOfWeek firstDayOfWeek)
        {
            if (DateRangeKind.Custom == rangeKind)
            {
                return(new DateRangeValue(from, to));
            }

            var range = GetDateRangeOfKind(rangeKind, firstDayOfWeek);

            return(new DateRangeValue(range.From, range.To));
        }
 /// <summary>
 /// Returns DateRange with date values calculated for the given input rangeKind parameter.
 /// </summary>
 internal static IDateRange GetDateRangeOfKind(DateRangeKind rangeKind)
 {
     return(GetDateRangeOfKind(rangeKind, CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek));
 }
 /// <summary>
 /// Returns recalculated date range as DateRangeValue for the given input parameters.
 /// </summary>
 /// <param name="rangeKind">The kind of range to calculate.</param>
 /// <param name="from">Start DateTime of the range. Has sense only if the rangeKind parameter is set to Custom.</param>
 /// <param name="to">End DateTime of the range. Has sense only if the rangeKind parameter is set to Custom.</param>
 public static DateRangeValue GetDateRangeDates(DateRangeKind rangeKind, DateTime from, DateTime to)
 {
     return(GetDateRangeDates(rangeKind, from, to, CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek));
 }
Beispiel #5
0
        public void GetSpanDatesTest(DateRangeKind kind, DateRangeValue expected)
        {
            var result = DateRangeHelper.GetDateRangeDates(kind, _fromIn, _toIn);

            Assert.AreEqual(expected, result);
        }
 static DateRangePicker()
 {
     DefaultStyleKeyProperty.OverrideMetadata(typeof(DateRangePicker), new FrameworkPropertyMetadata(typeof(DateRangePicker)));
     DefaultKindOfRange = (DateRangeKind)Enum.ToObject(typeof(DateRangeKind), 0);
 }
Beispiel #7
0
        public static DateRange Create(DateRangeKind dateRangeKind)
        {
            DateTime now = DateTime.UtcNow;

            DateRange dateRange;
            switch (dateRangeKind)
            {
                case DateRangeKind.Today:
                    dateRange = new DateRange(now, now.Date.AddDays(1));
                    break;
                case DateRangeKind.Last7Days:
                    dateRange = Create(DateRangePrecision.Days, -7);
                    break;
                case DateRangeKind.Last30Days:
                    dateRange = Create(DateRangePrecision.Days, -30);
                    break;
                case DateRangeKind.Last90Days:
                    dateRange = Create(DateRangePrecision.Days, -90);
                    break;
                case DateRangeKind.Last6Months:
                    dateRange = Create(DateRangePrecision.Months, -6);
                    break;
                case DateRangeKind.Last12Months:
                    dateRange = Create(DateRangePrecision.Months, -12);
                    break;

                case DateRangeKind.MonthToDate:
                    dateRange = new DateRange(new DateTime(now.Year, now.Month, 1), now.Date.AddDays(1));
                    break;
                case DateRangeKind.QuarterToDate:
                    int quarterMonth = (now.Month % 3) == 0 ? now.Month : 3 * (now.Month / 3) + 1;
                    dateRange = new DateRange(new DateTime(now.Year, quarterMonth, 1), now.Date.AddDays(1));
                    break;
                case DateRangeKind.YearToDate:
                    dateRange = new DateRange(new DateTime(now.Year, 1, 1), now.Date.AddDays(1));
                    break;

                case DateRangeKind.PreviousMonth:
                    DateTime lastMonth = now.AddMonths(-1);
                    dateRange = new DateRange(new DateTime(lastMonth.Year, lastMonth.Month, 1), new DateTime(now.Year, now.Month, 1));
                    break;
                case DateRangeKind.PreviousQuarter:
                    DateTime lastQuarter = now.AddMonths(-3);
                    int lastQuarterMonth = (lastQuarter.Month % 3) == 0 ? lastQuarter.Month : 3 * (lastQuarter.Month / 3) + 1;
                    lastQuarter = new DateTime(lastQuarter.Year, lastQuarterMonth, 1);
                    dateRange = new DateRange(lastQuarter, lastQuarter.AddMonths(3));
                    break;
                case DateRangeKind.PreviousYear:
                    DateTime lastYear = now.AddYears(-1);
                    dateRange = new DateRange(new DateTime(lastYear.Year, lastYear.Month, 1), new DateTime(now.Year, 1, 1));
                    break;
                default:
                    dateRange = DateRange.Empty;
                    break;
            }

            dateRange.Kind = dateRangeKind;

            return dateRange;
        }
Beispiel #8
0
        private static IEnumerable <TestCaseData> OnApplyTemplateCases()
        {
            var testName = nameof(OnApplyTemplateCases);

            yield return(new TestCaseData(
                             new PickerDataProps(), new PickerDataProps(Today, Today, DateRangeKind.Today))
                         .SetName($"{testName}: 010"));

            yield return(new TestCaseData(
                             new PickerDataProps(null, null, DateRangeKind.Custom), new PickerDataProps(Yesterday, Today, DateRangeKind.Custom))
                         .SetName($"{testName}: 020"));


            foreach (var kind in new DateRangeKind?[] { null, DateRangeKind.Custom })
            {
                yield return(new TestCaseData(
                                 new PickerDataProps(null, PastDate1, kind), new PickerDataProps(PastDate1, PastDate1, DateRangeKind.Custom))
                             .SetName($"{testName}: 030 {kind}"));

                yield return(new TestCaseData(
                                 new PickerDataProps(null, FutureDate, kind), new PickerDataProps(Yesterday, FutureDate, DateRangeKind.Custom))
                             .SetName($"{testName}: 040 {kind}"));

                yield return(new TestCaseData(
                                 new PickerDataProps(PastDate1, null, kind), new PickerDataProps(PastDate1, Today, DateRangeKind.Custom))
                             .SetName($"{testName}: 050 {kind}"));

                yield return(new TestCaseData(
                                 new PickerDataProps(FutureDate, null, kind), new PickerDataProps(FutureDate, FutureDate, DateRangeKind.Custom))
                             .SetName($"{testName}: 060 {kind}"));

                yield return(new TestCaseData(
                                 new PickerDataProps(PastDate1, PastDate2, kind), new PickerDataProps(PastDate1, PastDate2, DateRangeKind.Custom))
                             .SetName($"{testName}: 070 {kind}"));

                yield return(new TestCaseData(
                                 new PickerDataProps(PastDate2, PastDate1, kind), new PickerDataProps(PastDate2, PastDate2, DateRangeKind.Custom))
                             .SetName($"{testName}: 080 {kind}"));
            }


            foreach (DateRangeKind kind in Enum.GetValues(typeof(DateRangeKind)))
            {
                var kindsToPassBy = new DateRangeKind[] { DateRangeKind.Custom, DateRangeKind.Week, DateRangeKind.Month, DateRangeKind.Year };

                if (kindsToPassBy.Contains(kind))
                {
                    continue;
                }

                //for the case when DateRangeKind enum has more kinds than are actually contained in the Picker's DateRangeList.
                var dateRange = DateRangeHelper.GetDateRangeOfKind(kind);
                if (null == dateRange)
                {
                    continue;
                }

                yield return(new TestCaseData(
                                 new PickerDataProps(null, null, kind),
                                 new PickerDataProps(dateRange.From, dateRange.To, kind))
                             .SetName($"{testName}: 110 {kind}"));

                yield return(new TestCaseData(
                                 new PickerDataProps(PastDate1, null, kind),
                                 new PickerDataProps(dateRange.From, dateRange.To, kind))
                             .SetName($"{testName}: 120 {kind}"));

                yield return(new TestCaseData(
                                 new PickerDataProps(null, PastDate1, kind),
                                 new PickerDataProps(dateRange.From, dateRange.To, kind))
                             .SetName($"{testName}: 130 {kind}"));

                yield return(new TestCaseData(
                                 new PickerDataProps(PastDate1, PastDate2, kind),
                                 new PickerDataProps(dateRange.From, dateRange.To, kind))
                             .SetName($"{testName}: 140 {kind}"));

                yield return(new TestCaseData(
                                 new PickerDataProps(PastDate2, PastDate1, kind),
                                 new PickerDataProps(dateRange.From, dateRange.To, kind))
                             .SetName($"{testName}: 150 {kind}"));
            }
        }