1999-03-09 18:47:51 +01:00
|
|
|
/*
|
2002-11-25 02:12:39 +01:00
|
|
|
* Nt time functions.
|
1999-03-09 18:47:51 +01:00
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
|
2003-08-19 02:56:34 +02:00
|
|
|
* adapted to wine with special permissions of the author. This code is
|
|
|
|
* Copyright 2002 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
|
2007-08-07 13:01:16 +02:00
|
|
|
* Copyright 2007 Dmitry Timoshkov
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2002-11-27 21:14:45 +01:00
|
|
|
#include <stdlib.h>
|
2007-07-29 14:31:00 +02:00
|
|
|
#include <errno.h>
|
1999-03-25 16:57:35 +01:00
|
|
|
#include <string.h>
|
2004-09-08 01:01:34 +02:00
|
|
|
#include <limits.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
|
2003-01-07 21:36:20 +01:00
|
|
|
|
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2002-11-25 02:12:39 +01:00
|
|
|
#include "wine/unicode.h"
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
2003-04-03 00:48:59 +02:00
|
|
|
#include "ntdll_misc.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
|
|
|
|
2007-08-07 13:01:16 +02:00
|
|
|
static RTL_CRITICAL_SECTION TIME_tz_section;
|
2005-06-25 20:00:57 +02:00
|
|
|
static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
|
2004-02-27 01:43:20 +01:00
|
|
|
{
|
2007-08-07 13:01:16 +02:00
|
|
|
0, 0, &TIME_tz_section,
|
2004-02-27 01:43:20 +01:00
|
|
|
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
|
2007-08-07 13:01:16 +02:00
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": TIME_tz_section") }
|
2004-02-27 01:43:20 +01:00
|
|
|
};
|
2007-08-07 13:01:16 +02:00
|
|
|
static RTL_CRITICAL_SECTION TIME_tz_section = { &critsect_debug, -1, 0, 0, 0, 0 };
|
2004-02-27 01:43:20 +01:00
|
|
|
|
2002-11-25 02:12:39 +01:00
|
|
|
#define SETTIME_MAX_ADJUST 120
|
|
|
|
|
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
|
2002-12-13 21:53:04 +01:00
|
|
|
#define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
|
1999-03-09 18:47:51 +01:00
|
|
|
#define DAYSPERWEEK 7
|
|
|
|
#define EPOCHYEAR 1601
|
|
|
|
#define DAYSPERNORMALYEAR 365
|
|
|
|
#define DAYSPERLEAPYEAR 366
|
|
|
|
#define MONSPERYEAR 12
|
2004-02-24 02:01:27 +01:00
|
|
|
#define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
|
|
|
|
#define DAYSPERNORMALCENTURY (365 * 100 + 24)
|
|
|
|
#define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
|
1999-03-09 18:47:51 +01:00
|
|
|
|
2000-10-01 03:40:42 +02:00
|
|
|
/* 1601 to 1970 is 369 years plus 89 leap days */
|
2003-03-31 03:37:04 +02:00
|
|
|
#define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
|
|
|
|
#define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
|
2000-10-01 03:40:42 +02:00
|
|
|
/* 1601 to 1980 is 379 years plus 91 leap days */
|
2003-03-31 03:37:04 +02:00
|
|
|
#define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
|
|
|
|
#define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
|
2004-09-08 01:01:34 +02:00
|
|
|
/* max ticks that can be represented as Unix time */
|
|
|
|
#define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
|
2000-10-01 03:40:42 +02:00
|
|
|
|
|
|
|
|
1999-03-09 18:47:51 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2002-11-25 02:12:39 +01:00
|
|
|
* RtlTimeToTimeFields [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Convert a time into a TIME_FIELDS structure.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* PARAMS
|
2003-08-19 02:56:34 +02:00
|
|
|
* liTime [I] Time to convert.
|
|
|
|
* TimeFields [O] Destination for the converted time.
|
1999-03-09 18:47:51 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2002-11-25 02:12:39 +01:00
|
|
|
* Nothing.
|
1999-03-09 18:47:51 +01:00
|
|
|
*/
|
|
|
|
VOID WINAPI RtlTimeToTimeFields(
|
2002-11-25 02:12:39 +01:00
|
|
|
const LARGE_INTEGER *liTime,
|
1999-03-09 18:47:51 +01:00
|
|
|
PTIME_FIELDS TimeFields)
|
|
|
|
{
|
2004-11-06 04:53:53 +01:00
|
|
|
int SecondsInDay;
|
|
|
|
long int cleaps, years, yearday, months;
|
1999-03-09 18:47:51 +01:00
|
|
|
long int Days;
|
2004-11-06 04:53:53 +01:00
|
|
|
LONGLONG Time;
|
1999-03-09 18:47:51 +01:00
|
|
|
|
|
|
|
/* Extract millisecond from time and convert time into seconds */
|
2004-11-06 04:53:53 +01:00
|
|
|
TimeFields->Milliseconds =
|
|
|
|
(CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
|
|
|
|
Time = liTime->QuadPart / TICKSPERSEC;
|
1999-03-09 18:47:51 +01:00
|
|
|
|
2002-11-06 22:57:23 +01:00
|
|
|
/* The native version of RtlTimeToTimeFields does not take leap seconds
|
|
|
|
* into account */
|
1999-03-09 18:47:51 +01:00
|
|
|
|
|
|
|
/* Split the time into days and seconds within the day */
|
|
|
|
Days = Time / SECSPERDAY;
|
|
|
|
SecondsInDay = Time % SECSPERDAY;
|
|
|
|
|
|
|
|
/* compute time of day */
|
|
|
|
TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
|
|
|
|
SecondsInDay = SecondsInDay % SECSPERHOUR;
|
|
|
|
TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
|
|
|
|
TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
|
|
|
|
|
|
|
|
/* compute day of week */
|
|
|
|
TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
|
|
|
|
|
2004-11-06 04:53:53 +01:00
|
|
|
/* compute year, month and day of month. */
|
|
|
|
cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
|
|
|
|
Days += 28188 + cleaps;
|
|
|
|
years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
|
|
|
|
yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
|
|
|
|
months = (64 * yearday) / 1959;
|
|
|
|
/* the result is based on a year starting on March.
|
|
|
|
* To convert take 12 from Januari and Februari and
|
|
|
|
* increase the year by one. */
|
|
|
|
if( months < 14 ) {
|
|
|
|
TimeFields->Month = months - 1;
|
|
|
|
TimeFields->Year = years + 1524;
|
|
|
|
} else {
|
|
|
|
TimeFields->Month = months - 13;
|
|
|
|
TimeFields->Year = years + 1525;
|
|
|
|
}
|
|
|
|
/* calculation of day of month is based on the wonderful
|
|
|
|
* sequence of INT( n * 30.6): it reproduces the
|
|
|
|
* 31-30-31-30-31-31 month lengths exactly for small n's */
|
|
|
|
TimeFields->Day = yearday - (1959 * months) / 64 ;
|
|
|
|
return;
|
1999-03-09 18:47:51 +01:00
|
|
|
}
|
2002-11-06 22:57:23 +01:00
|
|
|
|
1999-03-09 18:47:51 +01:00
|
|
|
/******************************************************************************
|
2002-11-25 02:12:39 +01:00
|
|
|
* RtlTimeFieldsToTime [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Convert a TIME_FIELDS structure into a time.
|
1999-03-09 18:47:51 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* PARAMS
|
2003-08-19 02:56:34 +02:00
|
|
|
* ftTimeFields [I] TIME_FIELDS structure to convert.
|
|
|
|
* Time [O] Destination for the converted time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Success: TRUE.
|
|
|
|
* Failure: FALSE.
|
1999-03-09 18:47:51 +01:00
|
|
|
*/
|
|
|
|
BOOLEAN WINAPI RtlTimeFieldsToTime(
|
|
|
|
PTIME_FIELDS tfTimeFields,
|
|
|
|
PLARGE_INTEGER Time)
|
|
|
|
{
|
2004-11-06 04:53:53 +01:00
|
|
|
int month, year, cleaps, day;
|
1999-03-09 18:47:51 +01:00
|
|
|
|
|
|
|
/* FIXME: normalize the TIME_FIELDS structure here */
|
2004-11-06 04:53:53 +01:00
|
|
|
/* No, native just returns 0 (error) if the fields are not */
|
|
|
|
if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
|
|
|
|
tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
|
|
|
|
tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
|
|
|
|
tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
|
|
|
|
tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
|
|
|
|
tfTimeFields->Day < 1 ||
|
|
|
|
tfTimeFields->Day > MonthLengths
|
|
|
|
[ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
|
|
|
|
[ tfTimeFields->Month - 1] ||
|
|
|
|
tfTimeFields->Year < 1601 )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* now calculate a day count from the date
|
|
|
|
* First start counting years from March. This way the leap days
|
|
|
|
* are added at the end of the year, not somewhere in the middle.
|
|
|
|
* Formula's become so much less complicate that way.
|
|
|
|
* To convert: add 12 to the month numbers of Jan and Feb, and
|
|
|
|
* take 1 from the year */
|
|
|
|
if(tfTimeFields->Month < 3) {
|
|
|
|
month = tfTimeFields->Month + 13;
|
|
|
|
year = tfTimeFields->Year - 1;
|
|
|
|
} else {
|
|
|
|
month = tfTimeFields->Month + 1;
|
|
|
|
year = tfTimeFields->Year;
|
|
|
|
}
|
|
|
|
cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
|
|
|
|
day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
|
|
|
|
(1959 * month) / 64 + /* months * daypermonth */
|
|
|
|
tfTimeFields->Day - /* day of the month */
|
|
|
|
584817 ; /* zero that on 1601-01-01 */
|
|
|
|
/* done */
|
|
|
|
|
|
|
|
Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
|
|
|
|
tfTimeFields->Hour) * MINSPERHOUR +
|
|
|
|
tfTimeFields->Minute) * SECSPERMIN +
|
|
|
|
tfTimeFields->Second ) * 1000 +
|
|
|
|
tfTimeFields->Milliseconds ) * TICKSPERMSEC;
|
|
|
|
|
|
|
|
return TRUE;
|
1999-03-09 18:47:51 +01:00
|
|
|
}
|
2002-11-25 02:12:39 +01:00
|
|
|
|
2004-02-26 06:26:34 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* TIME_GetBias [internal]
|
|
|
|
*
|
|
|
|
* Helper function calculates delta local time from UTC.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* utc [I] The current utc time.
|
|
|
|
* pdaylight [I] Local daylight.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* The bias for the current timezone.
|
|
|
|
*/
|
|
|
|
static int TIME_GetBias(time_t utc, int *pdaylight)
|
|
|
|
{
|
2004-02-27 01:43:20 +01:00
|
|
|
struct tm *ptm;
|
|
|
|
static time_t last_utc;
|
|
|
|
static int last_bias;
|
2004-03-01 22:23:18 +01:00
|
|
|
static int last_daylight;
|
2004-02-27 01:43:20 +01:00
|
|
|
int ret;
|
|
|
|
|
2007-08-07 13:01:16 +02:00
|
|
|
RtlEnterCriticalSection( &TIME_tz_section );
|
2007-07-29 14:33:23 +02:00
|
|
|
if (utc != last_utc)
|
2004-02-27 01:43:20 +01:00
|
|
|
{
|
|
|
|
ptm = localtime(&utc);
|
2007-07-29 14:33:23 +02:00
|
|
|
last_daylight = ptm->tm_isdst; /* daylight for local timezone */
|
2004-02-27 01:43:20 +01:00
|
|
|
ptm = gmtime(&utc);
|
2007-07-29 14:33:23 +02:00
|
|
|
ptm->tm_isdst = last_daylight; /* use local daylight, not that of Greenwich */
|
2004-02-27 01:43:20 +01:00
|
|
|
last_utc = utc;
|
2007-07-29 14:33:23 +02:00
|
|
|
last_bias = (int)(utc - mktime(ptm));
|
2004-02-27 01:43:20 +01:00
|
|
|
}
|
2007-07-29 14:33:23 +02:00
|
|
|
|
|
|
|
*pdaylight = last_daylight;
|
|
|
|
ret = last_bias;
|
|
|
|
|
2007-08-07 13:01:16 +02:00
|
|
|
RtlLeaveCriticalSection( &TIME_tz_section );
|
2004-02-27 01:43:20 +01:00
|
|
|
return ret;
|
2004-02-26 06:26:34 +01:00
|
|
|
}
|
|
|
|
|
2002-12-11 01:19:56 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* RtlLocalTimeToSystemTime [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Convert a local time into system time.
|
2002-12-11 01:19:56 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* PARAMS
|
2003-08-19 02:56:34 +02:00
|
|
|
* LocalTime [I] Local time to convert.
|
|
|
|
* SystemTime [O] Destination for the converted time.
|
2002-12-11 01:19:56 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Success: STATUS_SUCCESS.
|
|
|
|
* Failure: An NTSTATUS error code indicating the problem.
|
2002-12-11 01:19:56 +01:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
|
|
|
|
PLARGE_INTEGER SystemTime)
|
|
|
|
{
|
2004-02-26 06:26:34 +01:00
|
|
|
time_t gmt;
|
|
|
|
int bias, daylight;
|
2002-12-11 01:19:56 +01:00
|
|
|
|
|
|
|
TRACE("(%p, %p)\n", LocalTime, SystemTime);
|
|
|
|
|
2004-02-26 06:26:34 +01:00
|
|
|
gmt = time(NULL);
|
|
|
|
bias = TIME_GetBias(gmt, &daylight);
|
|
|
|
|
2005-02-08 13:13:36 +01:00
|
|
|
SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)TICKSPERSEC;
|
2002-12-11 01:19:56 +01:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
1999-03-09 18:47:51 +01:00
|
|
|
|
|
|
|
/******************************************************************************
|
2002-11-25 02:12:39 +01:00
|
|
|
* RtlSystemTimeToLocalTime [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Convert a system time into a local time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* PARAMS
|
|
|
|
* SystemTime [I] System time to convert.
|
2003-08-19 02:56:34 +02:00
|
|
|
* LocalTime [O] Destination for the converted time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Success: STATUS_SUCCESS.
|
|
|
|
* Failure: An NTSTATUS error code indicating the problem.
|
1999-03-09 18:47:51 +01:00
|
|
|
*/
|
2002-12-11 01:19:56 +01:00
|
|
|
NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
|
|
|
|
PLARGE_INTEGER LocalTime )
|
1999-03-09 18:47:51 +01:00
|
|
|
{
|
2004-02-26 06:26:34 +01:00
|
|
|
time_t gmt;
|
|
|
|
int bias, daylight;
|
2002-12-11 01:19:56 +01:00
|
|
|
|
|
|
|
TRACE("(%p, %p)\n", SystemTime, LocalTime);
|
1999-03-09 18:47:51 +01:00
|
|
|
|
2004-02-26 06:26:34 +01:00
|
|
|
gmt = time(NULL);
|
|
|
|
bias = TIME_GetBias(gmt, &daylight);
|
|
|
|
|
2005-02-08 13:13:36 +01:00
|
|
|
LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)TICKSPERSEC;
|
2002-12-11 01:19:56 +01:00
|
|
|
return STATUS_SUCCESS;
|
1999-03-09 18:47:51 +01:00
|
|
|
}
|
2000-07-29 23:56:59 +02:00
|
|
|
|
1999-03-09 18:47:51 +01:00
|
|
|
/******************************************************************************
|
2002-11-25 02:12:39 +01:00
|
|
|
* RtlTimeToSecondsSince1970 [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Convert a time into a count of seconds since 1970.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* PARAMS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Time [I] Time to convert.
|
|
|
|
* Seconds [O] Destination for the converted time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Success: TRUE.
|
|
|
|
* Failure: FALSE, if the resulting value will not fit in a DWORD.
|
1999-03-09 18:47:51 +01:00
|
|
|
*/
|
2003-08-19 02:56:34 +02:00
|
|
|
BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
|
1999-03-09 18:47:51 +01:00
|
|
|
{
|
2004-01-23 02:51:33 +01:00
|
|
|
ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
|
2005-02-08 13:13:36 +01:00
|
|
|
tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
|
2000-10-01 03:40:42 +02:00
|
|
|
tmp -= SECS_1601_TO_1970;
|
|
|
|
if (tmp > 0xffffffff) return FALSE;
|
2003-08-19 02:56:34 +02:00
|
|
|
*Seconds = (DWORD)tmp;
|
2000-07-29 23:56:59 +02:00
|
|
|
return TRUE;
|
1999-03-09 18:47:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2002-11-25 02:12:39 +01:00
|
|
|
* RtlTimeToSecondsSince1980 [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Convert a time into a count of seconds since 1980.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* PARAMS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Time [I] Time to convert.
|
|
|
|
* Seconds [O] Destination for the converted time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Success: TRUE.
|
|
|
|
* Failure: FALSE, if the resulting value will not fit in a DWORD.
|
1999-03-09 18:47:51 +01:00
|
|
|
*/
|
2003-08-19 02:56:34 +02:00
|
|
|
BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
|
1999-03-09 18:47:51 +01:00
|
|
|
{
|
2004-01-23 02:51:33 +01:00
|
|
|
ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
|
2005-02-08 13:13:36 +01:00
|
|
|
tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
|
2003-03-31 03:37:04 +02:00
|
|
|
tmp -= SECS_1601_TO_1980;
|
2000-10-01 03:40:42 +02:00
|
|
|
if (tmp > 0xffffffff) return FALSE;
|
2003-08-19 02:56:34 +02:00
|
|
|
*Seconds = (DWORD)tmp;
|
2000-07-29 23:56:59 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2002-11-25 02:12:39 +01:00
|
|
|
* RtlSecondsSince1970ToTime [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Convert a count of seconds since 1970 to a time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* PARAMS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Seconds [I] Time to convert.
|
|
|
|
* Time [O] Destination for the converted time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2002-11-25 02:12:39 +01:00
|
|
|
* Nothing.
|
2000-07-29 23:56:59 +02:00
|
|
|
*/
|
2003-08-19 02:56:34 +02:00
|
|
|
void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
|
2000-07-29 23:56:59 +02:00
|
|
|
{
|
2003-08-19 02:56:34 +02:00
|
|
|
ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
|
2004-01-23 02:51:33 +01:00
|
|
|
Time->u.LowPart = (DWORD)secs;
|
|
|
|
Time->u.HighPart = (DWORD)(secs >> 32);
|
2000-07-29 23:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2002-11-25 02:12:39 +01:00
|
|
|
* RtlSecondsSince1980ToTime [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Convert a count of seconds since 1980 to a time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* PARAMS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Seconds [I] Time to convert.
|
|
|
|
* Time [O] Destination for the converted time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2002-11-25 02:12:39 +01:00
|
|
|
* Nothing.
|
2000-07-29 23:56:59 +02:00
|
|
|
*/
|
2003-08-19 02:56:34 +02:00
|
|
|
void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
|
2000-07-29 23:56:59 +02:00
|
|
|
{
|
2003-08-19 02:56:34 +02:00
|
|
|
ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
|
2004-01-23 02:51:33 +01:00
|
|
|
Time->u.LowPart = (DWORD)secs;
|
|
|
|
Time->u.HighPart = (DWORD)(secs >> 32);
|
1999-03-09 18:47:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2002-11-25 02:12:39 +01:00
|
|
|
* RtlTimeToElapsedTimeFields [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Convert a time to a count of elapsed seconds.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* Time [I] Time to convert.
|
|
|
|
* TimeFields [O] Destination for the converted time.
|
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2002-11-25 02:12:39 +01:00
|
|
|
* Nothing.
|
1999-03-09 18:47:51 +01:00
|
|
|
*/
|
2002-12-18 03:31:33 +01:00
|
|
|
void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
|
1999-03-09 18:47:51 +01:00
|
|
|
{
|
2002-12-18 03:31:33 +01:00
|
|
|
LONGLONG time;
|
2005-09-02 16:47:36 +02:00
|
|
|
INT rem;
|
2002-12-18 03:31:33 +01:00
|
|
|
|
|
|
|
time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
|
|
|
|
TimeFields->Milliseconds = rem / TICKSPERMSEC;
|
|
|
|
|
|
|
|
/* time is now in seconds */
|
|
|
|
TimeFields->Year = 0;
|
|
|
|
TimeFields->Month = 0;
|
|
|
|
TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
|
|
|
|
|
|
|
|
/* rem is now the remaining seconds in the last day */
|
|
|
|
TimeFields->Second = rem % 60;
|
|
|
|
rem /= 60;
|
|
|
|
TimeFields->Minute = rem % 60;
|
|
|
|
TimeFields->Hour = rem / 60;
|
1999-03-09 18:47:51 +01:00
|
|
|
}
|
2000-10-01 03:40:42 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
2002-11-25 02:12:39 +01:00
|
|
|
* NtQuerySystemTime [NTDLL.@]
|
|
|
|
* ZwQuerySystemTime [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Get the current system time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* PARAMS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Time [O] Destination for the current system time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Success: STATUS_SUCCESS.
|
|
|
|
* Failure: An NTSTATUS error code indicating the problem.
|
2000-10-01 03:40:42 +02:00
|
|
|
*/
|
2003-08-19 02:56:34 +02:00
|
|
|
NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
|
2000-10-01 03:40:42 +02:00
|
|
|
{
|
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
gettimeofday( &now, 0 );
|
2003-08-19 02:56:34 +02:00
|
|
|
Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
|
|
|
|
Time->QuadPart += now.tv_usec * 10;
|
2002-09-13 00:07:02 +02:00
|
|
|
return STATUS_SUCCESS;
|
2000-10-01 03:40:42 +02:00
|
|
|
}
|
2002-11-25 02:12:39 +01:00
|
|
|
|
2005-07-14 12:32:46 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* NtQueryPerformanceCounter [NTDLL.@]
|
|
|
|
*
|
|
|
|
* Note: Windows uses a timer clocked at a multiple of 1193182 Hz. There is a
|
|
|
|
* good number of applications that crash when the returned frequency is either
|
2007-08-09 10:42:59 +02:00
|
|
|
* lower or higher than what Windows gives. Also too high counter values are
|
2005-07-14 12:32:46 +02:00
|
|
|
* reported to give problems.
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtQueryPerformanceCounter( PLARGE_INTEGER Counter, PLARGE_INTEGER Frequency )
|
|
|
|
{
|
2007-04-17 20:08:59 +02:00
|
|
|
LARGE_INTEGER now;
|
2005-07-14 12:32:46 +02:00
|
|
|
|
|
|
|
if (!Counter) return STATUS_ACCESS_VIOLATION;
|
2007-04-17 20:08:59 +02:00
|
|
|
|
|
|
|
/* convert a counter that increments at a rate of 10 MHz
|
2005-07-14 12:32:46 +02:00
|
|
|
* to one of 1.193182 MHz, with some care for arithmetic
|
2007-04-17 20:08:59 +02:00
|
|
|
* overflow and good accuracy (21/176 = 0.11931818) */
|
|
|
|
NtQuerySystemTime( &now );
|
|
|
|
Counter->QuadPart = ((now.QuadPart - server_start_time) * 21) / 176;
|
2005-07-14 12:32:46 +02:00
|
|
|
if (Frequency) Frequency->QuadPart = 1193182;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-19 21:21:52 +02:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* NtGetTickCount (NTDLL.@)
|
|
|
|
* ZwGetTickCount (NTDLL.@)
|
|
|
|
*/
|
|
|
|
ULONG WINAPI NtGetTickCount(void)
|
|
|
|
{
|
2007-04-17 20:08:59 +02:00
|
|
|
LARGE_INTEGER now;
|
2006-04-19 21:21:52 +02:00
|
|
|
|
2007-04-17 20:08:59 +02:00
|
|
|
NtQuerySystemTime( &now );
|
|
|
|
return (now.QuadPart - server_start_time) / 10000;
|
2006-04-19 21:21:52 +02:00
|
|
|
}
|
|
|
|
|
2007-08-07 13:01:16 +02:00
|
|
|
/* calculate the mday of dst change date, so that for instance Sun 5 Oct 2007
|
|
|
|
* (last Sunday in October of 2007) becomes Sun Oct 28 2007
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2007-08-07 13:01:16 +02:00
|
|
|
* Note: year, day and month must be in unix format.
|
2002-11-25 02:12:39 +01:00
|
|
|
*/
|
2007-08-07 13:01:16 +02:00
|
|
|
static int weekday_to_mday(int year, int day, int mon, int day_of_week)
|
|
|
|
{
|
|
|
|
struct tm date;
|
|
|
|
time_t tmp;
|
|
|
|
int wday, mday;
|
|
|
|
|
|
|
|
/* find first day in the month matching week day of the date */
|
|
|
|
memset(&date, 0, sizeof(date));
|
|
|
|
date.tm_year = year;
|
|
|
|
date.tm_mon = mon;
|
|
|
|
date.tm_mday = -1;
|
|
|
|
date.tm_wday = -1;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
date.tm_mday++;
|
|
|
|
tmp = mktime(&date);
|
|
|
|
} while (date.tm_wday != day_of_week || date.tm_mon != mon);
|
|
|
|
|
|
|
|
mday = date.tm_mday;
|
|
|
|
|
|
|
|
/* find number of week days in the month matching week day of the date */
|
|
|
|
wday = 1; /* 1 - 1st, ...., 5 - last */
|
|
|
|
while (wday < day)
|
|
|
|
{
|
|
|
|
struct tm *tm;
|
|
|
|
|
|
|
|
date.tm_mday += 7;
|
|
|
|
tmp = mktime(&date);
|
|
|
|
tm = localtime(&tmp);
|
|
|
|
if (tm->tm_mon != mon)
|
|
|
|
break;
|
|
|
|
mday = tm->tm_mday;
|
|
|
|
wday++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mday;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL match_tz_date(const RTL_SYSTEM_TIME *st, const RTL_SYSTEM_TIME *reg_st)
|
|
|
|
{
|
|
|
|
WORD wDay;
|
|
|
|
|
|
|
|
if (st->wMonth != reg_st->wMonth) return FALSE;
|
|
|
|
|
|
|
|
if (!st->wMonth) return TRUE; /* no transition dates */
|
|
|
|
|
|
|
|
wDay = reg_st->wDay;
|
|
|
|
if (!reg_st->wYear) /* date in a day-of-week format */
|
|
|
|
wDay = weekday_to_mday(st->wYear - 1900, reg_st->wDay, reg_st->wMonth - 1, reg_st->wDayOfWeek);
|
|
|
|
|
|
|
|
if (st->wDay != wDay ||
|
|
|
|
st->wHour != reg_st->wHour ||
|
|
|
|
st->wMinute != reg_st->wMinute ||
|
|
|
|
st->wSecond != reg_st->wSecond ||
|
|
|
|
st->wMilliseconds != reg_st->wMilliseconds) return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL match_tz_info(const RTL_TIME_ZONE_INFORMATION *tzi, const RTL_TIME_ZONE_INFORMATION *reg_tzi)
|
2002-11-25 02:12:39 +01:00
|
|
|
{
|
2007-08-07 13:01:16 +02:00
|
|
|
if (tzi->Bias == reg_tzi->Bias &&
|
|
|
|
match_tz_date(&tzi->StandardDate, ®_tzi->StandardDate) &&
|
|
|
|
match_tz_date(&tzi->DaylightDate, ®_tzi->DaylightDate))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
2002-11-25 02:12:39 +01:00
|
|
|
}
|
|
|
|
|
2007-08-07 13:01:16 +02:00
|
|
|
static BOOL reg_query_value(HKEY hkey, LPCWSTR name, DWORD type, void *data, DWORD count)
|
|
|
|
{
|
|
|
|
UNICODE_STRING nameW;
|
|
|
|
char buf[256];
|
|
|
|
KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buf;
|
|
|
|
|
|
|
|
if (count > sizeof(buf) - sizeof(KEY_VALUE_PARTIAL_INFORMATION))
|
|
|
|
return FALSE;
|
2004-10-18 23:19:28 +02:00
|
|
|
|
2007-08-07 13:01:16 +02:00
|
|
|
RtlInitUnicodeString(&nameW, name);
|
|
|
|
|
|
|
|
if (NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation,
|
|
|
|
buf, sizeof(buf), &count))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (info->Type != type) return FALSE;
|
|
|
|
|
|
|
|
memcpy(data, info->Data, info->DataLength);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2004-10-18 23:19:28 +02:00
|
|
|
|
2007-08-07 13:01:16 +02:00
|
|
|
static void find_reg_tz_info(RTL_TIME_ZONE_INFORMATION *tzi)
|
2004-10-18 23:19:28 +02:00
|
|
|
{
|
2007-08-07 13:01:16 +02:00
|
|
|
static const WCHAR Time_ZonesW[] = { 'M','a','c','h','i','n','e','\\',
|
|
|
|
'S','o','f','t','w','a','r','e','\\',
|
|
|
|
'M','i','c','r','o','s','o','f','t','\\',
|
|
|
|
'W','i','n','d','o','w','s',' ','N','T','\\',
|
|
|
|
'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
|
|
|
|
'T','i','m','e',' ','Z','o','n','e','s',0 };
|
2005-06-17 15:58:33 +02:00
|
|
|
HANDLE hkey;
|
2007-08-07 13:01:16 +02:00
|
|
|
ULONG idx;
|
2004-10-18 23:19:28 +02:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
UNICODE_STRING nameW;
|
2007-08-07 13:01:16 +02:00
|
|
|
WCHAR buf[128];
|
2004-10-18 23:19:28 +02:00
|
|
|
|
|
|
|
attr.Length = sizeof(attr);
|
|
|
|
attr.RootDirectory = 0;
|
|
|
|
attr.ObjectName = &nameW;
|
|
|
|
attr.Attributes = 0;
|
|
|
|
attr.SecurityDescriptor = NULL;
|
|
|
|
attr.SecurityQualityOfService = NULL;
|
2007-08-07 13:01:16 +02:00
|
|
|
RtlInitUnicodeString(&nameW, Time_ZonesW);
|
|
|
|
if (NtOpenKey(&hkey, KEY_READ, &attr))
|
|
|
|
{
|
|
|
|
WARN("Unable to open the time zones key\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = 0;
|
|
|
|
nameW.Buffer = buf;
|
|
|
|
nameW.Length = sizeof(buf);
|
|
|
|
nameW.MaximumLength = sizeof(buf);
|
|
|
|
|
|
|
|
while (!RtlpNtEnumerateSubKey(hkey, &nameW, idx++))
|
|
|
|
{
|
|
|
|
static const WCHAR stdW[] = { 'S','t','d',0 };
|
|
|
|
static const WCHAR dltW[] = { 'D','l','t',0 };
|
|
|
|
static const WCHAR tziW[] = { 'T','Z','I',0 };
|
|
|
|
RTL_TIME_ZONE_INFORMATION reg_tzi;
|
|
|
|
HANDLE hSubkey;
|
|
|
|
struct tz_reg_data
|
|
|
|
{
|
|
|
|
LONG bias;
|
|
|
|
LONG std_bias;
|
|
|
|
LONG dlt_bias;
|
|
|
|
RTL_SYSTEM_TIME std_date;
|
|
|
|
RTL_SYSTEM_TIME dlt_date;
|
|
|
|
} tz_data;
|
|
|
|
|
|
|
|
attr.Length = sizeof(attr);
|
|
|
|
attr.RootDirectory = hkey;
|
|
|
|
attr.ObjectName = &nameW;
|
|
|
|
attr.Attributes = 0;
|
|
|
|
attr.SecurityDescriptor = NULL;
|
|
|
|
attr.SecurityQualityOfService = NULL;
|
|
|
|
if (NtOpenKey(&hSubkey, KEY_READ, &attr))
|
|
|
|
{
|
|
|
|
WARN("Unable to open subkey %s\n", debugstr_wn(nameW.Buffer, nameW.Length/sizeof(WCHAR)));
|
|
|
|
continue;
|
2004-10-18 23:19:28 +02:00
|
|
|
}
|
2007-08-07 13:01:16 +02:00
|
|
|
|
|
|
|
#define get_value(hkey, name, type, data, len) \
|
|
|
|
if (!reg_query_value(hkey, name, type, data, len)) \
|
|
|
|
{ \
|
|
|
|
WARN("can't read data from %s\n", debugstr_w(name)); \
|
|
|
|
NtClose(hkey); \
|
|
|
|
continue; \
|
|
|
|
}
|
|
|
|
|
|
|
|
get_value(hSubkey, stdW, REG_SZ, reg_tzi.StandardName, sizeof(reg_tzi.StandardName));
|
|
|
|
get_value(hSubkey, dltW, REG_SZ, reg_tzi.DaylightName, sizeof(reg_tzi.DaylightName));
|
|
|
|
get_value(hSubkey, tziW, REG_BINARY, &tz_data, sizeof(tz_data));
|
|
|
|
|
|
|
|
#undef get_value
|
|
|
|
|
|
|
|
reg_tzi.Bias = tz_data.bias;
|
|
|
|
reg_tzi.StandardBias = tz_data.std_bias;
|
|
|
|
reg_tzi.DaylightBias = tz_data.dlt_bias;
|
|
|
|
reg_tzi.StandardDate = tz_data.std_date;
|
|
|
|
reg_tzi.DaylightDate = tz_data.dlt_date;
|
|
|
|
|
|
|
|
TRACE("%s: bias %d\n", debugstr_wn(nameW.Buffer, nameW.Length/sizeof(WCHAR)), reg_tzi.Bias);
|
|
|
|
TRACE("std (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
|
|
|
|
reg_tzi.StandardDate.wDay, reg_tzi.StandardDate.wMonth,
|
|
|
|
reg_tzi.StandardDate.wYear, reg_tzi.StandardDate.wDayOfWeek,
|
|
|
|
reg_tzi.StandardDate.wHour, reg_tzi.StandardDate.wMinute,
|
|
|
|
reg_tzi.StandardDate.wSecond, reg_tzi.StandardDate.wMilliseconds,
|
|
|
|
reg_tzi.StandardBias);
|
|
|
|
TRACE("dst (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
|
|
|
|
reg_tzi.DaylightDate.wDay, reg_tzi.DaylightDate.wMonth,
|
|
|
|
reg_tzi.DaylightDate.wYear, reg_tzi.DaylightDate.wDayOfWeek,
|
|
|
|
reg_tzi.DaylightDate.wHour, reg_tzi.DaylightDate.wMinute,
|
|
|
|
reg_tzi.DaylightDate.wSecond, reg_tzi.DaylightDate.wMilliseconds,
|
|
|
|
reg_tzi.DaylightBias);
|
|
|
|
|
|
|
|
NtClose(hSubkey);
|
|
|
|
|
|
|
|
if (match_tz_info(tzi, ®_tzi))
|
|
|
|
{
|
|
|
|
memcpy(tzi, ®_tzi, sizeof(*tzi));
|
|
|
|
NtClose(hkey);
|
|
|
|
return;
|
2004-10-18 23:19:28 +02:00
|
|
|
}
|
2005-04-21 19:18:50 +02:00
|
|
|
|
2007-08-07 13:01:16 +02:00
|
|
|
/* reset len */
|
|
|
|
nameW.Length = sizeof(buf);
|
|
|
|
nameW.MaximumLength = sizeof(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
NtClose(hkey);
|
|
|
|
|
|
|
|
FIXME("Can't find matching timezone information in the registry for "
|
|
|
|
"bias %d, std (d/m/y): %u/%02u/%04u, dlt (d/m/y): %u/%02u/%04u\n",
|
|
|
|
tzi->Bias,
|
|
|
|
tzi->StandardDate.wDay, tzi->StandardDate.wMonth, tzi->StandardDate.wYear,
|
|
|
|
tzi->DaylightDate.wDay, tzi->DaylightDate.wMonth, tzi->DaylightDate.wYear);
|
|
|
|
}
|
|
|
|
|
|
|
|
static time_t find_dst_change(unsigned long min, unsigned long max, int *is_dst)
|
|
|
|
{
|
|
|
|
time_t start;
|
|
|
|
struct tm *tm;
|
|
|
|
|
|
|
|
start = min;
|
|
|
|
tm = localtime(&start);
|
|
|
|
*is_dst = !tm->tm_isdst;
|
|
|
|
TRACE("starting date isdst %d, %s", !*is_dst, ctime(&start));
|
|
|
|
|
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
time_t pos = (min + max) / 2;
|
|
|
|
tm = localtime(&pos);
|
|
|
|
|
|
|
|
if (tm->tm_isdst != *is_dst)
|
|
|
|
min = pos + 1;
|
|
|
|
else
|
|
|
|
max = pos - 1;
|
|
|
|
}
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_tz_info(RTL_TIME_ZONE_INFORMATION *tzi, int *valid_year)
|
|
|
|
{
|
|
|
|
struct tm *tm;
|
|
|
|
time_t year_start, year_end, tmp, dlt = 0, std = 0;
|
|
|
|
int is_dst;
|
|
|
|
|
|
|
|
year_start = time(NULL);
|
|
|
|
tm = localtime(&year_start);
|
|
|
|
|
|
|
|
if (*valid_year == tm->tm_year) return;
|
|
|
|
|
|
|
|
memset(tzi, 0, sizeof(*tzi));
|
|
|
|
|
|
|
|
TRACE("tz data will be valid through year %d\n", tm->tm_year + 1900);
|
|
|
|
*valid_year = tm->tm_year;
|
|
|
|
|
|
|
|
tm->tm_isdst = 0;
|
|
|
|
tm->tm_mday = 1;
|
|
|
|
tm->tm_mon = tm->tm_hour = tm->tm_min = tm->tm_sec = tm->tm_wday = tm->tm_yday = 0;
|
|
|
|
year_start = mktime(tm);
|
|
|
|
TRACE("year_start: %s", ctime(&year_start));
|
|
|
|
|
|
|
|
tm->tm_mday = tm->tm_wday = tm->tm_yday = 0;
|
|
|
|
tm->tm_mon = 12;
|
|
|
|
tm->tm_hour = 23;
|
|
|
|
tm->tm_min = tm->tm_sec = 59;
|
|
|
|
year_end = mktime(tm);
|
|
|
|
TRACE("year_end: %s", ctime(&year_end));
|
|
|
|
|
|
|
|
tm = gmtime(&year_start);
|
|
|
|
tzi->Bias = (LONG)(mktime(tm) - year_start) / 60;
|
|
|
|
TRACE("bias: %d\n", tzi->Bias);
|
|
|
|
|
|
|
|
tmp = find_dst_change(year_start, year_end, &is_dst);
|
|
|
|
if (is_dst)
|
|
|
|
dlt = tmp;
|
|
|
|
else
|
|
|
|
std = tmp;
|
|
|
|
|
|
|
|
tmp = find_dst_change(tmp, year_end, &is_dst);
|
|
|
|
if (is_dst)
|
|
|
|
dlt = tmp;
|
|
|
|
else
|
|
|
|
std = tmp;
|
|
|
|
|
|
|
|
TRACE("std: %s", ctime(&std));
|
|
|
|
TRACE("dlt: %s", ctime(&dlt));
|
|
|
|
|
|
|
|
if (dlt == std || !dlt || !std)
|
|
|
|
{
|
|
|
|
TRACE("there is no daylight saving rules in this time zone\n");
|
|
|
|
return;
|
2004-10-18 23:19:28 +02:00
|
|
|
}
|
2007-08-07 13:01:16 +02:00
|
|
|
|
|
|
|
tmp = dlt - tzi->Bias * 60;
|
|
|
|
tm = gmtime(&tmp);
|
|
|
|
TRACE("dlt gmtime: %s", asctime(tm));
|
|
|
|
|
|
|
|
tzi->DaylightBias = -60;
|
|
|
|
tzi->DaylightDate.wYear = tm->tm_year + 1900;
|
|
|
|
tzi->DaylightDate.wMonth = tm->tm_mon + 1;
|
|
|
|
tzi->DaylightDate.wDayOfWeek = tm->tm_wday;
|
|
|
|
tzi->DaylightDate.wDay = tm->tm_mday;
|
|
|
|
tzi->DaylightDate.wHour = tm->tm_hour;
|
|
|
|
tzi->DaylightDate.wMinute = tm->tm_min;
|
|
|
|
tzi->DaylightDate.wSecond = tm->tm_sec;
|
|
|
|
tzi->DaylightDate.wMilliseconds = 0;
|
|
|
|
|
|
|
|
TRACE("daylight (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
|
|
|
|
tzi->DaylightDate.wDay, tzi->DaylightDate.wMonth,
|
|
|
|
tzi->DaylightDate.wYear, tzi->DaylightDate.wDayOfWeek,
|
|
|
|
tzi->DaylightDate.wHour, tzi->DaylightDate.wMinute,
|
|
|
|
tzi->DaylightDate.wSecond, tzi->DaylightDate.wMilliseconds,
|
|
|
|
tzi->DaylightBias);
|
|
|
|
|
|
|
|
tmp = std - tzi->Bias * 60 - tzi->DaylightBias * 60;
|
|
|
|
tm = gmtime(&tmp);
|
|
|
|
TRACE("std gmtime: %s", asctime(tm));
|
|
|
|
|
|
|
|
tzi->StandardBias = 0;
|
|
|
|
tzi->StandardDate.wYear = tm->tm_year + 1900;
|
|
|
|
tzi->StandardDate.wMonth = tm->tm_mon + 1;
|
|
|
|
tzi->StandardDate.wDayOfWeek = tm->tm_wday;
|
|
|
|
tzi->StandardDate.wDay = tm->tm_mday;
|
|
|
|
tzi->StandardDate.wHour = tm->tm_hour;
|
|
|
|
tzi->StandardDate.wMinute = tm->tm_min;
|
|
|
|
tzi->StandardDate.wSecond = tm->tm_sec;
|
|
|
|
tzi->StandardDate.wMilliseconds = 0;
|
|
|
|
|
|
|
|
TRACE("standard (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
|
|
|
|
tzi->StandardDate.wDay, tzi->StandardDate.wMonth,
|
|
|
|
tzi->StandardDate.wYear, tzi->StandardDate.wDayOfWeek,
|
|
|
|
tzi->StandardDate.wHour, tzi->StandardDate.wMinute,
|
|
|
|
tzi->StandardDate.wSecond, tzi->StandardDate.wMilliseconds,
|
|
|
|
tzi->StandardBias);
|
|
|
|
|
|
|
|
find_reg_tz_info(tzi);
|
2004-10-18 23:19:28 +02:00
|
|
|
}
|
|
|
|
|
2002-11-25 02:12:39 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* RtlQueryTimeZoneInformation [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Get information about the current timezone.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* PARAMS
|
2003-08-19 02:56:34 +02:00
|
|
|
* tzinfo [O] Destination for the retrieved timezone info.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Success: STATUS_SUCCESS.
|
|
|
|
* Failure: An NTSTATUS error code indicating the problem.
|
2002-11-25 02:12:39 +01:00
|
|
|
*/
|
2005-06-20 20:37:17 +02:00
|
|
|
NTSTATUS WINAPI RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION *tzinfo)
|
2002-11-25 02:12:39 +01:00
|
|
|
{
|
2007-08-07 13:01:16 +02:00
|
|
|
static RTL_TIME_ZONE_INFORMATION *cached_tzi;
|
|
|
|
static int current_year = -1;
|
2002-11-25 02:12:39 +01:00
|
|
|
|
2007-08-07 13:01:16 +02:00
|
|
|
RtlEnterCriticalSection(&TIME_tz_section);
|
|
|
|
|
|
|
|
if (!cached_tzi)
|
|
|
|
cached_tzi = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*cached_tzi));
|
|
|
|
|
|
|
|
init_tz_info(cached_tzi, ¤t_year);
|
|
|
|
*tzinfo = *cached_tzi;
|
|
|
|
|
|
|
|
RtlLeaveCriticalSection(&TIME_tz_section);
|
2002-11-25 02:12:39 +01:00
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlSetTimeZoneInformation [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Set the current time zone information.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* PARAMS
|
2003-08-19 02:56:34 +02:00
|
|
|
* tzinfo [I] Timezone information to set.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Success: STATUS_SUCCESS.
|
|
|
|
* Failure: An NTSTATUS error code indicating the problem.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
|
|
|
*/
|
2005-06-20 20:37:17 +02:00
|
|
|
NTSTATUS WINAPI RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION *tzinfo )
|
2002-11-25 02:12:39 +01:00
|
|
|
{
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* NtSetSystemTime [NTDLL.@]
|
|
|
|
* ZwSetSystemTime [NTDLL.@]
|
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* Set the system time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-08-19 02:56:34 +02:00
|
|
|
* PARAMS
|
|
|
|
* NewTime [I] The time to set.
|
|
|
|
* OldTime [O] Optional destination for the previous system time.
|
2002-11-25 02:12:39 +01:00
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* RETURNS
|
2003-08-19 02:56:34 +02:00
|
|
|
* Success: STATUS_SUCCESS.
|
|
|
|
* Failure: An NTSTATUS error code indicating the problem.
|
2002-11-25 02:12:39 +01:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
|
|
|
|
{
|
|
|
|
TIME_FIELDS tf;
|
|
|
|
struct timeval tv;
|
|
|
|
struct timezone tz;
|
|
|
|
struct tm t;
|
|
|
|
time_t sec, oldsec;
|
|
|
|
int dst, bias;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* Return the old time if necessary */
|
|
|
|
if(OldTime)
|
|
|
|
NtQuerySystemTime(OldTime);
|
|
|
|
|
|
|
|
RtlTimeToTimeFields(NewTime, &tf);
|
|
|
|
|
|
|
|
/* call gettimeofday to get the current timezone */
|
|
|
|
gettimeofday(&tv, &tz);
|
|
|
|
oldsec = tv.tv_sec;
|
|
|
|
/* get delta local time from utc */
|
|
|
|
bias = TIME_GetBias(oldsec, &dst);
|
|
|
|
|
|
|
|
/* get the number of seconds */
|
|
|
|
t.tm_sec = tf.Second;
|
|
|
|
t.tm_min = tf.Minute;
|
|
|
|
t.tm_hour = tf.Hour;
|
|
|
|
t.tm_mday = tf.Day;
|
|
|
|
t.tm_mon = tf.Month - 1;
|
|
|
|
t.tm_year = tf.Year - 1900;
|
|
|
|
t.tm_isdst = dst;
|
|
|
|
sec = mktime (&t);
|
|
|
|
/* correct for timezone and daylight */
|
|
|
|
sec += bias;
|
|
|
|
|
|
|
|
/* set the new time */
|
|
|
|
tv.tv_sec = sec;
|
|
|
|
tv.tv_usec = tf.Milliseconds * 1000;
|
|
|
|
|
|
|
|
/* error and sanity check*/
|
|
|
|
if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
|
|
|
|
err = 2;
|
|
|
|
} else {
|
|
|
|
#ifdef HAVE_SETTIMEOFDAY
|
|
|
|
err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
|
|
|
|
if(err == 0)
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
#else
|
|
|
|
err = 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
|
|
|
|
tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
|
2003-10-02 06:29:30 +02:00
|
|
|
(long)(sec-oldsec),
|
|
|
|
err == -1 ? "No Permission"
|
|
|
|
: sec == (time_t)-1 ? "" : "is too large." );
|
2002-11-25 02:12:39 +01:00
|
|
|
|
|
|
|
if(err == 2)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
else if(err == -1)
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
|
|
|
else
|
|
|
|
return STATUS_NOT_IMPLEMENTED;
|
|
|
|
}
|