1999-03-09 18:47:51 +01:00
|
|
|
/*
|
|
|
|
* Conversion between Time and TimeFields
|
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
|
|
|
|
* adapted to wine with special permissions of the author
|
1999-03-09 18:47:51 +01:00
|
|
|
* Rex Jolliff (rex@lvcablemodem.com)
|
2002-06-01 01:06:46 +02:00
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
* Copyright 1999 Juergen Schmied
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
1999-03-09 18:47:51 +01:00
|
|
|
*/
|
|
|
|
|
2002-08-17 02:43:16 +02:00
|
|
|
#include "config.h"
|
2002-08-31 21:04:14 +02:00
|
|
|
#include "wine/port.h"
|
2002-08-17 02:43:16 +02:00
|
|
|
|
1999-03-25 16:57:35 +01:00
|
|
|
#include <string.h>
|
2000-10-01 03:40:42 +02:00
|
|
|
#include <time.h>
|
2002-08-17 02:43:16 +02:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
1999-04-19 16:56:29 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
|
1999-03-09 18:47:51 +01:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
2000-10-01 03:40:42 +02:00
|
|
|
/* 1601 to 1970 is 369 years plus 89 leap days */
|
2002-08-27 20:17:49 +02:00
|
|
|
#define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)86400)
|
2000-10-01 03:40:42 +02:00
|
|
|
/* 1601 to 1980 is 379 years plus 91 leap days */
|
2002-08-27 20:17:49 +02:00
|
|
|
#define SECS_1601_to_1980 ((379 * 365 + 91) * (ULONGLONG)86400)
|
2000-10-01 03:40:42 +02:00
|
|
|
|
|
|
|
|
1999-03-09 18:47:51 +01:00
|
|
|
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 }
|
|
|
|
};
|
|
|
|
|
1999-05-08 14:50:36 +02:00
|
|
|
static inline int IsLeapYear(int Year)
|
1999-03-09 18:47:51 +01:00
|
|
|
{
|
|
|
|
return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
1999-05-08 14:50:36 +02:00
|
|
|
static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
|
1999-03-09 18:47:51 +01:00
|
|
|
{
|
|
|
|
*FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
|
|
|
|
*CarryField = (CSHORT) (*CarryField + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* RtlTimeToTimeFields [NTDLL.@]
|
1999-03-09 18:47:51 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
VOID WINAPI RtlTimeToTimeFields(
|
|
|
|
PLARGE_INTEGER liTime,
|
|
|
|
PTIME_FIELDS TimeFields)
|
|
|
|
{
|
|
|
|
const int *Months;
|
|
|
|
int LeapSecondCorrections, SecondsInDay, CurYear;
|
|
|
|
int LeapYear, CurMonth, GMTOffset;
|
|
|
|
long int Days;
|
2002-08-27 20:17:49 +02:00
|
|
|
LONGLONG Time = *(LONGLONG *)&liTime;
|
1999-03-09 18:47:51 +01:00
|
|
|
|
|
|
|
/* Extract millisecond from time and convert time into seconds */
|
|
|
|
TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
|
|
|
|
Time = Time / TICKSPERSEC;
|
|
|
|
|
|
|
|
/* FIXME: Compute the number of leap second corrections here */
|
|
|
|
LeapSecondCorrections = 0;
|
|
|
|
|
|
|
|
/* FIXME: get the GMT offset here */
|
|
|
|
GMTOffset = 0;
|
|
|
|
|
|
|
|
/* Split the time into days and seconds within the day */
|
|
|
|
Days = Time / SECSPERDAY;
|
|
|
|
SecondsInDay = Time % SECSPERDAY;
|
|
|
|
|
|
|
|
/* Adjust the values for GMT and leap seconds */
|
|
|
|
SecondsInDay += (GMTOffset - LeapSecondCorrections);
|
2002-06-01 01:06:46 +02:00
|
|
|
while (SecondsInDay < 0)
|
1999-03-09 18:47:51 +01:00
|
|
|
{ SecondsInDay += SECSPERDAY;
|
|
|
|
Days--;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
while (SecondsInDay >= SECSPERDAY)
|
1999-03-09 18:47:51 +01:00
|
|
|
{ SecondsInDay -= SECSPERDAY;
|
|
|
|
Days++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute time of day */
|
|
|
|
TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
|
|
|
|
SecondsInDay = SecondsInDay % SECSPERHOUR;
|
|
|
|
TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
|
|
|
|
TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
|
|
|
|
|
|
|
|
/* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
|
|
|
|
|
|
|
|
/* compute day of week */
|
|
|
|
TimeFields->Weekday = (CSHORT) ((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];
|
|
|
|
}
|
|
|
|
TimeFields->Year = (CSHORT) CurYear;
|
|
|
|
|
|
|
|
/* Compute month of year */
|
|
|
|
Months = MonthLengths[LeapYear];
|
|
|
|
for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
|
|
|
|
Days = Days - (long) Months[CurMonth];
|
|
|
|
TimeFields->Month = (CSHORT) (CurMonth + 1);
|
|
|
|
TimeFields->Day = (CSHORT) (Days + 1);
|
|
|
|
}
|
|
|
|
/******************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* RtlTimeFieldsToTime [NTDLL.@]
|
1999-03-09 18:47:51 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
BOOLEAN WINAPI RtlTimeFieldsToTime(
|
|
|
|
PTIME_FIELDS tfTimeFields,
|
|
|
|
PLARGE_INTEGER Time)
|
|
|
|
{
|
|
|
|
int CurYear, CurMonth;
|
2002-08-27 20:17:49 +02:00
|
|
|
LONGLONG rcTime;
|
1999-03-09 18:47:51 +01:00
|
|
|
TIME_FIELDS TimeFields = *tfTimeFields;
|
|
|
|
|
|
|
|
rcTime = 0;
|
|
|
|
|
|
|
|
/* FIXME: normalize the TIME_FIELDS structure here */
|
|
|
|
while (TimeFields.Second >= SECSPERMIN)
|
|
|
|
{ NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
|
|
|
|
}
|
|
|
|
while (TimeFields.Minute >= MINSPERHOUR)
|
|
|
|
{ NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
|
|
|
|
}
|
|
|
|
while (TimeFields.Hour >= HOURSPERDAY)
|
|
|
|
{ NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
|
|
|
|
}
|
|
|
|
while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
|
|
|
|
{ NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
|
|
|
|
}
|
|
|
|
while (TimeFields.Month > MONSPERYEAR)
|
|
|
|
{ NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: handle calendar corrections here */
|
|
|
|
for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
|
|
|
|
{ rcTime += YearLengths[IsLeapYear(CurYear)];
|
|
|
|
}
|
|
|
|
for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
|
|
|
|
{ rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
|
|
|
|
}
|
|
|
|
rcTime += TimeFields.Day - 1;
|
|
|
|
rcTime *= SECSPERDAY;
|
|
|
|
rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
|
|
|
|
rcTime *= TICKSPERSEC;
|
|
|
|
rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
|
|
|
|
*Time = *(LARGE_INTEGER *)&rcTime;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
/************* end of code by Rex Jolliff (rex@lvcablemodem.com) *******************/
|
|
|
|
|
|
|
|
/******************************************************************************
|
2001-06-21 01:03:14 +02:00
|
|
|
* RtlSystemTimeToLocalTime [NTDLL.@]
|
1999-03-09 18:47:51 +01:00
|
|
|
*/
|
|
|
|
VOID WINAPI RtlSystemTimeToLocalTime(
|
|
|
|
IN PLARGE_INTEGER SystemTime,
|
|
|
|
OUT PLARGE_INTEGER LocalTime)
|
|
|
|
{
|
1999-05-23 12:25:25 +02:00
|
|
|
FIXME("(%p, %p),stub!\n",SystemTime,LocalTime);
|
1999-03-09 18:47:51 +01:00
|
|
|
|
|
|
|
memcpy (LocalTime, SystemTime, sizeof (PLARGE_INTEGER));
|
|
|
|
}
|
2000-07-29 23:56:59 +02:00
|
|
|
|
1999-03-09 18:47:51 +01:00
|
|
|
/******************************************************************************
|
2001-06-21 01:03:14 +02:00
|
|
|
* RtlTimeToSecondsSince1970 [NTDLL.@]
|
1999-03-09 18:47:51 +01:00
|
|
|
*/
|
2002-09-13 00:07:02 +02:00
|
|
|
BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *time, PULONG res )
|
1999-03-09 18:47:51 +01:00
|
|
|
{
|
2002-09-13 00:07:02 +02:00
|
|
|
ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
|
2002-08-27 20:17:49 +02:00
|
|
|
tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
|
2000-10-01 03:40:42 +02:00
|
|
|
tmp -= SECS_1601_TO_1970;
|
|
|
|
if (tmp > 0xffffffff) return FALSE;
|
|
|
|
*res = (DWORD)tmp;
|
2000-07-29 23:56:59 +02:00
|
|
|
return TRUE;
|
1999-03-09 18:47:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2001-06-21 01:03:14 +02:00
|
|
|
* RtlTimeToSecondsSince1980 [NTDLL.@]
|
1999-03-09 18:47:51 +01:00
|
|
|
*/
|
2002-09-13 00:07:02 +02:00
|
|
|
BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *time, LPDWORD res )
|
1999-03-09 18:47:51 +01:00
|
|
|
{
|
2002-09-13 00:07:02 +02:00
|
|
|
ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
|
2002-08-27 20:17:49 +02:00
|
|
|
tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
|
2000-10-01 03:40:42 +02:00
|
|
|
tmp -= SECS_1601_to_1980;
|
|
|
|
if (tmp > 0xffffffff) return FALSE;
|
|
|
|
*res = (DWORD)tmp;
|
2000-07-29 23:56:59 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2001-06-21 01:03:14 +02:00
|
|
|
* RtlSecondsSince1970ToTime [NTDLL.@]
|
2000-07-29 23:56:59 +02:00
|
|
|
*/
|
2002-09-13 00:07:02 +02:00
|
|
|
void WINAPI RtlSecondsSince1970ToTime( DWORD time, LARGE_INTEGER *res )
|
2000-07-29 23:56:59 +02:00
|
|
|
{
|
2002-05-29 19:04:10 +02:00
|
|
|
ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_TO_1970, 10000000 );
|
2002-09-13 00:07:02 +02:00
|
|
|
res->s.LowPart = (DWORD)secs;
|
|
|
|
res->s.HighPart = (DWORD)(secs >> 32);
|
2000-07-29 23:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2001-06-21 01:03:14 +02:00
|
|
|
* RtlSecondsSince1980ToTime [NTDLL.@]
|
2000-07-29 23:56:59 +02:00
|
|
|
*/
|
2002-09-13 00:07:02 +02:00
|
|
|
void WINAPI RtlSecondsSince1980ToTime( DWORD time, LARGE_INTEGER *res )
|
2000-07-29 23:56:59 +02:00
|
|
|
{
|
2002-05-29 19:04:10 +02:00
|
|
|
ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_to_1980, 10000000 );
|
2002-09-13 00:07:02 +02:00
|
|
|
res->s.LowPart = (DWORD)secs;
|
|
|
|
res->s.HighPart = (DWORD)(secs >> 32);
|
1999-03-09 18:47:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* RtlTimeToElapsedTimeFields [NTDLL.@]
|
1999-03-09 18:47:51 +01:00
|
|
|
* FIXME: prototype guessed
|
|
|
|
*/
|
|
|
|
VOID WINAPI RtlTimeToElapsedTimeFields(
|
|
|
|
PLARGE_INTEGER liTime,
|
|
|
|
PTIME_FIELDS TimeFields)
|
|
|
|
{
|
1999-05-23 12:25:25 +02:00
|
|
|
FIXME("(%p,%p): stub\n",liTime,TimeFields);
|
1999-03-09 18:47:51 +01:00
|
|
|
}
|
2000-10-01 03:40:42 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtQuerySystemTime (NTDLL.@)
|
2001-07-11 20:56:41 +02:00
|
|
|
* ZwQuerySystemTime (NTDLL.@)
|
2000-10-01 03:40:42 +02:00
|
|
|
*/
|
2002-09-13 00:07:02 +02:00
|
|
|
NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER time )
|
2000-10-01 03:40:42 +02:00
|
|
|
{
|
2002-07-01 20:14:27 +02:00
|
|
|
ULONGLONG secs;
|
2000-10-01 03:40:42 +02:00
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
gettimeofday( &now, 0 );
|
2002-07-01 20:14:27 +02:00
|
|
|
secs = RtlExtendedIntegerMultiply( now.tv_sec+SECS_1601_TO_1970, 10000000 ) + now.tv_usec * 10;
|
|
|
|
time->s.LowPart = (DWORD)secs;
|
|
|
|
time->s.HighPart = (DWORD)(secs >> 32);
|
2002-09-13 00:07:02 +02:00
|
|
|
return STATUS_SUCCESS;
|
2000-10-01 03:40:42 +02:00
|
|
|
}
|