Fixed SystemTimeToFileTime().

svn path=/trunk/; revision=479
This commit is contained in:
Eric Kohl 1999-05-16 18:33:24 +00:00
parent 7aaccee0e9
commit 0344d1a114

View file

@ -30,19 +30,6 @@ typedef struct __DOSDATE
WORD Year:5; WORD Year:5;
} DOSDATE, *PDOSDATE; } DOSDATE, *PDOSDATE;
#define NSPERSEC 10000000
#define SECOND 1
#define MINUTE 60*SECOND
#define HOUR 60*MINUTE
#define DAY 24*HOUR
#define YEAR (365*DAY)
#define FOURYEAR (4*YEAR+DAY)
#define CENTURY (25*FOURYEAR-DAY)
#define MILLENIUM (100*CENTURY)
#define TICKSPERMIN 600000000 #define TICKSPERMIN 600000000
#define TICKSPERSEC 10000000 #define TICKSPERSEC 10000000
#define TICKSPERMSEC 10000 #define TICKSPERMSEC 10000
@ -70,12 +57,12 @@ 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 __inline void NormalizeTimeFields(CSHORT *FieldToNormalize, static __inline void NormalizeTimeFields(WORD *FieldToNormalize,
CSHORT *CarryField, WORD *CarryField,
int Modulus) int Modulus)
{ {
*FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus); *FieldToNormalize = (WORD) (*FieldToNormalize - Modulus);
*CarryField = (CSHORT) (*CarryField + 1); *CarryField = (WORD) (*CarryField + 1);
} }
@ -180,71 +167,67 @@ SystemTimeToFileTime(
) )
{ {
LARGE_INTEGER FileTime; int CurYear, CurMonth, MonthLength;
LARGE_INTEGER Year; long long int rcTime;
LARGE_INTEGER Month; SYSTEMTIME SystemTime;
LARGE_INTEGER Day;
LARGE_INTEGER Hour;
LARGE_INTEGER Minute;
LARGE_INTEGER Second;
LARGE_INTEGER Milliseconds;
DWORD LeapDay = 0;
DWORD dwMonthDays = 0;
if ( (lpSystemTime->wYear % 4 == 0 && lpSystemTime->wYear % 100 != 0) || lpSystemTime->wYear % 400 == 0) memcpy (&SystemTime, lpSystemTime, sizeof(SYSTEMTIME));
LeapDay = 1;
else
LeapDay = 0;
rcTime = 0;
Year = RtlEnlargedIntegerMultiply(lpSystemTime->wYear - 1601,YEAR);
if ( lpSystemTime->wMonth > 1) /* FIXME: normalize the TIME_FIELDS structure here */
dwMonthDays = 31; while (SystemTime.wSecond >= SECSPERMIN)
if ( lpSystemTime->wMonth > 2) {
dwMonthDays += ( 28 + LeapDay ); NormalizeTimeFields(&SystemTime.wSecond,
if ( lpSystemTime->wMonth > 3) &SystemTime.wMinute,
dwMonthDays += 31; SECSPERMIN);
if ( lpSystemTime->wMonth > 4) }
dwMonthDays += 30; while (SystemTime.wMinute >= MINSPERHOUR)
if ( lpSystemTime->wMonth > 5) {
dwMonthDays += 31; NormalizeTimeFields(&SystemTime.wMinute,
if ( lpSystemTime->wMonth > 6) &SystemTime.wHour,
dwMonthDays += 30; MINSPERHOUR);
if ( lpSystemTime->wMonth > 7) }
dwMonthDays += 31; while (SystemTime.wHour >= HOURSPERDAY)
if ( lpSystemTime->wMonth > 8) {
dwMonthDays += 31; NormalizeTimeFields(&SystemTime.wHour,
if ( lpSystemTime->wMonth > 9) &SystemTime.wDay,
dwMonthDays += 30; HOURSPERDAY);
if ( lpSystemTime->wMonth > 10) }
dwMonthDays += 31; MonthLength =
if ( lpSystemTime->wMonth > 11) MonthLengths[IsLeapYear(SystemTime.wYear)][SystemTime.wMonth - 1];
dwMonthDays += 30; while (SystemTime.wDay > MonthLength)
{
NormalizeTimeFields(&SystemTime.wDay,
&SystemTime.wMonth,
MonthLength);
}
while (SystemTime.wMonth > MONSPERYEAR)
{
NormalizeTimeFields(&SystemTime.wMonth,
&SystemTime.wYear,
MONSPERYEAR);
}
Month = RtlEnlargedIntegerMultiply(dwMonthDays,DAY); /* FIXME: handle calendar corrections here */
for (CurYear = EPOCHYEAR; CurYear < SystemTime.wYear; CurYear++)
{
rcTime += YearLengths[IsLeapYear(CurYear)];
}
for (CurMonth = 1; CurMonth < SystemTime.wMonth; CurMonth++)
{
rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
}
rcTime += SystemTime.wDay - 1;
rcTime *= SECSPERDAY;
rcTime += SystemTime.wHour * SECSPERHOUR +
SystemTime.wMinute * SECSPERMIN + SystemTime.wSecond;
rcTime *= TICKSPERSEC;
rcTime += SystemTime.wMilliseconds * TICKSPERMSEC;
Day = RtlEnlargedIntegerMultiply(lpSystemTime->wDay,DAY); *lpFileTime = *(FILETIME *)&rcTime;
Hour = RtlEnlargedIntegerMultiply(lpSystemTime->wHour,HOUR); return TRUE;
Minute = RtlEnlargedIntegerMultiply(lpSystemTime->wMinute,MINUTE);
Second = RtlEnlargedIntegerMultiply(lpSystemTime->wSecond,DAY);
Milliseconds = RtlEnlargedIntegerMultiply(lpSystemTime->wMilliseconds,10000);
FileTime = RtlLargeIntegerAdd(FileTime,Year);
FileTime = RtlLargeIntegerAdd(FileTime,Month);
FileTime = RtlLargeIntegerAdd(FileTime,Day);
FileTime = RtlLargeIntegerAdd(FileTime,Hour);
FileTime = RtlLargeIntegerAdd(FileTime,Minute);
FileTime = RtlLargeIntegerAdd(FileTime,Second);
FileTime = RtlExtendedIntegerMultiply(FileTime,NSPERSEC);
FileTime = RtlLargeIntegerAdd(FileTime,Milliseconds);
memcpy(lpFileTime,&FileTime,sizeof(FILETIME));
return TRUE;
} }