static string CalcDateName(DateTimeParts part, DateTime value) { // Attempting to match T-SQL behavior // For any given datepart, if the values fall in the same part, then the result is zero. Then add 1 for each additional part that elapses. // E.g. jan-12 to dec-12 is zero years difference, but dec-11 to jan-13 is two years difference, even thought the gap is only 4 months longer. string result; switch (part) { case DateTimeParts.Year: result = value.Year.ToString(); break; case DateTimeParts.Quarter: result = QuarterNode.CalculateQuarter(value).ToString(); break; case DateTimeParts.Month: result = value.ToString("MMMM"); break; case DateTimeParts.Day: result = value.Day.ToString(); break; case DateTimeParts.Week: result = WeekNode.CalculateWeek(value).ToString(); break; case DateTimeParts.Hour: result = value.Hour.ToString(); break; case DateTimeParts.Minute: result = value.Minute.ToString(); break; case DateTimeParts.Second: result = value.Second.ToString(); break; case DateTimeParts.DayOfYear: result = value.DayOfYear.ToString(); break; case DateTimeParts.Weekday: result = value.ToString("dddd"); break; default: throw new InvalidOperationException(part.ToString()); } return(result); }
static int CalcDifference(DateTimeParts part, DateTime left, DateTime right) { // Attempting to match T-SQL behavior // For any given datepart, if the values fall in the same part, then the result is zero. Then add 1 for each additional part that elapses. // E.g. jan-12 to dec-12 is zero years difference, but dec-11 to jan-13 is two years difference, even thought the gap is only 4 months longer. int result; switch (part) { case DateTimeParts.Year: result = right.Year - left.Year; break; case DateTimeParts.Quarter: result = (right.Year * 12 + right.Month - 1) / 3 - (left.Year * 12 + left.Month - 1) / 3; break; case DateTimeParts.Month: result = right.Year * 12 + right.Month - left.Year * 12 - left.Month; break; case DateTimeParts.Day: result = (int)((right.Ticks / TimeSpan.TicksPerDay) - (left.Ticks / TimeSpan.TicksPerDay)); break; case DateTimeParts.Week: result = (int)((right.Ticks / TimeSpan.TicksPerDay + 1) / 7 - (left.Ticks / TimeSpan.TicksPerDay + 1) / 7); break; case DateTimeParts.Hour: result = (int)((right.Ticks / TimeSpan.TicksPerHour) - (left.Ticks / TimeSpan.TicksPerHour)); break; case DateTimeParts.Minute: result = (int)((right.Ticks / TimeSpan.TicksPerMinute) - (left.Ticks / TimeSpan.TicksPerMinute)); break; case DateTimeParts.Second: result = (int)((right.Ticks / TimeSpan.TicksPerSecond) - (left.Ticks / TimeSpan.TicksPerSecond)); break; // Because SQL just treats them the same as 'day' anyway. case DateTimeParts.DayOfYear: case DateTimeParts.Weekday: default: throw new InvalidOperationException(part.ToString()); } return(result); }
static DateTime AdjustDate(DateTimeParts part, int number, DateTime value) { DateTime result; switch (part) { case DateTimeParts.Year: result = value.AddYears(number); break; case DateTimeParts.Quarter: result = value.AddMonths(number * 3); break; case DateTimeParts.Month: result = value.AddMonths(number); break; case DateTimeParts.Day: result = value.AddDays(number); break; case DateTimeParts.Week: result = value.AddDays(number * 7); break; case DateTimeParts.Hour: result = value.AddHours(number); break; case DateTimeParts.Minute: result = value.AddMinutes(number); break; case DateTimeParts.Second: result = value.AddSeconds(number); break; // Because SQL just treats them the same as 'day' anyway. case DateTimeParts.DayOfYear: case DateTimeParts.Weekday: default: throw new InvalidOperationException(part.ToString()); } return(result); }