Exemplo n.º 1
0
        private static string LastDateValue(TimexProperty timex, DateObject date)
        {
            if (timex.Month != null && timex.DayOfMonth != null)
            {
                return(TimexValue.DateValue(new TimexProperty
                {
                    Year = date.Year - 1,
                    Month = timex.Month,
                    DayOfMonth = timex.DayOfMonth,
                }));
            }

            if (timex.DayOfWeek != null)
            {
                var day    = timex.DayOfWeek == 7 ? DayOfWeek.Sunday : (DayOfWeek)timex.DayOfWeek;
                var result = TimexDateHelpers.DateOfLastDay(day, date);
                return(TimexValue.DateValue(new TimexProperty
                {
                    Year = result.Year,
                    Month = result.Month,
                    DayOfMonth = result.Day,
                }));
            }

            return(string.Empty);
        }
Exemplo n.º 2
0
        private static DateObject GenerateWeekDate(TimexProperty timex, DateObject date, bool isBefore)
        {
            DateObject start;

            if (timex.WeekOfMonth == null && timex.WeekOfYear == null)
            {
                var day = timex.DayOfWeek == 7 ? DayOfWeek.Sunday : (DayOfWeek)timex.DayOfWeek;
                if (isBefore)
                {
                    start = TimexDateHelpers.DateOfLastDay(day, date);
                }
                else
                {
                    start = TimexDateHelpers.DateOfNextDay(day, date);
                }
            }
            else
            {
                int dayOfWeek = timex.DayOfWeek.Value - 1;
                int year      = timex.Year ?? date.Year;
                if (timex.WeekOfYear != null)
                {
                    int weekOfYear = timex.WeekOfYear.Value;
                    start = TimexHelpers.FirstDateOfWeek(year, weekOfYear, CultureInfo.InvariantCulture).AddDays(dayOfWeek);
                    if (timex.Year == null)
                    {
                        if (isBefore && start > date)
                        {
                            start = TimexHelpers.FirstDateOfWeek(year - 1, weekOfYear, CultureInfo.InvariantCulture).AddDays(dayOfWeek);
                        }
                        else if (!isBefore && start < date)
                        {
                            start = TimexHelpers.FirstDateOfWeek(year + 1, weekOfYear, CultureInfo.InvariantCulture).AddDays(dayOfWeek);
                        }
                    }
                }
                else
                {
                    int month       = timex.Month ?? date.Month;
                    int weekOfMonth = timex.WeekOfMonth.Value;
                    start = TimexHelpers.GenerateMonthWeekDateStart(year, month, weekOfMonth).AddDays(dayOfWeek);
                    if (timex.Year == null || timex.Month == null)
                    {
                        if (isBefore && start > date)
                        {
                            start = TimexHelpers.GenerateMonthWeekDateStart(timex.Month != null ? year - 1 : year, timex.Month == null ? month - 1 : month, weekOfMonth).AddDays(dayOfWeek);
                        }
                        else if (!isBefore && start < date)
                        {
                            start = TimexHelpers.GenerateMonthWeekDateStart(timex.Month != null ? year + 1 : year, timex.Month == null ? month + 1 : month, weekOfMonth).AddDays(dayOfWeek);
                        }
                    }
                }
            }

            return(start);
        }
Exemplo n.º 3
0
        public static string LastWeek(System.DateTime date = default(System.DateTime))
        {
            var d     = (date == default(System.DateTime)) ? System.DateTime.Now : date;
            var start = TimexDateHelpers.DateOfLastDay(DayOfWeek.Monday, d);

            start = start.AddDays(-7);
            var t = TimexProperty.FromDate(start);

            t.Days = 7;
            return(t.TimexValue);
        }
Exemplo n.º 4
0
        private static Tuple <string, string> WeekDateRange(int year, int weekOfYear)
        {
            var dateInWeek = new DateObject(year, 1, 1) + TimeSpan.FromDays((weekOfYear - 1) * 7);

            var start = TimexDateHelpers.DateOfLastDay(DayOfWeek.Monday, dateInWeek);
            var end   = TimexDateHelpers.DateOfLastDay(DayOfWeek.Monday, dateInWeek + TimeSpan.FromDays(7));

            return(new Tuple <string, string>(
                       TimexValue.DateValue(new TimexProperty {
                Year = start.Year, Month = start.Month, DayOfMonth = start.Day
            }),
                       TimexValue.DateValue(new TimexProperty {
                Year = end.Year, Month = end.Month, DayOfMonth = end.Day
            })));
        }