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 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
280 ' ','T','i','m','e','\0'}, -300, 0},
282 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
285 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
288 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
289 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
291 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
292 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
294 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
295 ' ','T','i','m','e','\0'}, -360, 0},
297 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
298 'i','m','e','\0'}, -390, 0},
300 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
301 'i','m','e','\0'}, -420, 0},
303 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
304 'd',' ','T','i','m','e',0}, -420, 0},
306 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
307 'd',' ','T','i','m','e','\0'}, -480, 1},
309 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
310 'a','n','d','a','r','d',' ','T','i','m','e',0}, -480, 0},
312 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
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'}, -540, 1},
318 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
319 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
321 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
322 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
324 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
327 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
330 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
331 'i','m','e','\0'}, -600, 1},
333 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
334 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
336 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','D','a','y',
337 'l','i','g','h','t',' ','T','i','m','e','\0'}, -630, 1},
339 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
340 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
342 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
343 'a','r','d',' ','T','i','m','e','\0'}, -660, 1},
345 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
346 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
348 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
349 'r','d',' ','T','i','m','e',0}, -600, 0 },
351 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
352 'a','r','d',' ','T','i','m','e',0}, -600, 0},
354 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
355 'r','d',' ','T','i','m','e','\0'}, -660, 1},
357 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
358 'a','n','d','a','r','d',' ','T','i','m','e',0}, -660, 0},
360 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
361 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
363 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
364 'r','d',' ','T','i','m','e','\0'}, -720, 0},
366 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','D','a','y','l','i','g',
367 'h','t',' ','T','i','m','e','\0'}, -780, 1},
369 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
372 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
375 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
376 'a','n','d','a','r','d',' ','T','i','m','e',0 }, -360, 0},
378 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','t','a','n','d','a',
379 'r','d',' ','T','i','m','e','\0'}, -360, 1},
381 {'A','n','a','d','y','r',' ','S','t','a','n','d','a','r','d',' ','T','i',
382 'm','e','\0'}, -720, 1},
384 {'H','o','n','g',' ','K','o','n','g',' ','S','t','a','n','d','a','r','d',
385 ' ','T','i','m','e','\0'}, -480, 0},
387 {'U','r','u','g','u','a','y','a','n',' ','T','i','m','e','\0'}, 180, 0},
389 {'M','a','l','a','y','s','i','a','n',' ','T','i','m','e','\0'}, -480, 0},
391 {'P','h','i','l','i','p','p','i','n','e',' ','T','i','m','e','\0'}, -480, 0},
393 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','u','m','m','e','r',
394 ' ','T','i','m','e','\0'}, -480, 1}
397 #define TICKSPERSEC 10000000
398 #define TICKSPERMSEC 10000
399 #define SECSPERDAY 86400
400 #define SECSPERHOUR 3600
401 #define SECSPERMIN 60
402 #define MINSPERHOUR 60
403 #define HOURSPERDAY 24
404 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
405 #define DAYSPERWEEK 7
406 #define EPOCHYEAR 1601
407 #define DAYSPERNORMALYEAR 365
408 #define DAYSPERLEAPYEAR 366
409 #define MONSPERYEAR 12
410 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
411 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
412 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
414 /* 1601 to 1970 is 369 years plus 89 leap days */
415 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
416 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
417 /* 1601 to 1980 is 379 years plus 91 leap days */
418 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
419 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
420 /* max ticks that can be represented as Unix time */
421 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
424 static const int MonthLengths
[2][MONSPERYEAR
] =
426 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
427 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
430 static inline int IsLeapYear(int Year
)
432 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0) ? 1 : 0;
435 /***********************************************************************
436 * NTDLL_get_server_timeout
438 * Convert a NTDLL timeout into a timeval struct to send to the server.
440 void NTDLL_get_server_timeout( abs_time_t
*when
, const LARGE_INTEGER
*timeout
)
444 if (!timeout
) /* infinite timeout */
446 when
->sec
= when
->usec
= 0;
448 else if (timeout
->QuadPart
<= 0) /* relative timeout */
452 if (-timeout
->QuadPart
> (LONGLONG
)INT_MAX
* TICKSPERSEC
)
453 when
->sec
= when
->usec
= INT_MAX
;
456 ULONG sec
= RtlEnlargedUnsignedDivide( -timeout
->QuadPart
, TICKSPERSEC
, &remainder
);
457 gettimeofday( &tv
, 0 );
458 when
->sec
= tv
.tv_sec
+ sec
;
459 if ((when
->usec
= tv
.tv_usec
+ (remainder
/ 10)) >= 1000000)
461 when
->usec
-= 1000000;
464 if (when
->sec
< tv
.tv_sec
) /* overflow */
465 when
->sec
= when
->usec
= INT_MAX
;
468 else /* absolute time */
470 if (timeout
->QuadPart
< TICKS_1601_TO_1970
)
471 when
->sec
= when
->usec
= 0;
472 else if (timeout
->QuadPart
> TICKS_1601_TO_UNIX_MAX
)
473 when
->sec
= when
->usec
= INT_MAX
;
476 when
->sec
= RtlEnlargedUnsignedDivide( timeout
->QuadPart
- TICKS_1601_TO_1970
,
477 TICKSPERSEC
, &remainder
);
478 when
->usec
= remainder
/ 10;
484 /***********************************************************************
485 * NTDLL_from_server_timeout
487 * Convert a timeval struct from the server into an NTDLL timeout.
489 void NTDLL_from_server_timeout( LARGE_INTEGER
*timeout
, const abs_time_t
*when
)
491 timeout
->QuadPart
= when
->sec
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
492 timeout
->QuadPart
+= when
->usec
* 10;
496 /******************************************************************************
497 * RtlTimeToTimeFields [NTDLL.@]
499 * Convert a time into a TIME_FIELDS structure.
502 * liTime [I] Time to convert.
503 * TimeFields [O] Destination for the converted time.
508 VOID WINAPI
RtlTimeToTimeFields(
509 const LARGE_INTEGER
*liTime
,
510 PTIME_FIELDS TimeFields
)
513 long int cleaps
, years
, yearday
, months
;
517 /* Extract millisecond from time and convert time into seconds */
518 TimeFields
->Milliseconds
=
519 (CSHORT
) (( liTime
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
);
520 Time
= liTime
->QuadPart
/ TICKSPERSEC
;
522 /* The native version of RtlTimeToTimeFields does not take leap seconds
525 /* Split the time into days and seconds within the day */
526 Days
= Time
/ SECSPERDAY
;
527 SecondsInDay
= Time
% SECSPERDAY
;
529 /* compute time of day */
530 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
531 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
532 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
533 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
535 /* compute day of week */
536 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
538 /* compute year, month and day of month. */
539 cleaps
=( 3 * ((4 * Days
+ 1227) / DAYSPERQUADRICENTENNIUM
) + 3 ) / 4;
540 Days
+= 28188 + cleaps
;
541 years
= (20 * Days
- 2442) / (5 * DAYSPERNORMALQUADRENNIUM
);
542 yearday
= Days
- (years
* DAYSPERNORMALQUADRENNIUM
)/4;
543 months
= (64 * yearday
) / 1959;
544 /* the result is based on a year starting on March.
545 * To convert take 12 from Januari and Februari and
546 * increase the year by one. */
548 TimeFields
->Month
= months
- 1;
549 TimeFields
->Year
= years
+ 1524;
551 TimeFields
->Month
= months
- 13;
552 TimeFields
->Year
= years
+ 1525;
554 /* calculation of day of month is based on the wonderful
555 * sequence of INT( n * 30.6): it reproduces the
556 * 31-30-31-30-31-31 month lengths exactly for small n's */
557 TimeFields
->Day
= yearday
- (1959 * months
) / 64 ;
561 /******************************************************************************
562 * RtlTimeFieldsToTime [NTDLL.@]
564 * Convert a TIME_FIELDS structure into a time.
567 * ftTimeFields [I] TIME_FIELDS structure to convert.
568 * Time [O] Destination for the converted time.
574 BOOLEAN WINAPI
RtlTimeFieldsToTime(
575 PTIME_FIELDS tfTimeFields
,
578 int month
, year
, cleaps
, day
;
580 /* FIXME: normalize the TIME_FIELDS structure here */
581 /* No, native just returns 0 (error) if the fields are not */
582 if( tfTimeFields
->Milliseconds
< 0 || tfTimeFields
->Milliseconds
> 999 ||
583 tfTimeFields
->Second
< 0 || tfTimeFields
->Second
> 59 ||
584 tfTimeFields
->Minute
< 0 || tfTimeFields
->Minute
> 59 ||
585 tfTimeFields
->Hour
< 0 || tfTimeFields
->Hour
> 23 ||
586 tfTimeFields
->Month
< 1 || tfTimeFields
->Month
> 12 ||
587 tfTimeFields
->Day
< 1 ||
588 tfTimeFields
->Day
> MonthLengths
589 [ tfTimeFields
->Month
==2 || IsLeapYear(tfTimeFields
->Year
)]
590 [ tfTimeFields
->Month
- 1] ||
591 tfTimeFields
->Year
< 1601 )
594 /* now calculate a day count from the date
595 * First start counting years from March. This way the leap days
596 * are added at the end of the year, not somewhere in the middle.
597 * Formula's become so much less complicate that way.
598 * To convert: add 12 to the month numbers of Jan and Feb, and
599 * take 1 from the year */
600 if(tfTimeFields
->Month
< 3) {
601 month
= tfTimeFields
->Month
+ 13;
602 year
= tfTimeFields
->Year
- 1;
604 month
= tfTimeFields
->Month
+ 1;
605 year
= tfTimeFields
->Year
;
607 cleaps
= (3 * (year
/ 100) + 3) / 4; /* nr of "century leap years"*/
608 day
= (36525 * year
) / 100 - cleaps
+ /* year * dayperyr, corrected */
609 (1959 * month
) / 64 + /* months * daypermonth */
610 tfTimeFields
->Day
- /* day of the month */
611 584817 ; /* zero that on 1601-01-01 */
614 Time
->QuadPart
= (((((LONGLONG
) day
* HOURSPERDAY
+
615 tfTimeFields
->Hour
) * MINSPERHOUR
+
616 tfTimeFields
->Minute
) * SECSPERMIN
+
617 tfTimeFields
->Second
) * 1000 +
618 tfTimeFields
->Milliseconds
) * TICKSPERMSEC
;
623 /***********************************************************************
624 * TIME_GetBias [internal]
626 * Helper function calculates delta local time from UTC.
629 * utc [I] The current utc time.
630 * pdaylight [I] Local daylight.
633 * The bias for the current timezone.
635 static int TIME_GetBias(time_t utc
, int *pdaylight
)
638 static time_t last_utc
;
639 static int last_bias
;
640 static int last_daylight
;
643 RtlEnterCriticalSection( &TIME_GetBias_section
);
646 *pdaylight
= last_daylight
;
650 ptm
= localtime(&utc
);
651 *pdaylight
= last_daylight
=
652 ptm
->tm_isdst
; /* daylight for local timezone */
654 ptm
->tm_isdst
= *pdaylight
; /* use local daylight, not that of Greenwich */
656 ret
= last_bias
= (int)(utc
-mktime(ptm
));
658 RtlLeaveCriticalSection( &TIME_GetBias_section
);
662 /******************************************************************************
663 * RtlLocalTimeToSystemTime [NTDLL.@]
665 * Convert a local time into system time.
668 * LocalTime [I] Local time to convert.
669 * SystemTime [O] Destination for the converted time.
672 * Success: STATUS_SUCCESS.
673 * Failure: An NTSTATUS error code indicating the problem.
675 NTSTATUS WINAPI
RtlLocalTimeToSystemTime( const LARGE_INTEGER
*LocalTime
,
676 PLARGE_INTEGER SystemTime
)
681 TRACE("(%p, %p)\n", LocalTime
, SystemTime
);
684 bias
= TIME_GetBias(gmt
, &daylight
);
686 SystemTime
->QuadPart
= LocalTime
->QuadPart
- bias
* (LONGLONG
)TICKSPERSEC
;
687 return STATUS_SUCCESS
;
690 /******************************************************************************
691 * RtlSystemTimeToLocalTime [NTDLL.@]
693 * Convert a system time into a local time.
696 * SystemTime [I] System time to convert.
697 * LocalTime [O] Destination for the converted time.
700 * Success: STATUS_SUCCESS.
701 * Failure: An NTSTATUS error code indicating the problem.
703 NTSTATUS WINAPI
RtlSystemTimeToLocalTime( const LARGE_INTEGER
*SystemTime
,
704 PLARGE_INTEGER LocalTime
)
709 TRACE("(%p, %p)\n", SystemTime
, LocalTime
);
712 bias
= TIME_GetBias(gmt
, &daylight
);
714 LocalTime
->QuadPart
= SystemTime
->QuadPart
+ bias
* (LONGLONG
)TICKSPERSEC
;
715 return STATUS_SUCCESS
;
718 /******************************************************************************
719 * RtlTimeToSecondsSince1970 [NTDLL.@]
721 * Convert a time into a count of seconds since 1970.
724 * Time [I] Time to convert.
725 * Seconds [O] Destination for the converted time.
729 * Failure: FALSE, if the resulting value will not fit in a DWORD.
731 BOOLEAN WINAPI
RtlTimeToSecondsSince1970( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
733 ULONGLONG tmp
= ((ULONGLONG
)Time
->u
.HighPart
<< 32) | Time
->u
.LowPart
;
734 tmp
= RtlLargeIntegerDivide( tmp
, TICKSPERSEC
, NULL
);
735 tmp
-= SECS_1601_TO_1970
;
736 if (tmp
> 0xffffffff) return FALSE
;
737 *Seconds
= (DWORD
)tmp
;
741 /******************************************************************************
742 * RtlTimeToSecondsSince1980 [NTDLL.@]
744 * Convert a time into a count of seconds since 1980.
747 * Time [I] Time to convert.
748 * Seconds [O] Destination for the converted time.
752 * Failure: FALSE, if the resulting value will not fit in a DWORD.
754 BOOLEAN WINAPI
RtlTimeToSecondsSince1980( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
756 ULONGLONG tmp
= ((ULONGLONG
)Time
->u
.HighPart
<< 32) | Time
->u
.LowPart
;
757 tmp
= RtlLargeIntegerDivide( tmp
, TICKSPERSEC
, NULL
);
758 tmp
-= SECS_1601_TO_1980
;
759 if (tmp
> 0xffffffff) return FALSE
;
760 *Seconds
= (DWORD
)tmp
;
764 /******************************************************************************
765 * RtlSecondsSince1970ToTime [NTDLL.@]
767 * Convert a count of seconds since 1970 to a time.
770 * Seconds [I] Time to convert.
771 * Time [O] Destination for the converted time.
776 void WINAPI
RtlSecondsSince1970ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
778 ULONGLONG secs
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
779 Time
->u
.LowPart
= (DWORD
)secs
;
780 Time
->u
.HighPart
= (DWORD
)(secs
>> 32);
783 /******************************************************************************
784 * RtlSecondsSince1980ToTime [NTDLL.@]
786 * Convert a count of seconds since 1980 to a time.
789 * Seconds [I] Time to convert.
790 * Time [O] Destination for the converted time.
795 void WINAPI
RtlSecondsSince1980ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
797 ULONGLONG secs
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1980
;
798 Time
->u
.LowPart
= (DWORD
)secs
;
799 Time
->u
.HighPart
= (DWORD
)(secs
>> 32);
802 /******************************************************************************
803 * RtlTimeToElapsedTimeFields [NTDLL.@]
805 * Convert a time to a count of elapsed seconds.
808 * Time [I] Time to convert.
809 * TimeFields [O] Destination for the converted time.
814 void WINAPI
RtlTimeToElapsedTimeFields( const LARGE_INTEGER
*Time
, PTIME_FIELDS TimeFields
)
819 time
= RtlExtendedLargeIntegerDivide( Time
->QuadPart
, TICKSPERSEC
, &rem
);
820 TimeFields
->Milliseconds
= rem
/ TICKSPERMSEC
;
822 /* time is now in seconds */
823 TimeFields
->Year
= 0;
824 TimeFields
->Month
= 0;
825 TimeFields
->Day
= RtlExtendedLargeIntegerDivide( time
, SECSPERDAY
, &rem
);
827 /* rem is now the remaining seconds in the last day */
828 TimeFields
->Second
= rem
% 60;
830 TimeFields
->Minute
= rem
% 60;
831 TimeFields
->Hour
= rem
/ 60;
834 /***********************************************************************
835 * NtQuerySystemTime [NTDLL.@]
836 * ZwQuerySystemTime [NTDLL.@]
838 * Get the current system time.
841 * Time [O] Destination for the current system time.
844 * Success: STATUS_SUCCESS.
845 * Failure: An NTSTATUS error code indicating the problem.
847 NTSTATUS WINAPI
NtQuerySystemTime( PLARGE_INTEGER Time
)
851 gettimeofday( &now
, 0 );
852 Time
->QuadPart
= now
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
853 Time
->QuadPart
+= now
.tv_usec
* 10;
854 return STATUS_SUCCESS
;
857 /******************************************************************************
858 * NtQueryPerformanceCounter [NTDLL.@]
860 * Note: Windows uses a timer clocked at a multiple of 1193182 Hz. There is a
861 * good number of applications that crash when the returned frequency is either
862 * lower or higher then what Windows gives. Also too high counter values are
863 * reported to give problems.
865 NTSTATUS WINAPI
NtQueryPerformanceCounter( PLARGE_INTEGER Counter
, PLARGE_INTEGER Frequency
)
869 if (!Counter
) return STATUS_ACCESS_VIOLATION
;
870 gettimeofday( &now
, 0 );
871 /* convert a counter that increments at a rate of 1 MHz
872 * to one of 1.193182 MHz, with some care for arithmetic
873 * overflow ( will not overflow for 5000 years ) and
874 * good accuracy ( 105/88 = 1.19318182) */
875 Counter
->QuadPart
= (((now
.tv_sec
- server_start_time
) * (ULONGLONG
)1000000 + now
.tv_usec
) * 105) / 88;
876 if (Frequency
) Frequency
->QuadPart
= 1193182;
877 return STATUS_SUCCESS
;
881 /******************************************************************************
882 * NtGetTickCount (NTDLL.@)
883 * ZwGetTickCount (NTDLL.@)
885 ULONG WINAPI
NtGetTickCount(void)
887 struct timeval current_time
;
889 gettimeofday(¤t_time
, NULL
);
890 return (current_time
.tv_sec
- server_start_time
)*1000 + current_time
.tv_usec
/1000;
894 /***********************************************************************
895 * TIME_GetTZAsStr [internal]
897 * Helper function that returns the given timezone as a string.
900 * utc [I] The current utc time.
901 * bias [I] The bias of the current timezone.
908 * This could be done with a hash table instead of merely iterating through a
909 * table, however with the small amount of entries (60 or so) I didn't think
912 static const WCHAR
* TIME_GetTZAsStr (time_t utc
, int bias
, int dst
)
915 struct tm
*ptm
= localtime(&utc
);
918 if (!strftime (psTZName
, 7, "%Z", ptm
))
921 for (i
=0; i
<(sizeof(TZ_INFO
) / sizeof(struct tagTZ_INFO
)); i
++)
923 if ( strcmp(TZ_INFO
[i
].psTZFromUnix
, psTZName
) == 0 &&
924 TZ_INFO
[i
].bias
== bias
&&
925 TZ_INFO
[i
].dst
== dst
927 return TZ_INFO
[i
].psTZWindows
;
929 FIXME("Can't match system time zone name \"%s\", bias=%d and dst=%d "
930 "to an entry in TZ_INFO. Please add appropriate entry to "
931 "TZ_INFO and submit as patch to wine-patches\n",psTZName
,bias
,dst
);
935 /*** TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
938 static int TIME_GetTimeZoneInfoFromReg(RTL_TIME_ZONE_INFORMATION
*tzinfo
)
941 KEY_VALUE_PARTIAL_INFORMATION
* KpInfo
=
942 (KEY_VALUE_PARTIAL_INFORMATION
*) buf
;
945 OBJECT_ATTRIBUTES attr
;
946 UNICODE_STRING nameW
;
948 attr
.Length
= sizeof(attr
);
949 attr
.RootDirectory
= 0;
950 attr
.ObjectName
= &nameW
;
952 attr
.SecurityDescriptor
= NULL
;
953 attr
.SecurityQualityOfService
= NULL
;
954 RtlInitUnicodeString( &nameW
, TZInformationKeyW
);
955 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) {
957 #define GTZIFR_N( valkey, tofield) \
958 RtlInitUnicodeString( &nameW, valkey );\
959 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
960 sizeof(buf), &size )) { \
961 if( size >= (sizeof((tofield)) + \
962 offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))) { \
964 KpInfo->Data, sizeof(tofield)); \
967 #define GTZIFR_S( valkey, tofield) \
968 RtlInitUnicodeString( &nameW, valkey );\
969 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
970 sizeof(buf), &size )) { \
971 size_t len = (strlenW( (WCHAR*)KpInfo->Data ) + 1) * sizeof(WCHAR); \
972 if (len > sizeof(tofield)) len = sizeof(tofield); \
973 memcpy( tofield, KpInfo->Data, len ); \
974 tofield[(len/sizeof(WCHAR))-1] = 0; \
977 GTZIFR_N( TZStandardStartW
, tzinfo
->StandardDate
)
978 GTZIFR_N( TZDaylightStartW
, tzinfo
->DaylightDate
)
979 GTZIFR_N( TZBiasW
, tzinfo
->Bias
)
980 GTZIFR_N( TZStandardBiasW
, tzinfo
->StandardBias
)
981 GTZIFR_N( TZDaylightBiasW
, tzinfo
->DaylightBias
)
982 GTZIFR_S( TZStandardNameW
, tzinfo
->StandardName
)
983 GTZIFR_S( TZDaylightNameW
, tzinfo
->DaylightName
)
993 /***********************************************************************
994 * RtlQueryTimeZoneInformation [NTDLL.@]
996 * Get information about the current timezone.
999 * tzinfo [O] Destination for the retrieved timezone info.
1002 * Success: STATUS_SUCCESS.
1003 * Failure: An NTSTATUS error code indicating the problem.
1005 NTSTATUS WINAPI
RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION
*tzinfo
)
1011 memset(tzinfo
, 0, sizeof(RTL_TIME_ZONE_INFORMATION
));
1013 if( !TIME_GetTimeZoneInfoFromReg(tzinfo
)) {
1016 bias
= TIME_GetBias(gmt
, &daylight
);
1018 tzinfo
->Bias
= -bias
/ 60;
1019 tzinfo
->StandardBias
= 0;
1020 tzinfo
->DaylightBias
= -60;
1021 tzinfo
->StandardName
[0]='\0';
1022 tzinfo
->DaylightName
[0]='\0';
1023 psTZ
= TIME_GetTZAsStr (gmt
, (-bias
/60), daylight
);
1024 if (psTZ
) strcpyW( tzinfo
->StandardName
, psTZ
);
1026 return STATUS_SUCCESS
;
1029 /***********************************************************************
1030 * RtlSetTimeZoneInformation [NTDLL.@]
1032 * Set the current time zone information.
1035 * tzinfo [I] Timezone information to set.
1038 * Success: STATUS_SUCCESS.
1039 * Failure: An NTSTATUS error code indicating the problem.
1042 * Uses the obsolete unix timezone structure and tz_dsttime member.
1044 NTSTATUS WINAPI
RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION
*tzinfo
)
1046 #ifdef HAVE_SETTIMEOFDAY
1049 tz
.tz_minuteswest
= tzinfo
->Bias
;
1051 tz
.tz_dsttime
= DST_NONE
;
1055 if(!settimeofday(NULL
, &tz
))
1056 return STATUS_SUCCESS
;
1058 return STATUS_PRIVILEGE_NOT_HELD
;
1061 /***********************************************************************
1062 * NtSetSystemTime [NTDLL.@]
1063 * ZwSetSystemTime [NTDLL.@]
1065 * Set the system time.
1068 * NewTime [I] The time to set.
1069 * OldTime [O] Optional destination for the previous system time.
1072 * Success: STATUS_SUCCESS.
1073 * Failure: An NTSTATUS error code indicating the problem.
1075 NTSTATUS WINAPI
NtSetSystemTime(const LARGE_INTEGER
*NewTime
, LARGE_INTEGER
*OldTime
)
1085 /* Return the old time if necessary */
1087 NtQuerySystemTime(OldTime
);
1089 RtlTimeToTimeFields(NewTime
, &tf
);
1091 /* call gettimeofday to get the current timezone */
1092 gettimeofday(&tv
, &tz
);
1094 /* get delta local time from utc */
1095 bias
= TIME_GetBias(oldsec
, &dst
);
1097 /* get the number of seconds */
1098 t
.tm_sec
= tf
.Second
;
1099 t
.tm_min
= tf
.Minute
;
1100 t
.tm_hour
= tf
.Hour
;
1102 t
.tm_mon
= tf
.Month
- 1;
1103 t
.tm_year
= tf
.Year
- 1900;
1106 /* correct for timezone and daylight */
1109 /* set the new time */
1111 tv
.tv_usec
= tf
.Milliseconds
* 1000;
1113 /* error and sanity check*/
1114 if(sec
== (time_t)-1 || abs((int)(sec
-oldsec
)) > SETTIME_MAX_ADJUST
) {
1117 #ifdef HAVE_SETTIMEOFDAY
1118 err
= settimeofday(&tv
, NULL
); /* 0 is OK, -1 is error */
1120 return STATUS_SUCCESS
;
1126 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
1127 tf
.Year
, tf
.Month
, tf
.Day
, tf
.Hour
, tf
.Minute
, tf
.Second
,
1129 err
== -1 ? "No Permission"
1130 : sec
== (time_t)-1 ? "" : "is too large." );
1133 return STATUS_INVALID_PARAMETER
;
1135 return STATUS_PRIVILEGE_NOT_HELD
;
1137 return STATUS_NOT_IMPLEMENTED
;