Use the functions from ntdll in SystemeTimeToFileTime/FileTimeToSystemTime.

svn path=/trunk/; revision=3494
This commit is contained in:
Hartmut Birr 2002-09-13 18:53:17 +00:00
parent 5c88f1916c
commit 0542fdd563

View file

@ -1,4 +1,4 @@
/* $Id: time.c,v 1.16 2002/09/08 10:22:45 chorns Exp $ /* $Id: time.c,v 1.17 2002/09/13 18:53:17 hbirr Exp $
* *
* COPYRIGHT: See COPYING in the top level directory * COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries * PROJECT: ReactOS system libraries
@ -37,51 +37,6 @@ typedef struct __DOSDATE
} DOSDATE, *PDOSDATE; } DOSDATE, *PDOSDATE;
#define TICKSPERMIN 600000000 #define TICKSPERMIN 600000000
#define TICKSPERSEC 10000000
#define TICKSPERMSEC 10000
#define SECSPERDAY 86400
#define SECSPERHOUR 3600
#define SECSPERMIN 60
#define MINSPERHOUR 60
#define HOURSPERDAY 24
#define EPOCHWEEKDAY 0
#define DAYSPERWEEK 7
#define EPOCHYEAR 1601
#define DAYSPERNORMALYEAR 365
#define DAYSPERLEAPYEAR 366
#define MONSPERYEAR 12
static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
static const int MonthLengths[2][MONSPERYEAR] =
{
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
static __inline int IsLeapYear(int Year)
{
return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
}
static __inline void NormalizeTimeFields(WORD *FieldToNormalize,
WORD *CarryField,
int Modulus)
{
*FieldToNormalize = (WORD) (*FieldToNormalize - Modulus);
*CarryField = (WORD) (*CarryField + 1);
}
#define LISECOND RtlEnlargedUnsignedMultiply(SECOND,NSPERSEC)
#define LIMINUTE RtlEnlargedUnsignedMultiply(MINUTE,NSPERSEC)
#define LIHOUR RtlEnlargedUnsignedMultiply(HOUR,NSPERSEC)
#define LIDAY RtlEnlargedUnsignedMultiply(DAY,NSPERSEC)
#define LIYEAR RtlEnlargedUnsignedMultiply(YEAR,NSPERSEC)
#define LIFOURYEAR RtlEnlargedUnsignedMultiply(FOURYEAR,NSPERSEC)
#define LICENTURY RtlEnlargedUnsignedMultiply(CENTURY,NSPERSEC)
#define LIMILLENIUM RtlEnlargedUnsignedMultiply(CENTURY,10*NSPERSEC)
/* FUNCTIONS ****************************************************************/ /* FUNCTIONS ****************************************************************/
@ -186,67 +141,24 @@ SystemTimeToFileTime(
) )
{ {
int CurYear, CurMonth, MonthLength; TIME_FIELDS TimeFields;
long long int rcTime; LARGE_INTEGER liTime;
SYSTEMTIME SystemTime;
memcpy (&SystemTime, lpSystemTime, sizeof(SYSTEMTIME)); TimeFields.Year = lpSystemTime->wYear;
TimeFields.Month = lpSystemTime->wMonth;
TimeFields.Day = lpSystemTime->wDay;
TimeFields.Hour = lpSystemTime->wHour;
TimeFields.Minute = lpSystemTime->wMinute;
TimeFields.Second = lpSystemTime->wSecond;
TimeFields.Milliseconds = lpSystemTime->wMilliseconds;
rcTime = 0; if (RtlTimeFieldsToTime (&TimeFields, &liTime))
{
/* FIXME: normalize the TIME_FIELDS structure here */ lpFileTime->dwLowDateTime = liTime.u.LowPart;
while (SystemTime.wSecond >= SECSPERMIN) lpFileTime->dwHighDateTime = liTime.u.LowPart;
{ return TRUE;
NormalizeTimeFields(&SystemTime.wSecond, }
&SystemTime.wMinute, return FALSE;
SECSPERMIN);
}
while (SystemTime.wMinute >= MINSPERHOUR)
{
NormalizeTimeFields(&SystemTime.wMinute,
&SystemTime.wHour,
MINSPERHOUR);
}
while (SystemTime.wHour >= HOURSPERDAY)
{
NormalizeTimeFields(&SystemTime.wHour,
&SystemTime.wDay,
HOURSPERDAY);
}
MonthLength =
MonthLengths[IsLeapYear(SystemTime.wYear)][SystemTime.wMonth - 1];
while (SystemTime.wDay > MonthLength)
{
NormalizeTimeFields(&SystemTime.wDay,
&SystemTime.wMonth,
MonthLength);
}
while (SystemTime.wMonth > MONSPERYEAR)
{
NormalizeTimeFields(&SystemTime.wMonth,
&SystemTime.wYear,
MONSPERYEAR);
}
/* 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;
*lpFileTime = *(FILETIME *)&rcTime;
return TRUE;
} }
// dwDayOfWeek = RtlLargeIntegerDivide(FileTime,LIDAY,&dwRemDay); // dwDayOfWeek = RtlLargeIntegerDivide(FileTime,LIDAY,&dwRemDay);
@ -261,68 +173,27 @@ FileTimeToSystemTime(
LPSYSTEMTIME lpSystemTime LPSYSTEMTIME lpSystemTime
) )
{ {
const int *Months; TIME_FIELDS TimeFields;
int LeapSecondCorrections, SecondsInDay, CurYear; LARGE_INTEGER liTime;
int LeapYear, CurMonth;
long int Days;
long long int Time = *((long long int*)lpFileTime);
/* Extract millisecond from time and convert time into seconds */ liTime.u.LowPart = lpFileTime->dwLowDateTime;
lpSystemTime->wMilliseconds = (WORD)((Time % TICKSPERSEC) / TICKSPERMSEC); liTime.u.HighPart = lpFileTime->dwHighDateTime;
Time = Time / TICKSPERSEC;
/* FIXME: Compute the number of leap second corrections here */ if (liTime.u.HighPart >= 0x80000000)
LeapSecondCorrections = 0; {
return FALSE;
}
/* Split the time into days and seconds within the day */ RtlTimeToTimeFields(&liTime, &TimeFields);
Days = Time / SECSPERDAY;
SecondsInDay = Time % SECSPERDAY;
/* Adjust the values for GMT and leap seconds */ lpSystemTime->wYear = TimeFields.Year;
SecondsInDay += LeapSecondCorrections; lpSystemTime->wMonth = TimeFields.Month;
while (SecondsInDay < 0) lpSystemTime->wDay = TimeFields.Day;
{ lpSystemTime->wHour = TimeFields.Hour;
SecondsInDay += SECSPERDAY; lpSystemTime->wMinute = TimeFields.Minute;
Days--; lpSystemTime->wSecond = TimeFields.Second;
} lpSystemTime->wMilliseconds = TimeFields.Milliseconds;
while (SecondsInDay >= SECSPERDAY) lpSystemTime->wDayOfWeek = TimeFields.Weekday;
{
SecondsInDay -= SECSPERDAY;
Days++;
}
/* compute time of day */
lpSystemTime->wHour = (WORD) (SecondsInDay / SECSPERHOUR);
SecondsInDay = SecondsInDay % SECSPERHOUR;
lpSystemTime->wMinute = (WORD) (SecondsInDay / SECSPERMIN);
lpSystemTime->wSecond = (WORD) (SecondsInDay % SECSPERMIN);
/* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
/* compute day of week */
lpSystemTime->wDayOfWeek = (WORD) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
/* compute year */
CurYear = EPOCHYEAR;
/* FIXME: handle calendar modifications */
while (1)
{
LeapYear = IsLeapYear(CurYear);
if (Days < (long) YearLengths[LeapYear])
{
break;
}
CurYear++;
Days = Days - (long) YearLengths[LeapYear];
}
lpSystemTime->wYear = (WORD) CurYear;
/* Compute month of year */
Months = MonthLengths[LeapYear];
for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
Days = Days - (long) Months[CurMonth];
lpSystemTime->wMonth = (WORD) (CurMonth + 1);
lpSystemTime->wDay = (WORD) (Days + 1);
return TRUE; return TRUE;
} }