Clean up devenum and properly implement DllCanUnloadNow ref counting.
[wine/multimedia.git] / dlls / ntdll / time.c
blob6b5fd0989fb3af15f073c259c269efad43e26292
1 /*
2 * Nt time functions.
4 * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
5 * adapted to wine with special permissions of the author. This code is
6 * Copyright 2002 Rex Jolliff (rex@lvcablemodem.com)
8 * Copyright 1999 Juergen Schmied
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <time.h>
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
35 #endif
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winreg.h"
45 #include "winternl.h"
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
48 #include "ntdll_misc.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
52 static CRITICAL_SECTION TIME_GetBias_section;
53 static CRITICAL_SECTION_DEBUG critsect_debug =
55 0, 0, &TIME_GetBias_section,
56 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
57 0, 0, { 0, (DWORD)(__FILE__ ": TIME_GetBias_section") }
59 static CRITICAL_SECTION TIME_GetBias_section = { &critsect_debug, -1, 0, 0, 0, 0 };
61 /* TimeZone registry key values */
62 static const WCHAR TZInformationKeyW[] = { 'M','a','c','h','i','n','e','\\',
63 'S','Y','S','T','E','M','\\','C','u','r','r','e','n','t','C','o','n','t','r',
64 'o','l','S','e','t','\\','C','o','n','t','r','o','l','\\','T','i','m','e','z',
65 'o','n','e','I','n','f','o','r','m','a','t','i','o','n', 0};
66 static const WCHAR TZStandardStartW[] = {
67 'S','t','a','n','d','a','r','d','s','t','a','r','t', 0};
68 static const WCHAR TZDaylightStartW[] = {
69 'D','a','y','l','i','g','h','t','s','t','a','r','t', 0};
70 static const WCHAR TZDaylightBiasW[] = {
71 'D','a','y','l','i','g','h','t','B','i','a','s', 0};
72 static const WCHAR TZStandardBiasW[] = {
73 'S','t','a','n','d','a','r','d','B','i','a','s', 0};
74 static const WCHAR TZBiasW[] = {'B','i','a','s', 0};
75 static const WCHAR TZDaylightNameW[] = {
76 'D','a','y','l','i','g','h','t','N','a','m','e', 0};
77 static const WCHAR TZStandardNameW[] = {
78 'S','t','a','n','d','a','r','d','N','a','m','e', 0};
81 #define SETTIME_MAX_ADJUST 120
83 /* This structure is used to store strings that represent all of the time zones
84 * in the world. (This is used to help GetTimeZoneInformation)
86 struct tagTZ_INFO
88 const char *psTZFromUnix;
89 WCHAR psTZWindows[32];
90 int bias;
91 int dst;
94 static const struct tagTZ_INFO TZ_INFO[] =
96 {"MHT",
97 {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
98 'T','i','m','e','\0'}, -720, 0},
99 {"SST",
100 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
101 'e','\0'}, 660, 0},
102 {"HST",
103 {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
104 'T','i','m','e','\0'}, 600, 0},
105 {"AKDT",
106 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
107 'i','m','e','\0'}, 480, 1},
108 {"PST",
109 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
110 'i','m','e','\0'}, 480, 0},
111 {"PDT",
112 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
113 'i','m','e','\0'}, 420, 1},
114 {"MST",
115 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
116 'r','d',' ','T','i','m','e','\0'}, 420, 0},
117 {"MDT",
118 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
119 'T','i','m','e','\0'}, 360, 1},
120 {"CST",
121 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
122 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
123 {"CDT",
124 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
125 'i','m','e','\0'}, 300, 1},
126 {"COT",
127 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
128 'd',' ','T','i','m','e','\0'}, 300, 0},
129 {"EDT",
130 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
131 'i','m','e','\0'}, 240, 1},
132 {"EST",
133 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
134 'd',' ','T','i','m','e','\0'}, 300, 0},
135 {"ADT",
136 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
137 'T','i','m','e','\0'}, 180, 1},
138 {"VET",
139 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
140 'd',' ','T','i','m','e','\0'}, 240, 0},
141 {"CLT",
142 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
143 'd',' ','T','i','m','e','\0'}, 240, 0},
144 {"NDT",
145 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
146 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
147 {"BRT",
148 {'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S',
149 't','a','n','d','a','r','d',' ','T','i','m','e','\0'}, 180, 0},
150 {"ART",
151 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
152 'd',' ','T','i','m','e','\0'}, 180, 0},
153 {"WGST",
154 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
155 ' ','T','i','m','e','\0'}, 120, 1},
156 {"GST",
157 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
158 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
159 {"AZOST",
160 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
161 'm','e','\0'}, 0, 1},
162 {"CVT",
163 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
164 'd',' ','T','i','m','e','\0'}, 60, 0},
165 {"WET",
166 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
167 ' ','T','i','m','e','\0'}, 0, 0},
168 {"BST",
169 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
170 -60, 1},
171 {"GMT",
172 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
173 0, 0},
174 {"UTC",
175 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
176 0, 0},
177 {"CET",
178 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e','a','n',' ',
179 'T','i','m','e','\0'}, -60, 0},
180 {"CEST",
181 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
182 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
183 {"MET",
184 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
185 'n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
186 {"MEST",
187 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','D','a','y',
188 'l','i','g','h','t',' ','T','i','m','e','\0'}, -120, 1},
189 {"WAT",
190 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
191 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
192 {"EEST",
193 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
194 ' ','T','i','m','e','\0'}, -180, 1},
195 {"EET",
196 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
197 'e','\0'}, -120, 0},
198 {"CAT",
199 {'C','e','n','t','r','a','l',' ','A','f','r','i','c','a','n',' '
200 ,'T','i','m','e','\0'}, -120, 0},
201 {"SAST",
202 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
203 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
204 {"IST",
205 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
206 'm','e','\0'}, -120, 0},
207 {"MSK",
208 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
209 'i','m','e','\0'}, -180, 0},
210 {"ADT",
211 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
212 'm','e','\0'}, -240, 1},
213 {"AST",
214 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
215 '\0'}, -180, 0},
216 {"MSD",
217 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
218 'i','m','e','\0'}, -240, 1},
219 {"EAT",
220 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
221 ' ','T','i','m','e','\0'}, -180, 0},
222 {"IRST",
223 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
224 '\0'}, -270, 1},
225 {"GST",
226 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
227 'i','m','e','\0'}, -240, 0},
228 {"AZST",
229 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
230 'T','i','m','e','\0'}, -300, 1},
231 {"AFT",
232 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
233 'r','d',' ','T','i','m','e','\0'}, -270, 0},
234 {"YEKST",
235 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
236 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
237 {"PKT",
238 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
239 ' ','T','i','m','e','\0'}, -300, 0},
240 {"IST",
241 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
242 'e','\0'}, -330, 0},
243 {"NPT",
244 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
245 'e','\0'}, -345, 0},
246 {"ALMST",
247 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
248 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
249 {"BDT",
250 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
251 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
252 {"LKT",
253 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
254 ' ','T','i','m','e','\0'}, -360, 0},
255 {"MMT",
256 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
257 'i','m','e','\0'}, -390, 0},
258 {"ICT",
259 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
260 'i','m','e','\0'}, -420, 0},
261 {"KRAST",
262 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
263 'd',' ','T','i','m','e','\0'}, -480, 1},
264 {"CST",
265 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
266 'e','\0'}, -480, 0},
267 {"IRKST",
268 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
269 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
270 {"SGT",
271 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
272 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
273 {"WST",
274 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
275 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
276 {"JST",
277 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
278 'e','\0'}, -540, 0},
279 {"KST",
280 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
281 'e','\0'}, -540, 0},
282 {"YAKST",
283 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
284 'i','m','e','\0'}, -600, 1},
285 {"CST",
286 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
287 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
288 {"EST",
289 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
290 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
291 {"GST",
292 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
293 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
294 {"VLAST",
295 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
296 'r','d',' ','T','i','m','e','\0'}, -660, 1},
297 {"MAGST",
298 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
299 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
300 {"NZST",
301 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
302 'r','d',' ','T','i','m','e','\0'}, -720, 0},
303 {"FJT",
304 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
305 '\0'}, -720, 0},
306 {"TOT",
307 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
308 'e','\0'}, -780, 0},
309 {"NOVT",
310 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','t','a','n','d','a',
311 'r','d',' ','T','i','m','e','\0'}, -360, 1}
314 #define TICKSPERSEC 10000000
315 #define TICKSPERMSEC 10000
316 #define SECSPERDAY 86400
317 #define SECSPERHOUR 3600
318 #define SECSPERMIN 60
319 #define MINSPERHOUR 60
320 #define HOURSPERDAY 24
321 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
322 #define DAYSPERWEEK 7
323 #define EPOCHYEAR 1601
324 #define DAYSPERNORMALYEAR 365
325 #define DAYSPERLEAPYEAR 366
326 #define MONSPERYEAR 12
327 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
328 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
329 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
331 /* 1601 to 1970 is 369 years plus 89 leap days */
332 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
333 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
334 /* 1601 to 1980 is 379 years plus 91 leap days */
335 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
336 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
337 /* max ticks that can be represented as Unix time */
338 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
341 static const int MonthLengths[2][MONSPERYEAR] =
343 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
344 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
347 static inline int IsLeapYear(int Year)
349 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
352 /***********************************************************************
353 * NTDLL_get_server_timeout
355 * Convert a NTDLL timeout into a timeval struct to send to the server.
357 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
359 UINT remainder;
361 if (!timeout) /* infinite timeout */
363 when->sec = when->usec = 0;
365 else if (timeout->QuadPart <= 0) /* relative timeout */
367 struct timeval tv;
369 if (-timeout->QuadPart > (LONGLONG)INT_MAX * TICKSPERSEC)
370 when->sec = when->usec = INT_MAX;
371 else
373 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
374 gettimeofday( &tv, 0 );
375 when->sec = tv.tv_sec + sec;
376 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
378 when->usec -= 1000000;
379 when->sec++;
381 if (when->sec < tv.tv_sec) /* overflow */
382 when->sec = when->usec = INT_MAX;
385 else /* absolute time */
387 if (timeout->QuadPart < TICKS_1601_TO_1970)
388 when->sec = when->usec = 0;
389 else if (timeout->QuadPart > TICKS_1601_TO_UNIX_MAX)
390 when->sec = when->usec = INT_MAX;
391 else
393 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
394 TICKSPERSEC, &remainder );
395 when->usec = remainder / 10;
401 /******************************************************************************
402 * RtlTimeToTimeFields [NTDLL.@]
404 * Convert a time into a TIME_FIELDS structure.
406 * PARAMS
407 * liTime [I] Time to convert.
408 * TimeFields [O] Destination for the converted time.
410 * RETURNS
411 * Nothing.
413 VOID WINAPI RtlTimeToTimeFields(
414 const LARGE_INTEGER *liTime,
415 PTIME_FIELDS TimeFields)
417 int SecondsInDay;
418 long int cleaps, years, yearday, months;
419 long int Days;
420 LONGLONG Time;
422 /* Extract millisecond from time and convert time into seconds */
423 TimeFields->Milliseconds =
424 (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
425 Time = liTime->QuadPart / TICKSPERSEC;
427 /* The native version of RtlTimeToTimeFields does not take leap seconds
428 * into account */
430 /* Split the time into days and seconds within the day */
431 Days = Time / SECSPERDAY;
432 SecondsInDay = Time % SECSPERDAY;
434 /* compute time of day */
435 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
436 SecondsInDay = SecondsInDay % SECSPERHOUR;
437 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
438 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
440 /* compute day of week */
441 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
443 /* compute year, month and day of month. */
444 cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
445 Days += 28188 + cleaps;
446 years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
447 yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
448 months = (64 * yearday) / 1959;
449 /* the result is based on a year starting on March.
450 * To convert take 12 from Januari and Februari and
451 * increase the year by one. */
452 if( months < 14 ) {
453 TimeFields->Month = months - 1;
454 TimeFields->Year = years + 1524;
455 } else {
456 TimeFields->Month = months - 13;
457 TimeFields->Year = years + 1525;
459 /* calculation of day of month is based on the wonderful
460 * sequence of INT( n * 30.6): it reproduces the
461 * 31-30-31-30-31-31 month lengths exactly for small n's */
462 TimeFields->Day = yearday - (1959 * months) / 64 ;
463 return;
466 /******************************************************************************
467 * RtlTimeFieldsToTime [NTDLL.@]
469 * Convert a TIME_FIELDS structure into a time.
471 * PARAMS
472 * ftTimeFields [I] TIME_FIELDS structure to convert.
473 * Time [O] Destination for the converted time.
475 * RETURNS
476 * Success: TRUE.
477 * Failure: FALSE.
479 BOOLEAN WINAPI RtlTimeFieldsToTime(
480 PTIME_FIELDS tfTimeFields,
481 PLARGE_INTEGER Time)
483 int month, year, cleaps, day;
485 /* FIXME: normalize the TIME_FIELDS structure here */
486 /* No, native just returns 0 (error) if the fields are not */
487 if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
488 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
489 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
490 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
491 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
492 tfTimeFields->Day < 1 ||
493 tfTimeFields->Day > MonthLengths
494 [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
495 [ tfTimeFields->Month - 1] ||
496 tfTimeFields->Year < 1601 )
497 return FALSE;
499 /* now calculate a day count from the date
500 * First start counting years from March. This way the leap days
501 * are added at the end of the year, not somewhere in the middle.
502 * Formula's become so much less complicate that way.
503 * To convert: add 12 to the month numbers of Jan and Feb, and
504 * take 1 from the year */
505 if(tfTimeFields->Month < 3) {
506 month = tfTimeFields->Month + 13;
507 year = tfTimeFields->Year - 1;
508 } else {
509 month = tfTimeFields->Month + 1;
510 year = tfTimeFields->Year;
512 cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
513 day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
514 (1959 * month) / 64 + /* months * daypermonth */
515 tfTimeFields->Day - /* day of the month */
516 584817 ; /* zero that on 1601-01-01 */
517 /* done */
519 Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
520 tfTimeFields->Hour) * MINSPERHOUR +
521 tfTimeFields->Minute) * SECSPERMIN +
522 tfTimeFields->Second ) * 1000 +
523 tfTimeFields->Milliseconds ) * TICKSPERMSEC;
525 return TRUE;
528 /***********************************************************************
529 * TIME_GetBias [internal]
531 * Helper function calculates delta local time from UTC.
533 * PARAMS
534 * utc [I] The current utc time.
535 * pdaylight [I] Local daylight.
537 * RETURNS
538 * The bias for the current timezone.
540 static int TIME_GetBias(time_t utc, int *pdaylight)
542 struct tm *ptm;
543 static time_t last_utc;
544 static int last_bias;
545 static int last_daylight;
546 int ret;
548 RtlEnterCriticalSection( &TIME_GetBias_section );
549 if(utc == last_utc)
551 *pdaylight = last_daylight;
552 ret = last_bias;
553 } else
555 ptm = localtime(&utc);
556 *pdaylight = last_daylight =
557 ptm->tm_isdst; /* daylight for local timezone */
558 ptm = gmtime(&utc);
559 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
560 last_utc = utc;
561 ret = last_bias = (int)(utc-mktime(ptm));
563 RtlLeaveCriticalSection( &TIME_GetBias_section );
564 return ret;
567 /******************************************************************************
568 * RtlLocalTimeToSystemTime [NTDLL.@]
570 * Convert a local time into system time.
572 * PARAMS
573 * LocalTime [I] Local time to convert.
574 * SystemTime [O] Destination for the converted time.
576 * RETURNS
577 * Success: STATUS_SUCCESS.
578 * Failure: An NTSTATUS error code indicating the problem.
580 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
581 PLARGE_INTEGER SystemTime)
583 time_t gmt;
584 int bias, daylight;
586 TRACE("(%p, %p)\n", LocalTime, SystemTime);
588 gmt = time(NULL);
589 bias = TIME_GetBias(gmt, &daylight);
591 SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)10000000;
592 return STATUS_SUCCESS;
595 /******************************************************************************
596 * RtlSystemTimeToLocalTime [NTDLL.@]
598 * Convert a system time into a local time.
600 * PARAMS
601 * SystemTime [I] System time to convert.
602 * LocalTime [O] Destination for the converted time.
604 * RETURNS
605 * Success: STATUS_SUCCESS.
606 * Failure: An NTSTATUS error code indicating the problem.
608 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
609 PLARGE_INTEGER LocalTime )
611 time_t gmt;
612 int bias, daylight;
614 TRACE("(%p, %p)\n", SystemTime, LocalTime);
616 gmt = time(NULL);
617 bias = TIME_GetBias(gmt, &daylight);
619 LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)10000000;
620 return STATUS_SUCCESS;
623 /******************************************************************************
624 * RtlTimeToSecondsSince1970 [NTDLL.@]
626 * Convert a time into a count of seconds since 1970.
628 * PARAMS
629 * Time [I] Time to convert.
630 * Seconds [O] Destination for the converted time.
632 * RETURNS
633 * Success: TRUE.
634 * Failure: FALSE, if the resulting value will not fit in a DWORD.
636 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
638 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
639 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
640 tmp -= SECS_1601_TO_1970;
641 if (tmp > 0xffffffff) return FALSE;
642 *Seconds = (DWORD)tmp;
643 return TRUE;
646 /******************************************************************************
647 * RtlTimeToSecondsSince1980 [NTDLL.@]
649 * Convert a time into a count of seconds since 1980.
651 * PARAMS
652 * Time [I] Time to convert.
653 * Seconds [O] Destination for the converted time.
655 * RETURNS
656 * Success: TRUE.
657 * Failure: FALSE, if the resulting value will not fit in a DWORD.
659 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
661 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
662 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
663 tmp -= SECS_1601_TO_1980;
664 if (tmp > 0xffffffff) return FALSE;
665 *Seconds = (DWORD)tmp;
666 return TRUE;
669 /******************************************************************************
670 * RtlSecondsSince1970ToTime [NTDLL.@]
672 * Convert a count of seconds since 1970 to a time.
674 * PARAMS
675 * Seconds [I] Time to convert.
676 * Time [O] Destination for the converted time.
678 * RETURNS
679 * Nothing.
681 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
683 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
684 Time->u.LowPart = (DWORD)secs;
685 Time->u.HighPart = (DWORD)(secs >> 32);
688 /******************************************************************************
689 * RtlSecondsSince1980ToTime [NTDLL.@]
691 * Convert a count of seconds since 1980 to a time.
693 * PARAMS
694 * Seconds [I] Time to convert.
695 * Time [O] Destination for the converted time.
697 * RETURNS
698 * Nothing.
700 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
702 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
703 Time->u.LowPart = (DWORD)secs;
704 Time->u.HighPart = (DWORD)(secs >> 32);
707 /******************************************************************************
708 * RtlTimeToElapsedTimeFields [NTDLL.@]
710 * Convert a time to a count of elapsed seconds.
712 * PARAMS
713 * Time [I] Time to convert.
714 * TimeFields [O] Destination for the converted time.
716 * RETURNS
717 * Nothing.
719 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
721 LONGLONG time;
722 UINT rem;
724 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
725 TimeFields->Milliseconds = rem / TICKSPERMSEC;
727 /* time is now in seconds */
728 TimeFields->Year = 0;
729 TimeFields->Month = 0;
730 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
732 /* rem is now the remaining seconds in the last day */
733 TimeFields->Second = rem % 60;
734 rem /= 60;
735 TimeFields->Minute = rem % 60;
736 TimeFields->Hour = rem / 60;
739 /***********************************************************************
740 * NtQuerySystemTime [NTDLL.@]
741 * ZwQuerySystemTime [NTDLL.@]
743 * Get the current system time.
745 * PARAMS
746 * Time [O] Destination for the current system time.
748 * RETURNS
749 * Success: STATUS_SUCCESS.
750 * Failure: An NTSTATUS error code indicating the problem.
752 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
754 struct timeval now;
756 gettimeofday( &now, 0 );
757 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
758 Time->QuadPart += now.tv_usec * 10;
759 return STATUS_SUCCESS;
762 /***********************************************************************
763 * TIME_GetTZAsStr [internal]
765 * Helper function that returns the given timezone as a string.
767 * PARAMS
768 * utc [I] The current utc time.
769 * bias [I] The bias of the current timezone.
770 * dst [I] ??
772 * RETURNS
773 * Timezone name.
775 * NOTES:
776 * This could be done with a hash table instead of merely iterating through a
777 * table, however with the small amount of entries (60 or so) I didn't think
778 * it was worth it.
780 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
782 char psTZName[7];
783 struct tm *ptm = localtime(&utc);
784 unsigned int i;
786 if (!strftime (psTZName, 7, "%Z", ptm))
787 return (NULL);
789 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
791 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
792 TZ_INFO[i].bias == bias &&
793 TZ_INFO[i].dst == dst
795 return TZ_INFO[i].psTZWindows;
797 FIXME("Can't match system time zone name \"%s\" to an entry in TZ_INFO\n",psTZName);
798 FIXME(" Please add appropriate entry to TZ_INFO and submit as patch to wine-patches\n");
799 return NULL;
802 /*** TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
805 static int TIME_GetTimeZoneInfoFromReg(LPTIME_ZONE_INFORMATION tzinfo)
807 BYTE buf[90];
808 KEY_VALUE_PARTIAL_INFORMATION * KpInfo =
809 (KEY_VALUE_PARTIAL_INFORMATION *) buf;
810 HKEY hkey;
811 DWORD size;
812 OBJECT_ATTRIBUTES attr;
813 UNICODE_STRING nameW;
815 attr.Length = sizeof(attr);
816 attr.RootDirectory = 0;
817 attr.ObjectName = &nameW;
818 attr.Attributes = 0;
819 attr.SecurityDescriptor = NULL;
820 attr.SecurityQualityOfService = NULL;
821 RtlInitUnicodeString( &nameW, TZInformationKeyW);
822 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) {
824 #define GTZIFR_N( valkey, tofield) \
825 RtlInitUnicodeString( &nameW, valkey );\
826 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
827 sizeof(buf), &size )) { \
828 if( size >= (sizeof((tofield)) + \
829 offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))); { \
830 memcpy(&(tofield), \
831 KpInfo->Data, sizeof(tofield)); \
834 #define GTZIFR_S( valkey, tofield) \
835 RtlInitUnicodeString( &nameW, valkey );\
836 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
837 sizeof(buf), &size )) { \
838 strncpyW( tofield, (WCHAR*) KpInfo->Data, \
839 sizeof( tofield) / sizeof(WCHAR) ); \
842 GTZIFR_N( TZStandardStartW, tzinfo->StandardDate)
843 GTZIFR_N( TZDaylightStartW, tzinfo->DaylightDate)
844 GTZIFR_N( TZBiasW, tzinfo->Bias)
845 GTZIFR_N( TZStandardBiasW, tzinfo->StandardBias)
846 GTZIFR_N( TZDaylightBiasW, tzinfo->DaylightBias)
847 GTZIFR_S( TZStandardNameW, tzinfo->StandardName)
848 GTZIFR_S( TZDaylightNameW, tzinfo->DaylightName)
850 #undef GTZIFR_N
851 #undef GTZIFR_S
852 NtClose( hkey );
853 return 1;
855 return 0;
858 /***********************************************************************
859 * RtlQueryTimeZoneInformation [NTDLL.@]
861 * Get information about the current timezone.
863 * PARAMS
864 * tzinfo [O] Destination for the retrieved timezone info.
866 * RETURNS
867 * Success: STATUS_SUCCESS.
868 * Failure: An NTSTATUS error code indicating the problem.
870 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
872 time_t gmt;
873 int bias, daylight;
874 const WCHAR *psTZ;
876 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
878 if( !TIME_GetTimeZoneInfoFromReg(tzinfo)) {
880 gmt = time(NULL);
881 bias = TIME_GetBias(gmt, &daylight);
883 tzinfo->Bias = -bias / 60;
884 tzinfo->StandardBias = 0;
885 tzinfo->DaylightBias = -60;
886 tzinfo->StandardName[0]='\0';
887 tzinfo->DaylightName[0]='\0';
888 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
889 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
891 return STATUS_SUCCESS;
894 /***********************************************************************
895 * RtlSetTimeZoneInformation [NTDLL.@]
897 * Set the current time zone information.
899 * PARAMS
900 * tzinfo [I] Timezone information to set.
902 * RETURNS
903 * Success: STATUS_SUCCESS.
904 * Failure: An NTSTATUS error code indicating the problem.
906 * BUGS
907 * Uses the obsolete unix timezone structure and tz_dsttime member.
909 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
911 #ifdef HAVE_SETTIMEOFDAY
912 struct timezone tz;
914 tz.tz_minuteswest = tzinfo->Bias;
915 #ifdef DST_NONE
916 tz.tz_dsttime = DST_NONE;
917 #else
918 tz.tz_dsttime = 0;
919 #endif
920 if(!settimeofday(NULL, &tz))
921 return STATUS_SUCCESS;
922 #endif
923 return STATUS_PRIVILEGE_NOT_HELD;
926 /***********************************************************************
927 * NtSetSystemTime [NTDLL.@]
928 * ZwSetSystemTime [NTDLL.@]
930 * Set the system time.
932 * PARAMS
933 * NewTime [I] The time to set.
934 * OldTime [O] Optional destination for the previous system time.
936 * RETURNS
937 * Success: STATUS_SUCCESS.
938 * Failure: An NTSTATUS error code indicating the problem.
940 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
942 TIME_FIELDS tf;
943 struct timeval tv;
944 struct timezone tz;
945 struct tm t;
946 time_t sec, oldsec;
947 int dst, bias;
948 int err;
950 /* Return the old time if necessary */
951 if(OldTime)
952 NtQuerySystemTime(OldTime);
954 RtlTimeToTimeFields(NewTime, &tf);
956 /* call gettimeofday to get the current timezone */
957 gettimeofday(&tv, &tz);
958 oldsec = tv.tv_sec;
959 /* get delta local time from utc */
960 bias = TIME_GetBias(oldsec, &dst);
962 /* get the number of seconds */
963 t.tm_sec = tf.Second;
964 t.tm_min = tf.Minute;
965 t.tm_hour = tf.Hour;
966 t.tm_mday = tf.Day;
967 t.tm_mon = tf.Month - 1;
968 t.tm_year = tf.Year - 1900;
969 t.tm_isdst = dst;
970 sec = mktime (&t);
971 /* correct for timezone and daylight */
972 sec += bias;
974 /* set the new time */
975 tv.tv_sec = sec;
976 tv.tv_usec = tf.Milliseconds * 1000;
978 /* error and sanity check*/
979 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
980 err = 2;
981 } else {
982 #ifdef HAVE_SETTIMEOFDAY
983 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
984 if(err == 0)
985 return STATUS_SUCCESS;
986 #else
987 err = 1;
988 #endif
991 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
992 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
993 (long)(sec-oldsec),
994 err == -1 ? "No Permission"
995 : sec == (time_t)-1 ? "" : "is too large." );
997 if(err == 2)
998 return STATUS_INVALID_PARAMETER;
999 else if(err == -1)
1000 return STATUS_PRIVILEGE_NOT_HELD;
1001 else
1002 return STATUS_NOT_IMPLEMENTED;