Extract days-since-epoch calculation code from RtlTimeToTimeFields into own function, optimize a bit, and use it in RtlTimeFieldsToTime instead of doing a 400 iteration loop.

svn path=/trunk/; revision=34352
This commit is contained in:
Jeffrey Morlan 2008-07-07 10:23:41 +00:00
parent 622a737e7f
commit f2d4e4394e

View file

@ -52,6 +52,15 @@ static __inline int IsLeapYear(int Year)
return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0; return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
} }
static int DaysSinceEpoch(int Year)
{
int Days;
Year--; /* Don't include a leap day from the current year */
Days = Year * DAYSPERNORMALYEAR + Year / 4 - Year / 100 + Year / 400;
Days -= (EPOCHYEAR - 1) * DAYSPERNORMALYEAR + (EPOCHYEAR - 1) / 4 - (EPOCHYEAR - 1) / 100 + (EPOCHYEAR - 1) / 400;
return Days;
}
static __inline void NormalizeTimeFields(CSHORT *FieldToNormalize, static __inline void NormalizeTimeFields(CSHORT *FieldToNormalize,
CSHORT *CarryField, CSHORT *CarryField,
int Modulus) int Modulus)
@ -170,11 +179,9 @@ RtlTimeFieldsToTime(
IN PTIME_FIELDS TimeFields, IN PTIME_FIELDS TimeFields,
OUT PLARGE_INTEGER Time) OUT PLARGE_INTEGER Time)
{ {
int CurYear;
int CurMonth; int CurMonth;
TIME_FIELDS IntTimeFields; TIME_FIELDS IntTimeFields;
Time->QuadPart = 0;
memcpy(&IntTimeFields, memcpy(&IntTimeFields,
TimeFields, TimeFields,
sizeof(TIME_FIELDS)); sizeof(TIME_FIELDS));
@ -213,13 +220,10 @@ RtlTimeFieldsToTime(
} }
/* Compute the time */ /* Compute the time */
for (CurYear = EPOCHYEAR; CurYear < IntTimeFields.Year; CurYear++) Time->QuadPart = DaysSinceEpoch(IntTimeFields.Year);
{
Time->QuadPart += YearLengths[IsLeapYear(CurYear)];
}
for (CurMonth = 1; CurMonth < IntTimeFields.Month; CurMonth++) for (CurMonth = 1; CurMonth < IntTimeFields.Month; CurMonth++)
{ {
Time->QuadPart += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1]; Time->QuadPart += MonthLengths[IsLeapYear(IntTimeFields.Year)][CurMonth - 1];
} }
Time->QuadPart += IntTimeFields.Day - 1; Time->QuadPart += IntTimeFields.Day - 1;
Time->QuadPart *= SECSPERDAY; Time->QuadPart *= SECSPERDAY;
@ -317,11 +321,7 @@ RtlTimeToTimeFields(
/* compute year */ /* compute year */
CurYear = EPOCHYEAR; CurYear = EPOCHYEAR;
CurYear += Days / DAYSPERLEAPYEAR; CurYear += Days / DAYSPERLEAPYEAR;
Days -= (CurYear - EPOCHYEAR) * DAYSPERLEAPYEAR; Days -= DaysSinceEpoch(CurYear);
CurYear--; /* The next calculation needs CurYear - 1 */
Days += CurYear - CurYear / 4 + CurYear / 100 - CurYear / 400;
CurYear++;
Days -= EPOCHYEAR - 1 - (EPOCHYEAR -1) / 4 + (EPOCHYEAR -1) / 100 - (EPOCHYEAR - 1) / 400;
while (1) while (1)
{ {
LeapYear = IsLeapYear(CurYear); LeapYear = IsLeapYear(CurYear);