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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/port.h"
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
43 #define WIN32_NO_STATUS
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
48 #include "ntdll_misc.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
52 static RTL_CRITICAL_SECTION TIME_GetBias_section;
53 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
55 0, 0, &TIME_GetBias_section,
56 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
57 0, 0, { (DWORD_PTR)(__FILE__ ": TIME_GetBias_section") }
59 static RTL_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)
88 const char *psTZFromUnix;
89 WCHAR psTZWindows[32];
94 static const struct tagTZ_INFO TZ_INFO[] =
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},
100 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
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},
106 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ',
107 'T','i','m','e',0}, 540, 0 },
109 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
110 'i','m','e','\0'}, 480, 1},
112 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
113 'i','m','e','\0'}, 480, 0},
115 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
116 'i','m','e','\0'}, 420, 1},
118 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
119 'r','d',' ','T','i','m','e','\0'}, 420, 0},
121 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
122 'T','i','m','e','\0'}, 360, 1},
124 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
125 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
127 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
128 'i','m','e','\0'}, 300, 1},
130 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
131 'd',' ','T','i','m','e','\0'}, 300, 0},
133 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
134 'd',' ','T','i','m','e',0}, 300, 0 },
136 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
137 'i','m','e','\0'}, 240, 1},
139 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
140 'd',' ','T','i','m','e','\0'}, 300, 0},
142 {'E','a','s','t','e','r','n',' ','C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r',
143 'd',' ','T','i','m','e','\0'}, 300, 0},
145 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
146 'T','i','m','e','\0'}, 180, 1},
148 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
149 'd',' ','T','i','m','e','\0'}, 240, 0},
151 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
152 'd',' ','T','i','m','e','\0'}, 240, 0},
154 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
155 'd',' ','T','i','m','e',0}, 180, 1},
157 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
158 'T','i','m','e',0}, 240, 0 },
160 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
161 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
163 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
164 'a','r','d',' ','T','i','m','e',0}, 210, 0 },
166 {'B','r','a','z','i','l','i','a','n',' ','S','t','a','n','d','a','r','d',
167 ' ','T','i','m','e','\0'}, 180, 0},
169 {'B','r','a','z','i','l','i','a','n',' ','S','u','m','m','e','r',
170 ' ','T','i','m','e','\0'}, 120, 1},
172 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
173 'd',' ','T','i','m','e','\0'}, 180, 0},
175 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
176 ' ','T','i','m','e','\0'}, 120, 1},
178 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
179 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
181 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
184 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
185 'm','e','\0'}, 0, 1},
187 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
188 'd',' ','T','i','m','e','\0'}, 60, 0},
190 {'W','e','s','t','e','r','n',' ','E','u','r','o','p','e','a','n',' ','S','u','m','m','e','r',' ','T','i','m','e','\0'}, -60, 1},
192 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
193 ' ','T','i','m','e','\0'}, 0, 0},
195 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
198 {'I','r','i','s','h',' ','S','u','m','m','e','r',' ','T','i','m','e','\0'},
201 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
204 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
207 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e','a','n',' ',
208 'T','i','m','e','\0'}, -60, 0},
210 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
211 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
213 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
214 'n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
216 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','D','a','y',
217 'l','i','g','h','t',' ','T','i','m','e','\0'}, -120, 1},
219 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
220 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
222 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
223 ' ','T','i','m','e','\0'}, -180, 1},
225 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
228 {'C','e','n','t','r','a','l',' ','A','f','r','i','c','a','n',' '
229 ,'T','i','m','e','\0'}, -120, 0},
231 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
232 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
234 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
235 'm','e','\0'}, -120, 0},
237 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
238 'i','m','e','\0'}, -180, 0},
240 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
241 'm','e','\0'}, -240, 1},
243 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
246 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
247 'i','m','e','\0'}, -240, 1},
249 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
250 ' ','T','i','m','e','\0'}, -180, 0},
252 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
255 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
258 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
259 'i','m','e','\0'}, -240, 0},
261 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
262 'T','i','m','e',0}, -240, 0 },
264 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
265 'T','i','m','e','\0'}, -300, 1},
267 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
268 'r','d',' ','T','i','m','e','\0'}, -270, 0},
270 {'S','a','m','a','r','a',' ','S','t','a','n','d','a','r','d',' ','T','i',
271 'm','e','\0'}, -270, 1},
273 {'U','r','a','l','s',' ','S','t','a','n','d','a','r','d',
274 ' ','T','i','m','e',' ','(','W','i','n','t','e','r',')','\0'}, -300, 0},
276 {'U','r','a','l','s',' ','D','a','y','l','i','g','h','t',
277 ' ','T','i','m','e',' ','(','S','u','m','m','e','r',')','\0'}, -360, 1},
279 {'O','m','s','k',' ','S','t','a','n','d','a','r','d',
280 ' ','T','i','m','e',' ','(','W','i','n','t','e','r',')','\0'}, -360, 0},
282 {'O','m','s','k',' ','D','a','y','l','i','g','h','t',
283 ' ','T','i','m','e',' ','(','S','u','m','m','e','r',')','\0'}, -420, 1},
285 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
286 ' ','T','i','m','e','\0'}, -300, 0},
288 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
291 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
294 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
295 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
297 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
298 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
300 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
301 ' ','T','i','m','e','\0'}, -360, 0},
303 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
304 'i','m','e','\0'}, -390, 0},
306 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
307 'i','m','e','\0'}, -420, 0},
309 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
310 'd',' ','T','i','m','e',0}, -420, 0},
312 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
313 'd',' ','T','i','m','e','\0'}, -480, 1},
315 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
316 'a','n','d','a','r','d',' ','T','i','m','e',0}, -480, 0},
318 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
321 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
322 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
324 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
325 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
327 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
328 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
330 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','u','m','m','e',
331 'r',' ','T','i','m','e','\0'}, -540, 1},
333 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
336 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
339 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
340 'i','m','e','\0'}, -600, 1},
342 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
343 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
345 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','D','a','y',
346 'l','i','g','h','t',' ','T','i','m','e','\0'}, -630, 1},
348 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
349 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
351 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
352 'a','r','d',' ','T','i','m','e','\0'}, -660, 1},
354 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
355 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
357 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
358 'r','d',' ','T','i','m','e',0}, -600, 0 },
360 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
361 'a','r','d',' ','T','i','m','e',0}, -600, 0},
363 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
364 'r','d',' ','T','i','m','e','\0'}, -660, 1},
366 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
367 'a','n','d','a','r','d',' ','T','i','m','e',0}, -660, 0},
369 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
370 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
372 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
373 'r','d',' ','T','i','m','e','\0'}, -720, 0},
375 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','D','a','y','l','i','g',
376 'h','t',' ','T','i','m','e','\0'}, -780, 1},
378 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
381 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
384 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
385 'a','n','d','a','r','d',' ','T','i','m','e',0 }, -360, 0},
387 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','t','a','n','d','a',
388 'r','d',' ','T','i','m','e','\0'}, -360, 1},
390 {'A','n','a','d','y','r',' ','S','t','a','n','d','a','r','d',' ','T','i',
391 'm','e','\0'}, -720, 1},
393 {'H','o','n','g',' ','K','o','n','g',' ','S','t','a','n','d','a','r','d',
394 ' ','T','i','m','e','\0'}, -480, 0},
396 {'U','r','u','g','u','a','y','a','n',' ','T','i','m','e','\0'}, 180, 0},
398 {'U','r','u','g','u','a','y','a','n',' ','S','u','m','m','e','r',' ','T',
399 'i','m','e','\0'}, 120, 1},
401 {'M','a','l','a','y','s','i','a','n',' ','T','i','m','e','\0'}, -480, 0},
403 {'P','h','i','l','i','p','p','i','n','e',' ','T','i','m','e','\0'}, -480, 0},
405 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','u','m','m','e','r',
406 ' ','T','i','m','e','\0'}, -420, 1},
408 {'U','z','b','e','k','i','s','t','h','a','n',' ','T','i','m','e','\0'}, -300, 0}
411 #define TICKSPERSEC 10000000
412 #define TICKSPERMSEC 10000
413 #define SECSPERDAY 86400
414 #define SECSPERHOUR 3600
415 #define SECSPERMIN 60
416 #define MINSPERHOUR 60
417 #define HOURSPERDAY 24
418 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
419 #define DAYSPERWEEK 7
420 #define EPOCHYEAR 1601
421 #define DAYSPERNORMALYEAR 365
422 #define DAYSPERLEAPYEAR 366
423 #define MONSPERYEAR 12
424 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
425 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
426 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
428 /* 1601 to 1970 is 369 years plus 89 leap days */
429 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
430 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
431 /* 1601 to 1980 is 379 years plus 91 leap days */
432 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
433 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
434 /* max ticks that can be represented as Unix time */
435 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
438 static const int MonthLengths[2][MONSPERYEAR] =
440 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
441 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
444 static inline int IsLeapYear(int Year)
446 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
449 /******************************************************************************
450 * RtlTimeToTimeFields [NTDLL.@]
452 * Convert a time into a TIME_FIELDS structure.
455 * liTime [I] Time to convert.
456 * TimeFields [O] Destination for the converted time.
461 VOID WINAPI RtlTimeToTimeFields(
462 const LARGE_INTEGER *liTime,
463 PTIME_FIELDS TimeFields)
466 long int cleaps, years, yearday, months;
470 /* Extract millisecond from time and convert time into seconds */
471 TimeFields->Milliseconds =
472 (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
473 Time = liTime->QuadPart / TICKSPERSEC;
475 /* The native version of RtlTimeToTimeFields does not take leap seconds
478 /* Split the time into days and seconds within the day */
479 Days = Time / SECSPERDAY;
480 SecondsInDay = Time % SECSPERDAY;
482 /* compute time of day */
483 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
484 SecondsInDay = SecondsInDay % SECSPERHOUR;
485 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
486 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
488 /* compute day of week */
489 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
491 /* compute year, month and day of month. */
492 cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
493 Days += 28188 + cleaps;
494 years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
495 yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
496 months = (64 * yearday) / 1959;
497 /* the result is based on a year starting on March.
498 * To convert take 12 from Januari and Februari and
499 * increase the year by one. */
501 TimeFields->Month = months - 1;
502 TimeFields->Year = years + 1524;
504 TimeFields->Month = months - 13;
505 TimeFields->Year = years + 1525;
507 /* calculation of day of month is based on the wonderful
508 * sequence of INT( n * 30.6): it reproduces the
509 * 31-30-31-30-31-31 month lengths exactly for small n's */
510 TimeFields->Day = yearday - (1959 * months) / 64 ;
514 /******************************************************************************
515 * RtlTimeFieldsToTime [NTDLL.@]
517 * Convert a TIME_FIELDS structure into a time.
520 * ftTimeFields [I] TIME_FIELDS structure to convert.
521 * Time [O] Destination for the converted time.
527 BOOLEAN WINAPI RtlTimeFieldsToTime(
528 PTIME_FIELDS tfTimeFields,
531 int month, year, cleaps, day;
533 /* FIXME: normalize the TIME_FIELDS structure here */
534 /* No, native just returns 0 (error) if the fields are not */
535 if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
536 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
537 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
538 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
539 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
540 tfTimeFields->Day < 1 ||
541 tfTimeFields->Day > MonthLengths
542 [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
543 [ tfTimeFields->Month - 1] ||
544 tfTimeFields->Year < 1601 )
547 /* now calculate a day count from the date
548 * First start counting years from March. This way the leap days
549 * are added at the end of the year, not somewhere in the middle.
550 * Formula's become so much less complicate that way.
551 * To convert: add 12 to the month numbers of Jan and Feb, and
552 * take 1 from the year */
553 if(tfTimeFields->Month < 3) {
554 month = tfTimeFields->Month + 13;
555 year = tfTimeFields->Year - 1;
557 month = tfTimeFields->Month + 1;
558 year = tfTimeFields->Year;
560 cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
561 day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
562 (1959 * month) / 64 + /* months * daypermonth */
563 tfTimeFields->Day - /* day of the month */
564 584817 ; /* zero that on 1601-01-01 */
567 Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
568 tfTimeFields->Hour) * MINSPERHOUR +
569 tfTimeFields->Minute) * SECSPERMIN +
570 tfTimeFields->Second ) * 1000 +
571 tfTimeFields->Milliseconds ) * TICKSPERMSEC;
576 /***********************************************************************
577 * TIME_GetBias [internal]
579 * Helper function calculates delta local time from UTC.
582 * utc [I] The current utc time.
583 * pdaylight [I] Local daylight.
586 * The bias for the current timezone.
588 static int TIME_GetBias(time_t utc, int *pdaylight)
591 static time_t last_utc;
592 static int last_bias;
593 static int last_daylight;
596 RtlEnterCriticalSection( &TIME_GetBias_section );
599 *pdaylight = last_daylight;
603 ptm = localtime(&utc);
604 *pdaylight = last_daylight =
605 ptm->tm_isdst; /* daylight for local timezone */
607 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
609 ret = last_bias = (int)(utc-mktime(ptm));
611 RtlLeaveCriticalSection( &TIME_GetBias_section );
615 /******************************************************************************
616 * RtlLocalTimeToSystemTime [NTDLL.@]
618 * Convert a local time into system time.
621 * LocalTime [I] Local time to convert.
622 * SystemTime [O] Destination for the converted time.
625 * Success: STATUS_SUCCESS.
626 * Failure: An NTSTATUS error code indicating the problem.
628 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
629 PLARGE_INTEGER SystemTime)
634 TRACE("(%p, %p)\n", LocalTime, SystemTime);
637 bias = TIME_GetBias(gmt, &daylight);
639 SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)TICKSPERSEC;
640 return STATUS_SUCCESS;
643 /******************************************************************************
644 * RtlSystemTimeToLocalTime [NTDLL.@]
646 * Convert a system time into a local time.
649 * SystemTime [I] System time to convert.
650 * LocalTime [O] Destination for the converted time.
653 * Success: STATUS_SUCCESS.
654 * Failure: An NTSTATUS error code indicating the problem.
656 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
657 PLARGE_INTEGER LocalTime )
662 TRACE("(%p, %p)\n", SystemTime, LocalTime);
665 bias = TIME_GetBias(gmt, &daylight);
667 LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)TICKSPERSEC;
668 return STATUS_SUCCESS;
671 /******************************************************************************
672 * RtlTimeToSecondsSince1970 [NTDLL.@]
674 * Convert a time into a count of seconds since 1970.
677 * Time [I] Time to convert.
678 * Seconds [O] Destination for the converted time.
682 * Failure: FALSE, if the resulting value will not fit in a DWORD.
684 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
686 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
687 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
688 tmp -= SECS_1601_TO_1970;
689 if (tmp > 0xffffffff) return FALSE;
690 *Seconds = (DWORD)tmp;
694 /******************************************************************************
695 * RtlTimeToSecondsSince1980 [NTDLL.@]
697 * Convert a time into a count of seconds since 1980.
700 * Time [I] Time to convert.
701 * Seconds [O] Destination for the converted time.
705 * Failure: FALSE, if the resulting value will not fit in a DWORD.
707 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
709 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
710 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
711 tmp -= SECS_1601_TO_1980;
712 if (tmp > 0xffffffff) return FALSE;
713 *Seconds = (DWORD)tmp;
717 /******************************************************************************
718 * RtlSecondsSince1970ToTime [NTDLL.@]
720 * Convert a count of seconds since 1970 to a time.
723 * Seconds [I] Time to convert.
724 * Time [O] Destination for the converted time.
729 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
731 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
732 Time->u.LowPart = (DWORD)secs;
733 Time->u.HighPart = (DWORD)(secs >> 32);
736 /******************************************************************************
737 * RtlSecondsSince1980ToTime [NTDLL.@]
739 * Convert a count of seconds since 1980 to a time.
742 * Seconds [I] Time to convert.
743 * Time [O] Destination for the converted time.
748 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
750 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
751 Time->u.LowPart = (DWORD)secs;
752 Time->u.HighPart = (DWORD)(secs >> 32);
755 /******************************************************************************
756 * RtlTimeToElapsedTimeFields [NTDLL.@]
758 * Convert a time to a count of elapsed seconds.
761 * Time [I] Time to convert.
762 * TimeFields [O] Destination for the converted time.
767 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
772 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
773 TimeFields->Milliseconds = rem / TICKSPERMSEC;
775 /* time is now in seconds */
776 TimeFields->Year = 0;
777 TimeFields->Month = 0;
778 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
780 /* rem is now the remaining seconds in the last day */
781 TimeFields->Second = rem % 60;
783 TimeFields->Minute = rem % 60;
784 TimeFields->Hour = rem / 60;
787 /***********************************************************************
788 * NtQuerySystemTime [NTDLL.@]
789 * ZwQuerySystemTime [NTDLL.@]
791 * Get the current system time.
794 * Time [O] Destination for the current system time.
797 * Success: STATUS_SUCCESS.
798 * Failure: An NTSTATUS error code indicating the problem.
800 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
804 gettimeofday( &now, 0 );
805 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
806 Time->QuadPart += now.tv_usec * 10;
807 return STATUS_SUCCESS;
810 /******************************************************************************
811 * NtQueryPerformanceCounter [NTDLL.@]
813 * Note: Windows uses a timer clocked at a multiple of 1193182 Hz. There is a
814 * good number of applications that crash when the returned frequency is either
815 * lower or higher then what Windows gives. Also too high counter values are
816 * reported to give problems.
818 NTSTATUS WINAPI NtQueryPerformanceCounter( PLARGE_INTEGER Counter, PLARGE_INTEGER Frequency )
822 if (!Counter) return STATUS_ACCESS_VIOLATION;
824 /* convert a counter that increments at a rate of 10 MHz
825 * to one of 1.193182 MHz, with some care for arithmetic
826 * overflow and good accuracy (21/176 = 0.11931818) */
827 NtQuerySystemTime( &now );
828 Counter->QuadPart = ((now.QuadPart - server_start_time) * 21) / 176;
829 if (Frequency) Frequency->QuadPart = 1193182;
830 return STATUS_SUCCESS;
834 /******************************************************************************
835 * NtGetTickCount (NTDLL.@)
836 * ZwGetTickCount (NTDLL.@)
838 ULONG WINAPI NtGetTickCount(void)
842 NtQuerySystemTime( &now );
843 return (now.QuadPart - server_start_time) / 10000;
847 /***********************************************************************
848 * TIME_GetTZAsStr [internal]
850 * Helper function that returns the given timezone as a string.
853 * utc [I] The current utc time.
854 * bias [I] The bias of the current timezone.
861 * This could be done with a hash table instead of merely iterating through a
862 * table, however with the small amount of entries (60 or so) I didn't think
865 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
868 struct tm *ptm = localtime(&utc);
871 if (!strftime (psTZName, 7, "%Z", ptm))
874 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
876 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
877 TZ_INFO[i].bias == bias &&
878 TZ_INFO[i].dst == dst
880 return TZ_INFO[i].psTZWindows;
882 FIXME("Can't match system time zone name \"%s\", bias=%d and dst=%d "
883 "to an entry in TZ_INFO. Please add appropriate entry to "
884 "TZ_INFO and submit as patch to wine-patches\n",psTZName,bias,dst);
888 /*** TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
891 static int TIME_GetTimeZoneInfoFromReg(RTL_TIME_ZONE_INFORMATION *tzinfo)
894 KEY_VALUE_PARTIAL_INFORMATION * KpInfo =
895 (KEY_VALUE_PARTIAL_INFORMATION *) buf;
898 OBJECT_ATTRIBUTES attr;
899 UNICODE_STRING nameW;
901 attr.Length = sizeof(attr);
902 attr.RootDirectory = 0;
903 attr.ObjectName = &nameW;
905 attr.SecurityDescriptor = NULL;
906 attr.SecurityQualityOfService = NULL;
907 RtlInitUnicodeString( &nameW, TZInformationKeyW);
908 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) {
910 #define GTZIFR_N( valkey, tofield) \
911 RtlInitUnicodeString( &nameW, valkey );\
912 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
913 sizeof(buf), &size )) { \
914 if( size >= (sizeof((tofield)) + \
915 offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))) { \
917 KpInfo->Data, sizeof(tofield)); \
920 #define GTZIFR_S( valkey, tofield) \
921 RtlInitUnicodeString( &nameW, valkey );\
922 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
923 sizeof(buf), &size )) { \
924 size_t len = (strlenW( (WCHAR*)KpInfo->Data ) + 1) * sizeof(WCHAR); \
925 if (len > sizeof(tofield)) len = sizeof(tofield); \
926 memcpy( tofield, KpInfo->Data, len ); \
927 tofield[(len/sizeof(WCHAR))-1] = 0; \
930 GTZIFR_N( TZStandardStartW, tzinfo->StandardDate)
931 GTZIFR_N( TZDaylightStartW, tzinfo->DaylightDate)
932 GTZIFR_N( TZBiasW, tzinfo->Bias)
933 GTZIFR_N( TZStandardBiasW, tzinfo->StandardBias)
934 GTZIFR_N( TZDaylightBiasW, tzinfo->DaylightBias)
935 GTZIFR_S( TZStandardNameW, tzinfo->StandardName)
936 GTZIFR_S( TZDaylightNameW, tzinfo->DaylightName)
946 /***********************************************************************
947 * RtlQueryTimeZoneInformation [NTDLL.@]
949 * Get information about the current timezone.
952 * tzinfo [O] Destination for the retrieved timezone info.
955 * Success: STATUS_SUCCESS.
956 * Failure: An NTSTATUS error code indicating the problem.
958 NTSTATUS WINAPI RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION *tzinfo)
964 memset(tzinfo, 0, sizeof(RTL_TIME_ZONE_INFORMATION));
966 if( !TIME_GetTimeZoneInfoFromReg(tzinfo)) {
969 bias = TIME_GetBias(gmt, &daylight);
971 tzinfo->Bias = -bias / 60;
972 tzinfo->StandardBias = 0;
973 tzinfo->DaylightBias = -60;
974 tzinfo->StandardName[0]='\0';
975 tzinfo->DaylightName[0]='\0';
976 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
977 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
979 return STATUS_SUCCESS;
982 /***********************************************************************
983 * RtlSetTimeZoneInformation [NTDLL.@]
985 * Set the current time zone information.
988 * tzinfo [I] Timezone information to set.
991 * Success: STATUS_SUCCESS.
992 * Failure: An NTSTATUS error code indicating the problem.
995 NTSTATUS WINAPI RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION *tzinfo )
997 return STATUS_PRIVILEGE_NOT_HELD;
1000 /***********************************************************************
1001 * NtSetSystemTime [NTDLL.@]
1002 * ZwSetSystemTime [NTDLL.@]
1004 * Set the system time.
1007 * NewTime [I] The time to set.
1008 * OldTime [O] Optional destination for the previous system time.
1011 * Success: STATUS_SUCCESS.
1012 * Failure: An NTSTATUS error code indicating the problem.
1014 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
1024 /* Return the old time if necessary */
1026 NtQuerySystemTime(OldTime);
1028 RtlTimeToTimeFields(NewTime, &tf);
1030 /* call gettimeofday to get the current timezone */
1031 gettimeofday(&tv, &tz);
1033 /* get delta local time from utc */
1034 bias = TIME_GetBias(oldsec, &dst);
1036 /* get the number of seconds */
1037 t.tm_sec = tf.Second;
1038 t.tm_min = tf.Minute;
1039 t.tm_hour = tf.Hour;
1041 t.tm_mon = tf.Month - 1;
1042 t.tm_year = tf.Year - 1900;
1045 /* correct for timezone and daylight */
1048 /* set the new time */
1050 tv.tv_usec = tf.Milliseconds * 1000;
1052 /* error and sanity check*/
1053 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
1056 #ifdef HAVE_SETTIMEOFDAY
1057 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
1059 return STATUS_SUCCESS;
1065 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
1066 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
1068 err == -1 ? "No Permission"
1069 : sec == (time_t)-1 ? "" : "is too large." );
1072 return STATUS_INVALID_PARAMETER;
1074 return STATUS_PRIVILEGE_NOT_HELD;
1076 return STATUS_NOT_IMPLEMENTED;