blob: d233f236772ac80035b1048ac455089e91b02de5 [file] [log] [blame]
Juergen Schmied026d9db1999-03-09 17:47:51 +00001/*
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +00002 * Nt time functions.
Juergen Schmied026d9db1999-03-09 17:47:51 +00003 *
Vincent Béron9a624912002-05-31 23:06:46 +00004 * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
Jon Griffiths08922852003-08-19 00:56:34 +00005 * adapted to wine with special permissions of the author. This code is
6 * Copyright 2002 Rex Jolliff (rex@lvcablemodem.com)
Vincent Béron9a624912002-05-31 23:06:46 +00007 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00008 * Copyright 1999 Juergen Schmied
Dmitry Timoshkovd6661432007-08-07 20:01:16 +09009 * Copyright 2007 Dmitry Timoshkov
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000010 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
Jonathan Ernst360a3f92006-05-18 14:49:52 +020023 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Juergen Schmied026d9db1999-03-09 17:47:51 +000024 */
25
Patrik Stridvalld016f812002-08-17 00:43:16 +000026#include "config.h"
Patrik Stridvall51e6c0c2002-08-31 19:04:14 +000027#include "wine/port.h"
Patrik Stridvalld016f812002-08-17 00:43:16 +000028
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000029#include <stdarg.h>
Vincent Béronea966aa2002-11-27 20:14:45 +000030#include <stdlib.h>
Dmitry Timoshkova1886622007-07-29 21:31:00 +090031#include <errno.h>
David Luyer26cd7a11999-03-25 15:57:35 +000032#include <string.h>
Alexandre Julliard25b23a02004-09-07 23:01:34 +000033#include <limits.h>
Alexandre Julliardd76f9f92000-10-01 01:40:42 +000034#include <time.h>
Patrik Stridvalld016f812002-08-17 00:43:16 +000035#ifdef HAVE_SYS_TIME_H
36# include <sys/time.h>
37#endif
38#ifdef HAVE_UNISTD_H
39# include <unistd.h>
40#endif
Dimitrie O. Paun297f3d82003-01-07 20:36:20 +000041
42#define NONAMELESSUNION
43#define NONAMELESSSTRUCT
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010044#include "ntstatus.h"
45#define WIN32_NO_STATUS
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000046#include "windef.h"
Patrik Stridvall9c1de6d2002-09-12 22:07:02 +000047#include "winternl.h"
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +000048#include "wine/unicode.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000049#include "wine/debug.h"
Alexandre Julliard462172a2003-04-02 22:48:59 +000050#include "ntdll_misc.h"
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000051
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000052WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
Juergen Schmied026d9db1999-03-09 17:47:51 +000053
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +090054static int init_tz_info(RTL_TIME_ZONE_INFORMATION *tzi);
55
Dmitry Timoshkovd6661432007-08-07 20:01:16 +090056static RTL_CRITICAL_SECTION TIME_tz_section;
Mike McCormack6b636e32005-06-25 18:00:57 +000057static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
Huw Davies67e2d6c2004-02-27 00:43:20 +000058{
Dmitry Timoshkovd6661432007-08-07 20:01:16 +090059 0, 0, &TIME_tz_section,
Huw Davies67e2d6c2004-02-27 00:43:20 +000060 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
Dmitry Timoshkovd6661432007-08-07 20:01:16 +090061 0, 0, { (DWORD_PTR)(__FILE__ ": TIME_tz_section") }
Huw Davies67e2d6c2004-02-27 00:43:20 +000062};
Dmitry Timoshkovd6661432007-08-07 20:01:16 +090063static RTL_CRITICAL_SECTION TIME_tz_section = { &critsect_debug, -1, 0, 0, 0, 0 };
Huw Davies67e2d6c2004-02-27 00:43:20 +000064
Juergen Schmied026d9db1999-03-09 17:47:51 +000065#define TICKSPERSEC 10000000
66#define TICKSPERMSEC 10000
67#define SECSPERDAY 86400
68#define SECSPERHOUR 3600
69#define SECSPERMIN 60
70#define MINSPERHOUR 60
71#define HOURSPERDAY 24
Alexandre Julliarddcc3afd2002-12-13 20:53:04 +000072#define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
Juergen Schmied026d9db1999-03-09 17:47:51 +000073#define DAYSPERWEEK 7
74#define EPOCHYEAR 1601
75#define DAYSPERNORMALYEAR 365
76#define DAYSPERLEAPYEAR 366
77#define MONSPERYEAR 12
Huw Davies166faa42004-02-24 01:01:27 +000078#define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
79#define DAYSPERNORMALCENTURY (365 * 100 + 24)
80#define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
Juergen Schmied026d9db1999-03-09 17:47:51 +000081
Alexandre Julliardd76f9f92000-10-01 01:40:42 +000082/* 1601 to 1970 is 369 years plus 89 leap days */
Alexandre Julliardfbef57c2003-03-31 01:37:04 +000083#define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
84#define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
Alexandre Julliardd76f9f92000-10-01 01:40:42 +000085/* 1601 to 1980 is 379 years plus 91 leap days */
Alexandre Julliardfbef57c2003-03-31 01:37:04 +000086#define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
87#define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
Alexandre Julliard25b23a02004-09-07 23:01:34 +000088/* max ticks that can be represented as Unix time */
89#define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
Alexandre Julliardd76f9f92000-10-01 01:40:42 +000090
91
Juergen Schmied026d9db1999-03-09 17:47:51 +000092static const int MonthLengths[2][MONSPERYEAR] =
93{
94 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
95 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
96};
97
Patrik Stridvall896889f1999-05-08 12:50:36 +000098static inline int IsLeapYear(int Year)
Juergen Schmied026d9db1999-03-09 17:47:51 +000099{
100 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
101}
102
Juergen Schmied026d9db1999-03-09 17:47:51 +0000103/******************************************************************************
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000104 * RtlTimeToTimeFields [NTDLL.@]
Juergen Schmied026d9db1999-03-09 17:47:51 +0000105 *
Jon Griffiths08922852003-08-19 00:56:34 +0000106 * Convert a time into a TIME_FIELDS structure.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000107 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000108 * PARAMS
Jon Griffiths08922852003-08-19 00:56:34 +0000109 * liTime [I] Time to convert.
110 * TimeFields [O] Destination for the converted time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000111 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000112 * RETURNS
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000113 * Nothing.
Juergen Schmied026d9db1999-03-09 17:47:51 +0000114 */
Juergen Schmied026d9db1999-03-09 17:47:51 +0000115VOID WINAPI RtlTimeToTimeFields(
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000116 const LARGE_INTEGER *liTime,
Juergen Schmied026d9db1999-03-09 17:47:51 +0000117 PTIME_FIELDS TimeFields)
118{
Rein Klazesdceae022004-11-06 03:53:53 +0000119 int SecondsInDay;
120 long int cleaps, years, yearday, months;
Juergen Schmied026d9db1999-03-09 17:47:51 +0000121 long int Days;
Rein Klazesdceae022004-11-06 03:53:53 +0000122 LONGLONG Time;
Juergen Schmied026d9db1999-03-09 17:47:51 +0000123
124 /* Extract millisecond from time and convert time into seconds */
Rein Klazesdceae022004-11-06 03:53:53 +0000125 TimeFields->Milliseconds =
126 (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
127 Time = liTime->QuadPart / TICKSPERSEC;
Juergen Schmied026d9db1999-03-09 17:47:51 +0000128
György 'Nog' Jeney39433b92002-11-06 21:57:23 +0000129 /* The native version of RtlTimeToTimeFields does not take leap seconds
130 * into account */
Juergen Schmied026d9db1999-03-09 17:47:51 +0000131
132 /* Split the time into days and seconds within the day */
133 Days = Time / SECSPERDAY;
134 SecondsInDay = Time % SECSPERDAY;
135
Juergen Schmied026d9db1999-03-09 17:47:51 +0000136 /* compute time of day */
137 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
138 SecondsInDay = SecondsInDay % SECSPERHOUR;
139 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
140 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
141
Juergen Schmied026d9db1999-03-09 17:47:51 +0000142 /* compute day of week */
143 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
144
Rein Klazesdceae022004-11-06 03:53:53 +0000145 /* compute year, month and day of month. */
146 cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
147 Days += 28188 + cleaps;
148 years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
149 yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
150 months = (64 * yearday) / 1959;
151 /* the result is based on a year starting on March.
152 * To convert take 12 from Januari and Februari and
153 * increase the year by one. */
154 if( months < 14 ) {
155 TimeFields->Month = months - 1;
156 TimeFields->Year = years + 1524;
157 } else {
158 TimeFields->Month = months - 13;
159 TimeFields->Year = years + 1525;
160 }
161 /* calculation of day of month is based on the wonderful
162 * sequence of INT( n * 30.6): it reproduces the
163 * 31-30-31-30-31-31 month lengths exactly for small n's */
164 TimeFields->Day = yearday - (1959 * months) / 64 ;
165 return;
Juergen Schmied026d9db1999-03-09 17:47:51 +0000166}
György 'Nog' Jeney39433b92002-11-06 21:57:23 +0000167
Juergen Schmied026d9db1999-03-09 17:47:51 +0000168/******************************************************************************
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000169 * RtlTimeFieldsToTime [NTDLL.@]
Juergen Schmied026d9db1999-03-09 17:47:51 +0000170 *
Jon Griffiths08922852003-08-19 00:56:34 +0000171 * Convert a TIME_FIELDS structure into a time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000172 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000173 * PARAMS
Jon Griffiths08922852003-08-19 00:56:34 +0000174 * ftTimeFields [I] TIME_FIELDS structure to convert.
175 * Time [O] Destination for the converted time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000176 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000177 * RETURNS
Jon Griffiths08922852003-08-19 00:56:34 +0000178 * Success: TRUE.
179 * Failure: FALSE.
Juergen Schmied026d9db1999-03-09 17:47:51 +0000180 */
181BOOLEAN WINAPI RtlTimeFieldsToTime(
182 PTIME_FIELDS tfTimeFields,
183 PLARGE_INTEGER Time)
184{
Rein Klazesdceae022004-11-06 03:53:53 +0000185 int month, year, cleaps, day;
Juergen Schmied026d9db1999-03-09 17:47:51 +0000186
187 /* FIXME: normalize the TIME_FIELDS structure here */
Rein Klazesdceae022004-11-06 03:53:53 +0000188 /* No, native just returns 0 (error) if the fields are not */
189 if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
190 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
191 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
192 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
193 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
194 tfTimeFields->Day < 1 ||
195 tfTimeFields->Day > MonthLengths
196 [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
197 [ tfTimeFields->Month - 1] ||
198 tfTimeFields->Year < 1601 )
199 return FALSE;
Juergen Schmied026d9db1999-03-09 17:47:51 +0000200
Rein Klazesdceae022004-11-06 03:53:53 +0000201 /* now calculate a day count from the date
202 * First start counting years from March. This way the leap days
203 * are added at the end of the year, not somewhere in the middle.
204 * Formula's become so much less complicate that way.
205 * To convert: add 12 to the month numbers of Jan and Feb, and
206 * take 1 from the year */
207 if(tfTimeFields->Month < 3) {
208 month = tfTimeFields->Month + 13;
209 year = tfTimeFields->Year - 1;
210 } else {
211 month = tfTimeFields->Month + 1;
212 year = tfTimeFields->Year;
213 }
214 cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
215 day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
216 (1959 * month) / 64 + /* months * daypermonth */
217 tfTimeFields->Day - /* day of the month */
218 584817 ; /* zero that on 1601-01-01 */
219 /* done */
220
221 Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
222 tfTimeFields->Hour) * MINSPERHOUR +
223 tfTimeFields->Minute) * SECSPERMIN +
224 tfTimeFields->Second ) * 1000 +
225 tfTimeFields->Milliseconds ) * TICKSPERMSEC;
Juergen Schmied026d9db1999-03-09 17:47:51 +0000226
Rein Klazesdceae022004-11-06 03:53:53 +0000227 return TRUE;
Juergen Schmied026d9db1999-03-09 17:47:51 +0000228}
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000229
Huw Davies64ed8482004-02-26 05:26:34 +0000230/***********************************************************************
231 * TIME_GetBias [internal]
232 *
233 * Helper function calculates delta local time from UTC.
234 *
235 * PARAMS
236 * utc [I] The current utc time.
237 * pdaylight [I] Local daylight.
238 *
239 * RETURNS
240 * The bias for the current timezone.
241 */
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900242static LONG TIME_GetBias(void)
Huw Davies64ed8482004-02-26 05:26:34 +0000243{
Huw Davies67e2d6c2004-02-27 00:43:20 +0000244 static time_t last_utc;
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900245 static LONG last_bias;
246 LONG ret;
247 time_t utc;
248
249 utc = time( NULL );
Huw Davies67e2d6c2004-02-27 00:43:20 +0000250
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900251 RtlEnterCriticalSection( &TIME_tz_section );
Dmitry Timoshkov6119f492007-07-29 21:33:23 +0900252 if (utc != last_utc)
Huw Davies67e2d6c2004-02-27 00:43:20 +0000253 {
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900254 RTL_TIME_ZONE_INFORMATION tzi;
255 int is_dst = init_tz_info( &tzi );
256
Huw Davies67e2d6c2004-02-27 00:43:20 +0000257 last_utc = utc;
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900258 last_bias = tzi.Bias;
259 last_bias += is_dst ? tzi.DaylightBias : tzi.StandardBias;
260 last_bias *= SECSPERMIN;
Huw Davies67e2d6c2004-02-27 00:43:20 +0000261 }
Dmitry Timoshkov6119f492007-07-29 21:33:23 +0900262
Dmitry Timoshkov6119f492007-07-29 21:33:23 +0900263 ret = last_bias;
264
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900265 RtlLeaveCriticalSection( &TIME_tz_section );
Huw Davies67e2d6c2004-02-27 00:43:20 +0000266 return ret;
Huw Davies64ed8482004-02-26 05:26:34 +0000267}
268
György 'Nog' Jeney4d6ba252002-12-11 00:19:56 +0000269/******************************************************************************
270 * RtlLocalTimeToSystemTime [NTDLL.@]
271 *
Jon Griffiths08922852003-08-19 00:56:34 +0000272 * Convert a local time into system time.
György 'Nog' Jeney4d6ba252002-12-11 00:19:56 +0000273 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000274 * PARAMS
Jon Griffiths08922852003-08-19 00:56:34 +0000275 * LocalTime [I] Local time to convert.
276 * SystemTime [O] Destination for the converted time.
György 'Nog' Jeney4d6ba252002-12-11 00:19:56 +0000277 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000278 * RETURNS
Jon Griffiths08922852003-08-19 00:56:34 +0000279 * Success: STATUS_SUCCESS.
280 * Failure: An NTSTATUS error code indicating the problem.
György 'Nog' Jeney4d6ba252002-12-11 00:19:56 +0000281 */
282NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
283 PLARGE_INTEGER SystemTime)
284{
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900285 LONG bias;
György 'Nog' Jeney4d6ba252002-12-11 00:19:56 +0000286
287 TRACE("(%p, %p)\n", LocalTime, SystemTime);
288
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900289 bias = TIME_GetBias();
290 SystemTime->QuadPart = LocalTime->QuadPart + bias * (LONGLONG)TICKSPERSEC;
György 'Nog' Jeney4d6ba252002-12-11 00:19:56 +0000291 return STATUS_SUCCESS;
292}
Juergen Schmied026d9db1999-03-09 17:47:51 +0000293
294/******************************************************************************
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000295 * RtlSystemTimeToLocalTime [NTDLL.@]
296 *
Jon Griffiths08922852003-08-19 00:56:34 +0000297 * Convert a system time into a local time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000298 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000299 * PARAMS
300 * SystemTime [I] System time to convert.
Jon Griffiths08922852003-08-19 00:56:34 +0000301 * LocalTime [O] Destination for the converted time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000302 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000303 * RETURNS
Jon Griffiths08922852003-08-19 00:56:34 +0000304 * Success: STATUS_SUCCESS.
305 * Failure: An NTSTATUS error code indicating the problem.
Juergen Schmied026d9db1999-03-09 17:47:51 +0000306 */
György 'Nog' Jeney4d6ba252002-12-11 00:19:56 +0000307NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
308 PLARGE_INTEGER LocalTime )
Juergen Schmied026d9db1999-03-09 17:47:51 +0000309{
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900310 LONG bias;
Juergen Schmied026d9db1999-03-09 17:47:51 +0000311
György 'Nog' Jeney4d6ba252002-12-11 00:19:56 +0000312 TRACE("(%p, %p)\n", SystemTime, LocalTime);
313
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900314 bias = TIME_GetBias();
315 LocalTime->QuadPart = SystemTime->QuadPart - bias * (LONGLONG)TICKSPERSEC;
György 'Nog' Jeney4d6ba252002-12-11 00:19:56 +0000316 return STATUS_SUCCESS;
Juergen Schmied026d9db1999-03-09 17:47:51 +0000317}
Juergen Schmied026d9db1999-03-09 17:47:51 +0000318
319/******************************************************************************
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000320 * RtlTimeToSecondsSince1970 [NTDLL.@]
321 *
Jon Griffiths08922852003-08-19 00:56:34 +0000322 * Convert a time into a count of seconds since 1970.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000323 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000324 * PARAMS
Jon Griffiths08922852003-08-19 00:56:34 +0000325 * Time [I] Time to convert.
326 * Seconds [O] Destination for the converted time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000327 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000328 * RETURNS
Jon Griffiths08922852003-08-19 00:56:34 +0000329 * Success: TRUE.
330 * Failure: FALSE, if the resulting value will not fit in a DWORD.
Juergen Schmied026d9db1999-03-09 17:47:51 +0000331 */
Jon Griffiths08922852003-08-19 00:56:34 +0000332BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
Juergen Schmied026d9db1999-03-09 17:47:51 +0000333{
Ge van Geldorp399901e2004-01-23 01:51:33 +0000334 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
Alexandre Julliard49643742009-08-24 12:50:11 +0200335 tmp = tmp / TICKSPERSEC - SECS_1601_TO_1970;
Alexandre Julliardd76f9f92000-10-01 01:40:42 +0000336 if (tmp > 0xffffffff) return FALSE;
Jon Griffiths08922852003-08-19 00:56:34 +0000337 *Seconds = (DWORD)tmp;
Alexandre Julliard0aa6cc22000-07-29 21:56:59 +0000338 return TRUE;
339}
340
341/******************************************************************************
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000342 * RtlTimeToSecondsSince1980 [NTDLL.@]
343 *
Jon Griffiths08922852003-08-19 00:56:34 +0000344 * Convert a time into a count of seconds since 1980.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000345 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000346 * PARAMS
Jon Griffiths08922852003-08-19 00:56:34 +0000347 * Time [I] Time to convert.
348 * Seconds [O] Destination for the converted time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000349 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000350 * RETURNS
Jon Griffiths08922852003-08-19 00:56:34 +0000351 * Success: TRUE.
352 * Failure: FALSE, if the resulting value will not fit in a DWORD.
Alexandre Julliard0aa6cc22000-07-29 21:56:59 +0000353 */
Jon Griffiths08922852003-08-19 00:56:34 +0000354BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
Alexandre Julliard0aa6cc22000-07-29 21:56:59 +0000355{
Ge van Geldorp399901e2004-01-23 01:51:33 +0000356 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
Alexandre Julliard49643742009-08-24 12:50:11 +0200357 tmp = tmp / TICKSPERSEC - SECS_1601_TO_1980;
Alexandre Julliardd76f9f92000-10-01 01:40:42 +0000358 if (tmp > 0xffffffff) return FALSE;
Jon Griffiths08922852003-08-19 00:56:34 +0000359 *Seconds = (DWORD)tmp;
Alexandre Julliard0aa6cc22000-07-29 21:56:59 +0000360 return TRUE;
361}
362
363/******************************************************************************
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000364 * RtlSecondsSince1970ToTime [NTDLL.@]
365 *
Jon Griffiths08922852003-08-19 00:56:34 +0000366 * Convert a count of seconds since 1970 to a time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000367 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000368 * PARAMS
Jon Griffiths08922852003-08-19 00:56:34 +0000369 * Seconds [I] Time to convert.
370 * Time [O] Destination for the converted time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000371 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000372 * RETURNS
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000373 * Nothing.
Alexandre Julliard0aa6cc22000-07-29 21:56:59 +0000374 */
Jon Griffiths08922852003-08-19 00:56:34 +0000375void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
Alexandre Julliard0aa6cc22000-07-29 21:56:59 +0000376{
Jon Griffiths08922852003-08-19 00:56:34 +0000377 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
Ge van Geldorp399901e2004-01-23 01:51:33 +0000378 Time->u.LowPart = (DWORD)secs;
379 Time->u.HighPart = (DWORD)(secs >> 32);
Alexandre Julliard0aa6cc22000-07-29 21:56:59 +0000380}
381
382/******************************************************************************
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000383 * RtlSecondsSince1980ToTime [NTDLL.@]
384 *
Jon Griffiths08922852003-08-19 00:56:34 +0000385 * Convert a count of seconds since 1980 to a time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000386 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000387 * PARAMS
Jon Griffiths08922852003-08-19 00:56:34 +0000388 * Seconds [I] Time to convert.
389 * Time [O] Destination for the converted time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000390 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000391 * RETURNS
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000392 * Nothing.
Alexandre Julliard0aa6cc22000-07-29 21:56:59 +0000393 */
Jon Griffiths08922852003-08-19 00:56:34 +0000394void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
Alexandre Julliard0aa6cc22000-07-29 21:56:59 +0000395{
Jon Griffiths08922852003-08-19 00:56:34 +0000396 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
Ge van Geldorp399901e2004-01-23 01:51:33 +0000397 Time->u.LowPart = (DWORD)secs;
398 Time->u.HighPart = (DWORD)(secs >> 32);
Juergen Schmied026d9db1999-03-09 17:47:51 +0000399}
400
401/******************************************************************************
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000402 * RtlTimeToElapsedTimeFields [NTDLL.@]
403 *
Jon Griffiths08922852003-08-19 00:56:34 +0000404 * Convert a time to a count of elapsed seconds.
405 *
406 * PARAMS
407 * Time [I] Time to convert.
408 * TimeFields [O] Destination for the converted time.
409 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000410 * RETURNS
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000411 * Nothing.
Juergen Schmied026d9db1999-03-09 17:47:51 +0000412 */
Alexandre Julliarda16ed902002-12-18 02:31:33 +0000413void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
Juergen Schmied026d9db1999-03-09 17:47:51 +0000414{
Alexandre Julliarda16ed902002-12-18 02:31:33 +0000415 LONGLONG time;
Mike McCormackb5bd4a12005-09-02 14:47:36 +0000416 INT rem;
Alexandre Julliarda16ed902002-12-18 02:31:33 +0000417
Alexandre Julliard49643742009-08-24 12:50:11 +0200418 time = Time->QuadPart / TICKSPERSEC;
419 TimeFields->Milliseconds = (Time->QuadPart % TICKSPERSEC) / TICKSPERMSEC;
Alexandre Julliarda16ed902002-12-18 02:31:33 +0000420
421 /* time is now in seconds */
422 TimeFields->Year = 0;
423 TimeFields->Month = 0;
Alexandre Julliard49643742009-08-24 12:50:11 +0200424 TimeFields->Day = time / SECSPERDAY;
Alexandre Julliarda16ed902002-12-18 02:31:33 +0000425
426 /* rem is now the remaining seconds in the last day */
Alexandre Julliard49643742009-08-24 12:50:11 +0200427 rem = time % SECSPERDAY;
Alexandre Julliarda16ed902002-12-18 02:31:33 +0000428 TimeFields->Second = rem % 60;
429 rem /= 60;
430 TimeFields->Minute = rem % 60;
431 TimeFields->Hour = rem / 60;
Juergen Schmied026d9db1999-03-09 17:47:51 +0000432}
Alexandre Julliardd76f9f92000-10-01 01:40:42 +0000433
434/***********************************************************************
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000435 * NtQuerySystemTime [NTDLL.@]
436 * ZwQuerySystemTime [NTDLL.@]
437 *
Jon Griffiths08922852003-08-19 00:56:34 +0000438 * Get the current system time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000439 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000440 * PARAMS
Jon Griffiths08922852003-08-19 00:56:34 +0000441 * Time [O] Destination for the current system time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000442 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000443 * RETURNS
Jon Griffiths08922852003-08-19 00:56:34 +0000444 * Success: STATUS_SUCCESS.
445 * Failure: An NTSTATUS error code indicating the problem.
Alexandre Julliardd76f9f92000-10-01 01:40:42 +0000446 */
Jon Griffiths08922852003-08-19 00:56:34 +0000447NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
Alexandre Julliardd76f9f92000-10-01 01:40:42 +0000448{
Alexandre Julliardd76f9f92000-10-01 01:40:42 +0000449 struct timeval now;
450
451 gettimeofday( &now, 0 );
Jon Griffiths08922852003-08-19 00:56:34 +0000452 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
453 Time->QuadPart += now.tv_usec * 10;
Patrik Stridvall9c1de6d2002-09-12 22:07:02 +0000454 return STATUS_SUCCESS;
Alexandre Julliardd76f9f92000-10-01 01:40:42 +0000455}
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000456
Alexandre Julliard9ad56282005-07-14 10:32:46 +0000457/******************************************************************************
458 * NtQueryPerformanceCounter [NTDLL.@]
459 *
460 * Note: Windows uses a timer clocked at a multiple of 1193182 Hz. There is a
461 * good number of applications that crash when the returned frequency is either
Francois Gougetebadbd42007-08-09 10:42:59 +0200462 * lower or higher than what Windows gives. Also too high counter values are
Alexandre Julliard9ad56282005-07-14 10:32:46 +0000463 * reported to give problems.
464 */
465NTSTATUS WINAPI NtQueryPerformanceCounter( PLARGE_INTEGER Counter, PLARGE_INTEGER Frequency )
466{
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200467 LARGE_INTEGER now;
Alexandre Julliard9ad56282005-07-14 10:32:46 +0000468
469 if (!Counter) return STATUS_ACCESS_VIOLATION;
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200470
471 /* convert a counter that increments at a rate of 10 MHz
Alexandre Julliard9ad56282005-07-14 10:32:46 +0000472 * to one of 1.193182 MHz, with some care for arithmetic
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200473 * overflow and good accuracy (21/176 = 0.11931818) */
474 NtQuerySystemTime( &now );
475 Counter->QuadPart = ((now.QuadPart - server_start_time) * 21) / 176;
Alexandre Julliard9ad56282005-07-14 10:32:46 +0000476 if (Frequency) Frequency->QuadPart = 1193182;
477 return STATUS_SUCCESS;
478}
479
Jacek Caban2333c802006-04-19 21:21:52 +0200480
481/******************************************************************************
482 * NtGetTickCount (NTDLL.@)
483 * ZwGetTickCount (NTDLL.@)
484 */
485ULONG WINAPI NtGetTickCount(void)
486{
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200487 LARGE_INTEGER now;
Jacek Caban2333c802006-04-19 21:21:52 +0200488
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200489 NtQuerySystemTime( &now );
490 return (now.QuadPart - server_start_time) / 10000;
Jacek Caban2333c802006-04-19 21:21:52 +0200491}
492
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900493/* calculate the mday of dst change date, so that for instance Sun 5 Oct 2007
494 * (last Sunday in October of 2007) becomes Sun Oct 28 2007
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000495 *
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900496 * Note: year, day and month must be in unix format.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000497 */
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900498static int weekday_to_mday(int year, int day, int mon, int day_of_week)
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000499{
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900500 struct tm date;
501 time_t tmp;
502 int wday, mday;
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000503
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900504 /* find first day in the month matching week day of the date */
505 memset(&date, 0, sizeof(date));
506 date.tm_year = year;
507 date.tm_mon = mon;
508 date.tm_mday = -1;
509 date.tm_wday = -1;
510 do
511 {
512 date.tm_mday++;
513 tmp = mktime(&date);
514 } while (date.tm_wday != day_of_week || date.tm_mon != mon);
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000515
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900516 mday = date.tm_mday;
517
518 /* find number of week days in the month matching week day of the date */
519 wday = 1; /* 1 - 1st, ...., 5 - last */
520 while (wday < day)
521 {
522 struct tm *tm;
523
524 date.tm_mday += 7;
525 tmp = mktime(&date);
526 tm = localtime(&tmp);
527 if (tm->tm_mon != mon)
528 break;
529 mday = tm->tm_mday;
530 wday++;
531 }
532
533 return mday;
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000534}
535
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900536static BOOL match_tz_date(const RTL_SYSTEM_TIME *st, const RTL_SYSTEM_TIME *reg_st)
Rein Klazesc5c93d12004-10-18 21:19:28 +0000537{
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900538 WORD wDay;
539
540 if (st->wMonth != reg_st->wMonth) return FALSE;
541
542 if (!st->wMonth) return TRUE; /* no transition dates */
543
544 wDay = reg_st->wDay;
545 if (!reg_st->wYear) /* date in a day-of-week format */
546 wDay = weekday_to_mday(st->wYear - 1900, reg_st->wDay, reg_st->wMonth - 1, reg_st->wDayOfWeek);
547
548 if (st->wDay != wDay ||
549 st->wHour != reg_st->wHour ||
550 st->wMinute != reg_st->wMinute ||
551 st->wSecond != reg_st->wSecond ||
552 st->wMilliseconds != reg_st->wMilliseconds) return FALSE;
553
554 return TRUE;
555}
556
557static BOOL match_tz_info(const RTL_TIME_ZONE_INFORMATION *tzi, const RTL_TIME_ZONE_INFORMATION *reg_tzi)
558{
559 if (tzi->Bias == reg_tzi->Bias &&
560 match_tz_date(&tzi->StandardDate, &reg_tzi->StandardDate) &&
561 match_tz_date(&tzi->DaylightDate, &reg_tzi->DaylightDate))
562 return TRUE;
563
564 return FALSE;
565}
566
567static BOOL reg_query_value(HKEY hkey, LPCWSTR name, DWORD type, void *data, DWORD count)
568{
569 UNICODE_STRING nameW;
570 char buf[256];
571 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buf;
572
573 if (count > sizeof(buf) - sizeof(KEY_VALUE_PARTIAL_INFORMATION))
574 return FALSE;
575
576 RtlInitUnicodeString(&nameW, name);
577
578 if (NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation,
579 buf, sizeof(buf), &count))
580 return FALSE;
581
582 if (info->Type != type) return FALSE;
583
584 memcpy(data, info->Data, info->DataLength);
585 return TRUE;
586}
587
588static void find_reg_tz_info(RTL_TIME_ZONE_INFORMATION *tzi)
589{
590 static const WCHAR Time_ZonesW[] = { 'M','a','c','h','i','n','e','\\',
591 'S','o','f','t','w','a','r','e','\\',
592 'M','i','c','r','o','s','o','f','t','\\',
593 'W','i','n','d','o','w','s',' ','N','T','\\',
594 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
595 'T','i','m','e',' ','Z','o','n','e','s',0 };
Mike McCormack5b2d5fd2005-06-17 13:58:33 +0000596 HANDLE hkey;
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900597 ULONG idx;
Rein Klazesc5c93d12004-10-18 21:19:28 +0000598 OBJECT_ATTRIBUTES attr;
599 UNICODE_STRING nameW;
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900600 WCHAR buf[128];
Rein Klazesc5c93d12004-10-18 21:19:28 +0000601
602 attr.Length = sizeof(attr);
603 attr.RootDirectory = 0;
604 attr.ObjectName = &nameW;
605 attr.Attributes = 0;
606 attr.SecurityDescriptor = NULL;
607 attr.SecurityQualityOfService = NULL;
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900608 RtlInitUnicodeString(&nameW, Time_ZonesW);
609 if (NtOpenKey(&hkey, KEY_READ, &attr))
610 {
611 WARN("Unable to open the time zones key\n");
612 return;
Rein Klazesc5c93d12004-10-18 21:19:28 +0000613 }
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900614
615 idx = 0;
616 nameW.Buffer = buf;
617 nameW.Length = sizeof(buf);
618 nameW.MaximumLength = sizeof(buf);
619
620 while (!RtlpNtEnumerateSubKey(hkey, &nameW, idx++))
621 {
622 static const WCHAR stdW[] = { 'S','t','d',0 };
623 static const WCHAR dltW[] = { 'D','l','t',0 };
624 static const WCHAR tziW[] = { 'T','Z','I',0 };
625 RTL_TIME_ZONE_INFORMATION reg_tzi;
626 HANDLE hSubkey;
627 struct tz_reg_data
628 {
629 LONG bias;
630 LONG std_bias;
631 LONG dlt_bias;
632 RTL_SYSTEM_TIME std_date;
633 RTL_SYSTEM_TIME dlt_date;
634 } tz_data;
635
636 attr.Length = sizeof(attr);
637 attr.RootDirectory = hkey;
638 attr.ObjectName = &nameW;
639 attr.Attributes = 0;
640 attr.SecurityDescriptor = NULL;
641 attr.SecurityQualityOfService = NULL;
642 if (NtOpenKey(&hSubkey, KEY_READ, &attr))
643 {
644 WARN("Unable to open subkey %s\n", debugstr_wn(nameW.Buffer, nameW.Length/sizeof(WCHAR)));
645 continue;
646 }
647
648#define get_value(hkey, name, type, data, len) \
649 if (!reg_query_value(hkey, name, type, data, len)) \
650 { \
651 WARN("can't read data from %s\n", debugstr_w(name)); \
652 NtClose(hkey); \
653 continue; \
654 }
655
656 get_value(hSubkey, stdW, REG_SZ, reg_tzi.StandardName, sizeof(reg_tzi.StandardName));
657 get_value(hSubkey, dltW, REG_SZ, reg_tzi.DaylightName, sizeof(reg_tzi.DaylightName));
658 get_value(hSubkey, tziW, REG_BINARY, &tz_data, sizeof(tz_data));
659
660#undef get_value
661
662 reg_tzi.Bias = tz_data.bias;
663 reg_tzi.StandardBias = tz_data.std_bias;
664 reg_tzi.DaylightBias = tz_data.dlt_bias;
665 reg_tzi.StandardDate = tz_data.std_date;
666 reg_tzi.DaylightDate = tz_data.dlt_date;
667
668 TRACE("%s: bias %d\n", debugstr_wn(nameW.Buffer, nameW.Length/sizeof(WCHAR)), reg_tzi.Bias);
669 TRACE("std (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
670 reg_tzi.StandardDate.wDay, reg_tzi.StandardDate.wMonth,
671 reg_tzi.StandardDate.wYear, reg_tzi.StandardDate.wDayOfWeek,
672 reg_tzi.StandardDate.wHour, reg_tzi.StandardDate.wMinute,
673 reg_tzi.StandardDate.wSecond, reg_tzi.StandardDate.wMilliseconds,
674 reg_tzi.StandardBias);
675 TRACE("dst (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
676 reg_tzi.DaylightDate.wDay, reg_tzi.DaylightDate.wMonth,
677 reg_tzi.DaylightDate.wYear, reg_tzi.DaylightDate.wDayOfWeek,
678 reg_tzi.DaylightDate.wHour, reg_tzi.DaylightDate.wMinute,
679 reg_tzi.DaylightDate.wSecond, reg_tzi.DaylightDate.wMilliseconds,
680 reg_tzi.DaylightBias);
681
682 NtClose(hSubkey);
683
684 if (match_tz_info(tzi, &reg_tzi))
685 {
Andrew Talbot5ecd33e2008-03-08 15:37:05 +0000686 *tzi = reg_tzi;
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900687 NtClose(hkey);
688 return;
689 }
690
691 /* reset len */
692 nameW.Length = sizeof(buf);
693 nameW.MaximumLength = sizeof(buf);
694 }
695
696 NtClose(hkey);
697
698 FIXME("Can't find matching timezone information in the registry for "
699 "bias %d, std (d/m/y): %u/%02u/%04u, dlt (d/m/y): %u/%02u/%04u\n",
700 tzi->Bias,
701 tzi->StandardDate.wDay, tzi->StandardDate.wMonth, tzi->StandardDate.wYear,
702 tzi->DaylightDate.wDay, tzi->DaylightDate.wMonth, tzi->DaylightDate.wYear);
703}
704
705static time_t find_dst_change(unsigned long min, unsigned long max, int *is_dst)
706{
707 time_t start;
708 struct tm *tm;
709
710 start = min;
711 tm = localtime(&start);
712 *is_dst = !tm->tm_isdst;
713 TRACE("starting date isdst %d, %s", !*is_dst, ctime(&start));
714
715 while (min <= max)
716 {
717 time_t pos = (min + max) / 2;
718 tm = localtime(&pos);
719
720 if (tm->tm_isdst != *is_dst)
721 min = pos + 1;
722 else
723 max = pos - 1;
724 }
725 return min;
726}
727
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900728static int init_tz_info(RTL_TIME_ZONE_INFORMATION *tzi)
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900729{
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900730 static RTL_TIME_ZONE_INFORMATION cached_tzi;
Dmitry Timoshkov0e44bda2011-10-18 15:34:05 +0900731 static int current_year = -1, current_bias = 65535;
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900732 struct tm *tm;
733 time_t year_start, year_end, tmp, dlt = 0, std = 0;
Dmitry Timoshkov0e44bda2011-10-18 15:34:05 +0900734 int is_dst, current_is_dst, bias;
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900735
736 RtlEnterCriticalSection( &TIME_tz_section );
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900737
738 year_start = time(NULL);
Dmitry Timoshkov0e44bda2011-10-18 15:34:05 +0900739 tm = gmtime(&year_start);
740 bias = (LONG)(mktime(tm) - year_start) / 60;
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900741
Dmitry Timoshkov0e44bda2011-10-18 15:34:05 +0900742 tm = localtime(&year_start);
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900743 current_is_dst = tm->tm_isdst;
Dmitry Timoshkov0e44bda2011-10-18 15:34:05 +0900744 if (current_year == tm->tm_year && current_bias == bias)
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900745 {
746 *tzi = cached_tzi;
747 RtlLeaveCriticalSection( &TIME_tz_section );
748 return current_is_dst;
749 }
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900750
751 memset(tzi, 0, sizeof(*tzi));
752
Dmitry Timoshkov0e44bda2011-10-18 15:34:05 +0900753 TRACE("tz data will be valid through year %d, bias %d\n", tm->tm_year + 1900, bias);
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900754 current_year = tm->tm_year;
Dmitry Timoshkov0e44bda2011-10-18 15:34:05 +0900755 current_bias = bias;
756
757 tzi->Bias = bias;
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900758
759 tm->tm_isdst = 0;
760 tm->tm_mday = 1;
761 tm->tm_mon = tm->tm_hour = tm->tm_min = tm->tm_sec = tm->tm_wday = tm->tm_yday = 0;
762 year_start = mktime(tm);
763 TRACE("year_start: %s", ctime(&year_start));
764
765 tm->tm_mday = tm->tm_wday = tm->tm_yday = 0;
766 tm->tm_mon = 12;
767 tm->tm_hour = 23;
768 tm->tm_min = tm->tm_sec = 59;
769 year_end = mktime(tm);
770 TRACE("year_end: %s", ctime(&year_end));
771
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900772 tmp = find_dst_change(year_start, year_end, &is_dst);
773 if (is_dst)
774 dlt = tmp;
775 else
776 std = tmp;
777
778 tmp = find_dst_change(tmp, year_end, &is_dst);
779 if (is_dst)
780 dlt = tmp;
781 else
782 std = tmp;
783
784 TRACE("std: %s", ctime(&std));
785 TRACE("dlt: %s", ctime(&dlt));
786
787 if (dlt == std || !dlt || !std)
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900788 TRACE("there is no daylight saving rules in this time zone\n");
Dmitry Timoshkova76ef052007-08-29 18:56:52 +0900789 else
790 {
791 tmp = dlt - tzi->Bias * 60;
792 tm = gmtime(&tmp);
793 TRACE("dlt gmtime: %s", asctime(tm));
794
795 tzi->DaylightBias = -60;
796 tzi->DaylightDate.wYear = tm->tm_year + 1900;
797 tzi->DaylightDate.wMonth = tm->tm_mon + 1;
798 tzi->DaylightDate.wDayOfWeek = tm->tm_wday;
799 tzi->DaylightDate.wDay = tm->tm_mday;
800 tzi->DaylightDate.wHour = tm->tm_hour;
801 tzi->DaylightDate.wMinute = tm->tm_min;
802 tzi->DaylightDate.wSecond = tm->tm_sec;
803 tzi->DaylightDate.wMilliseconds = 0;
804
805 TRACE("daylight (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
806 tzi->DaylightDate.wDay, tzi->DaylightDate.wMonth,
807 tzi->DaylightDate.wYear, tzi->DaylightDate.wDayOfWeek,
808 tzi->DaylightDate.wHour, tzi->DaylightDate.wMinute,
809 tzi->DaylightDate.wSecond, tzi->DaylightDate.wMilliseconds,
810 tzi->DaylightBias);
811
812 tmp = std - tzi->Bias * 60 - tzi->DaylightBias * 60;
813 tm = gmtime(&tmp);
814 TRACE("std gmtime: %s", asctime(tm));
815
816 tzi->StandardBias = 0;
817 tzi->StandardDate.wYear = tm->tm_year + 1900;
818 tzi->StandardDate.wMonth = tm->tm_mon + 1;
819 tzi->StandardDate.wDayOfWeek = tm->tm_wday;
820 tzi->StandardDate.wDay = tm->tm_mday;
821 tzi->StandardDate.wHour = tm->tm_hour;
822 tzi->StandardDate.wMinute = tm->tm_min;
823 tzi->StandardDate.wSecond = tm->tm_sec;
824 tzi->StandardDate.wMilliseconds = 0;
825
826 TRACE("standard (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
827 tzi->StandardDate.wDay, tzi->StandardDate.wMonth,
828 tzi->StandardDate.wYear, tzi->StandardDate.wDayOfWeek,
829 tzi->StandardDate.wHour, tzi->StandardDate.wMinute,
830 tzi->StandardDate.wSecond, tzi->StandardDate.wMilliseconds,
831 tzi->StandardBias);
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900832 }
833
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900834 find_reg_tz_info(tzi);
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900835 cached_tzi = *tzi;
836
837 RtlLeaveCriticalSection( &TIME_tz_section );
838
839 return current_is_dst;
Rein Klazesc5c93d12004-10-18 21:19:28 +0000840}
841
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000842/***********************************************************************
843 * RtlQueryTimeZoneInformation [NTDLL.@]
844 *
Jon Griffiths08922852003-08-19 00:56:34 +0000845 * Get information about the current timezone.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000846 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000847 * PARAMS
Jon Griffiths08922852003-08-19 00:56:34 +0000848 * tzinfo [O] Destination for the retrieved timezone info.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000849 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000850 * RETURNS
Jon Griffiths08922852003-08-19 00:56:34 +0000851 * Success: STATUS_SUCCESS.
852 * Failure: An NTSTATUS error code indicating the problem.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000853 */
Mike McCormack78911f32005-06-20 18:37:17 +0000854NTSTATUS WINAPI RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION *tzinfo)
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000855{
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900856 init_tz_info( tzinfo );
Dmitry Timoshkovd6661432007-08-07 20:01:16 +0900857
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000858 return STATUS_SUCCESS;
859}
860
861/***********************************************************************
862 * RtlSetTimeZoneInformation [NTDLL.@]
863 *
Jon Griffiths08922852003-08-19 00:56:34 +0000864 * Set the current time zone information.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000865 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000866 * PARAMS
Jon Griffiths08922852003-08-19 00:56:34 +0000867 * tzinfo [I] Timezone information to set.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000868 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000869 * RETURNS
Jon Griffiths08922852003-08-19 00:56:34 +0000870 * Success: STATUS_SUCCESS.
871 * Failure: An NTSTATUS error code indicating the problem.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000872 *
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000873 */
Mike McCormack78911f32005-06-20 18:37:17 +0000874NTSTATUS WINAPI RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION *tzinfo )
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000875{
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000876 return STATUS_PRIVILEGE_NOT_HELD;
877}
878
879/***********************************************************************
880 * NtSetSystemTime [NTDLL.@]
881 * ZwSetSystemTime [NTDLL.@]
882 *
Jon Griffiths08922852003-08-19 00:56:34 +0000883 * Set the system time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000884 *
Jon Griffiths08922852003-08-19 00:56:34 +0000885 * PARAMS
886 * NewTime [I] The time to set.
887 * OldTime [O] Optional destination for the previous system time.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000888 *
Jon Griffithscd4234a2003-03-18 18:35:48 +0000889 * RETURNS
Jon Griffiths08922852003-08-19 00:56:34 +0000890 * Success: STATUS_SUCCESS.
891 * Failure: An NTSTATUS error code indicating the problem.
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000892 */
893NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
894{
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000895 struct timeval tv;
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900896 time_t tm_t;
897 DWORD sec, oldsec;
898 LARGE_INTEGER tm;
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000899
900 /* Return the old time if necessary */
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900901 if (!OldTime) OldTime = &tm;
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000902
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900903 NtQuerySystemTime( OldTime );
904 RtlTimeToSecondsSince1970( OldTime, &oldsec );
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000905
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900906 RtlTimeToSecondsSince1970( NewTime, &sec );
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000907
908 /* set the new time */
909 tv.tv_sec = sec;
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900910 tv.tv_usec = 0;
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000911
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000912#ifdef HAVE_SETTIMEOFDAY
Rob Shearman67cc0302007-09-07 18:41:42 +0100913 if (!settimeofday(&tv, NULL)) /* 0 is OK, -1 is error */
914 return STATUS_SUCCESS;
Dmitry Timoshkovd2d5bbe2007-08-22 17:02:18 +0900915 tm_t = sec;
Rob Shearman67cc0302007-09-07 18:41:42 +0100916 ERR("Cannot set time to %s, time adjustment %ld: %s\n",
917 ctime(&tm_t), (long)(sec-oldsec), strerror(errno));
918 if (errno == EPERM)
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000919 return STATUS_PRIVILEGE_NOT_HELD;
920 else
Rob Shearman67cc0302007-09-07 18:41:42 +0100921 return STATUS_INVALID_PARAMETER;
922#else
923 tm_t = sec;
924 FIXME("setting time to %s not implemented for missing settimeofday\n",
925 ctime(&tm_t));
926 return STATUS_NOT_IMPLEMENTED;
927#endif
György 'Nog' Jeney6f3015b2002-11-25 01:12:39 +0000928}