private GetDatePart ( long ticks, int part ) : int | ||
ticks | long | |
part | int | |
return | int |
internal int GetDatePart(long ticks, int part)
{
// Gregorian 1/1/0001 is Julian 1/3/0001. Remember DateTime(0) is refered to Gregorian 1/1/0001.
// The following line convert Gregorian ticks to Julian ticks.
long julianTicks = ticks + TicksPerDay * 2;
// n = number of days since 1/1/0001
int n = (int)(julianTicks / TicksPerDay);
// y4 = number of whole 4-year periods within 100-year period
int y4 = n / JulianDaysPer4Years;
// n = day number within 4-year period
n -= y4 * JulianDaysPer4Years;
// y1 = number of whole years within 4-year period
int y1 = n / JulianDaysPerYear;
// Last year has an extra day, so decrement result if 4
if (y1 == 4) y1 = 3;
// If year was requested, compute and return it
if (part == DatePartYear)
{
return (y4 * 4 + y1 + 1);
}
// n = day number within year
n -= y1 * JulianDaysPerYear;
// If day-of-year was requested, return it
if (part == DatePartDayOfYear)
{
return (n + 1);
}
// Leap year calculation looks different from IsLeapYear since y1, y4,
// and y100 are relative to year 1, not year 0
bool leapYear = (y1 == 3);
int[] days = leapYear? DaysToMonth366: DaysToMonth365;
// All months have less than 32 days, so n >> 5 is a good conservative
// estimate for the month
int m = n >> 5 + 1;
// m = 1-based month number
while (n >= days[m]) m++;
// If month was requested, return it
if (part == DatePartMonth) return (m);
// Return 1-based day-of-month
return (n - days[m - 1] + 1);
}
/// <summary>Returns a <see cref="T:System.DateTime" /> that is the specified number of months away from the specified <see cref="T:System.DateTime" />.</summary> /// <param name="time">The <see cref="T:System.DateTime" /> to which to add months. </param> /// <param name="months">The number of months to add. </param> /// <returns>The <see cref="T:System.DateTime" /> that results from adding the specified number of months to the specified <see cref="T:System.DateTime" />.</returns> /// <exception cref="T:System.ArgumentException">The resulting <see cref="T:System.DateTime" /> is outside the supported range. </exception> /// <exception cref="T:System.ArgumentOutOfRangeException"> /// <paramref name="months" /> is less than -120000.-or- /// <paramref name="months" /> is greater than 120000. </exception> // Token: 0x06002F49 RID: 12105 RVA: 0x000B53F8 File Offset: 0x000B35F8 public override DateTime AddMonths(DateTime time, int months) { if (months < -120000 || months > 120000) { throw new ArgumentOutOfRangeException("months", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), -120000, 120000)); } int num = JulianCalendar.GetDatePart(time.Ticks, 0); int num2 = JulianCalendar.GetDatePart(time.Ticks, 2); int num3 = JulianCalendar.GetDatePart(time.Ticks, 3); int num4 = num2 - 1 + months; if (num4 >= 0) { num2 = num4 % 12 + 1; num += num4 / 12; } else { num2 = 12 + (num4 + 1) % 12; num += (num4 - 11) / 12; } int[] array = (num % 4 == 0 && (num % 100 != 0 || num % 400 == 0)) ? JulianCalendar.DaysToMonth366 : JulianCalendar.DaysToMonth365; int num5 = array[num2] - array[num2 - 1]; if (num3 > num5) { num3 = num5; } long ticks = JulianCalendar.DateToTicks(num, num2, num3) + time.Ticks % 864000000000L; Calendar.CheckAddResult(ticks, this.MinSupportedDateTime, this.MaxSupportedDateTime); return(new DateTime(ticks)); }