/** * @function GetRelativeTo * @brief Computes the valid dates relative to the relativeDate parameter taking * in account the delta parameter (-1 or 1). This function is called from the * GetNext and GetPrevious functions inherited from MonthDataSource class. * @param1 relativeDate DateTime represents the date taken in account for the * computations involving the previous/next elements inside a looping selector * @param2 delta int represents the delta for the calculus, -1 implies the computations * for a previous date and 1 for a next date. * @return DateTime object relative to the value given as param1 regarding param2 */ protected override DateTime?GetRelativeTo(DateTime relativeDate, int delta) { Microsoft.Phone.Controls.Primitives.DateTimeWrapper currentDate = (Microsoft.Phone.Controls.Primitives.DateTimeWrapper) this.SelectedItem; int currentYear = currentDate.DateTime.Year; int monthsInYear = 12; // If the currentYear has the same value as the MaxDate.Year then the number of months inside this year // will be equal to the month number of the MaxDate.Year if (currentYear == MaxDate.Year) { monthsInYear = MaxDate.Month; } // Computing the nextMonth, taking in acount the delta (-1 or 1) int nextMonth = ((monthsInYear + relativeDate.Month - 1 + delta) % monthsInYear) + 1; // If our month makes the return date to out of bounds (regarding the Min and Max Dates // we return null instead of the new date; this will make the looping stop at this certain value if (MaxDate.Year == currentYear && MaxDate.Year == MinDate.Year && (nextMonth < MinDate.Month || nextMonth > MaxDate.Month)) { return(null); } // Compute the next day int nextDay = Math.Min(relativeDate.Day, DateTime.DaysInMonth(relativeDate.Year, nextMonth)); // Return the newly calculated DateTime object return(new DateTime(relativeDate.Year, nextMonth, nextDay, relativeDate.Hour, relativeDate.Minute, relativeDate.Second)); }
/** * @function GetRelativeTo * @brief Computes the valid dates relative to the relativeDate parameter taking * in account the delta parameter (-1 or 1). This function is called from the * GetNext and GetPrevious functions inherited from DayDataSource class. * @param1 relativeDate DateTime represents the date taken in account for the * computations involving the previous/next elements inside a looping selector * @param2 delta int represents the delta for the calculus, -1 implies the computations * for a previous date and 1 for a next date. * @return DateTime object relative to the value given as param1 regarding param2 */ protected override DateTime?GetRelativeTo(DateTime relativeDate, int delta) { Microsoft.Phone.Controls.Primitives.DateTimeWrapper currentDate = (Microsoft.Phone.Controls.Primitives.DateTimeWrapper) this.SelectedItem; int currentMonth = currentDate.DateTime.Month; int currentYear = currentDate.DateTime.Year; // If the date that we try to compute will be larger than the max date. if ((MaxDate.Day == relativeDate.Day) && (MaxDate.Month == currentMonth) && (MaxDate.Year == currentYear) && (delta > 0)) { return(null); } // If the date we try to compute will be smaller than the min date. if ((MinDate.Day == relativeDate.Day) && (MinDate.Month == currentMonth) && (MinDate.Year == currentYear) && (delta < 0)) { return(null); } // Compute the days in month value. int daysInMonth = DateTime.DaysInMonth(currentYear, currentMonth); // Compute the next day value. int nextDay = ((((daysInMonth + relativeDate.Day) - 1) + delta) % daysInMonth) + 1; // The special case in which we have the max year / monthe equal with the min year / month and basically // we have just a day range if (MaxDate.Year == currentYear && MaxDate.Month == currentMonth && MaxDate.Year == MinDate.Year && MaxDate.Month == MinDate.Month && (nextDay < MinDate.Day || nextDay > MaxDate.Day)) { return(null); } if ((MaxDate.Year == currentYear && MaxDate.Month == currentMonth && nextDay > MaxDate.Day) || (MinDate.Year == currentYear && MinDate.Month == currentMonth && nextDay < MinDate.Day)) { return(null); } return(new DateTime(currentYear, currentMonth, nextDay, relativeDate.Hour, relativeDate.Minute, relativeDate.Second)); }