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},
396 {'U','z','b','e','k','i','s','t','h','a','n',' ','T','i','m','e','\0'}, -300, 0}
399 #define TICKSPERSEC 10000000
400 #define TICKSPERMSEC 10000
401 #define SECSPERDAY 86400
402 #define SECSPERHOUR 3600
403 #define SECSPERMIN 60
404 #define MINSPERHOUR 60
405 #define HOURSPERDAY 24
406 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
407 #define DAYSPERWEEK 7
408 #define EPOCHYEAR 1601
409 #define DAYSPERNORMALYEAR 365
410 #define DAYSPERLEAPYEAR 366
411 #define MONSPERYEAR 12
412 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
413 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
414 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
416 /* 1601 to 1970 is 369 years plus 89 leap days */
417 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
418 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
419 /* 1601 to 1980 is 379 years plus 91 leap days */
420 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
421 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
422 /* max ticks that can be represented as Unix time */
423 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
426 static const int MonthLengths
[2][MONSPERYEAR
] =
428 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
429 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
432 static inline int IsLeapYear(int Year
)
434 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0) ? 1 : 0;
437 /***********************************************************************
438 * NTDLL_get_server_abstime
440 * Convert a NTDLL time into an abs_time_t struct to send to the server.
442 void NTDLL_get_server_abstime( abs_time_t
*when
, const LARGE_INTEGER
*timeout
)
446 if (!timeout
) /* infinite timeout */
448 when
->sec
= when
->usec
= 0;
450 else if (timeout
->QuadPart
<= 0) /* relative timeout */
454 if (-timeout
->QuadPart
> (LONGLONG
)INT_MAX
* TICKSPERSEC
)
455 when
->sec
= when
->usec
= INT_MAX
;
458 ULONG sec
= RtlEnlargedUnsignedDivide( -timeout
->QuadPart
, TICKSPERSEC
, &remainder
);
459 gettimeofday( &tv
, 0 );
460 when
->sec
= tv
.tv_sec
+ sec
;
461 if ((when
->usec
= tv
.tv_usec
+ (remainder
/ 10)) >= 1000000)
463 when
->usec
-= 1000000;
466 if (when
->sec
< tv
.tv_sec
) /* overflow */
467 when
->sec
= when
->usec
= INT_MAX
;
470 else /* absolute time */
472 if (timeout
->QuadPart
< TICKS_1601_TO_1970
)
473 when
->sec
= when
->usec
= 0;
474 else if (timeout
->QuadPart
> TICKS_1601_TO_UNIX_MAX
)
475 when
->sec
= when
->usec
= INT_MAX
;
478 when
->sec
= RtlEnlargedUnsignedDivide( timeout
->QuadPart
- TICKS_1601_TO_1970
,
479 TICKSPERSEC
, &remainder
);
480 when
->usec
= remainder
/ 10;
486 /***********************************************************************
487 * NTDLL_from_server_abstime
489 * Convert a timeval struct from the server into an NTDLL time.
491 void NTDLL_from_server_abstime( LARGE_INTEGER
*time
, const abs_time_t
*when
)
493 time
->QuadPart
= when
->sec
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
494 time
->QuadPart
+= when
->usec
* 10;
498 /******************************************************************************
499 * RtlTimeToTimeFields [NTDLL.@]
501 * Convert a time into a TIME_FIELDS structure.
504 * liTime [I] Time to convert.
505 * TimeFields [O] Destination for the converted time.
510 VOID WINAPI
RtlTimeToTimeFields(
511 const LARGE_INTEGER
*liTime
,
512 PTIME_FIELDS TimeFields
)
515 long int cleaps
, years
, yearday
, months
;
519 /* Extract millisecond from time and convert time into seconds */
520 TimeFields
->Milliseconds
=
521 (CSHORT
) (( liTime
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
);
522 Time
= liTime
->QuadPart
/ TICKSPERSEC
;
524 /* The native version of RtlTimeToTimeFields does not take leap seconds
527 /* Split the time into days and seconds within the day */
528 Days
= Time
/ SECSPERDAY
;
529 SecondsInDay
= Time
% SECSPERDAY
;
531 /* compute time of day */
532 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
533 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
534 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
535 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
537 /* compute day of week */
538 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
540 /* compute year, month and day of month. */
541 cleaps
=( 3 * ((4 * Days
+ 1227) / DAYSPERQUADRICENTENNIUM
) + 3 ) / 4;
542 Days
+= 28188 + cleaps
;
543 years
= (20 * Days
- 2442) / (5 * DAYSPERNORMALQUADRENNIUM
);
544 yearday
= Days
- (years
* DAYSPERNORMALQUADRENNIUM
)/4;
545 months
= (64 * yearday
) / 1959;
546 /* the result is based on a year starting on March.
547 * To convert take 12 from Januari and Februari and
548 * increase the year by one. */
550 TimeFields
->Month
= months
- 1;
551 TimeFields
->Year
= years
+ 1524;
553 TimeFields
->Month
= months
- 13;
554 TimeFields
->Year
= years
+ 1525;
556 /* calculation of day of month is based on the wonderful
557 * sequence of INT( n * 30.6): it reproduces the
558 * 31-30-31-30-31-31 month lengths exactly for small n's */
559 TimeFields
->Day
= yearday
- (1959 * months
) / 64 ;
563 /******************************************************************************
564 * RtlTimeFieldsToTime [NTDLL.@]
566 * Convert a TIME_FIELDS structure into a time.
569 * ftTimeFields [I] TIME_FIELDS structure to convert.
570 * Time [O] Destination for the converted time.
576 BOOLEAN WINAPI
RtlTimeFieldsToTime(
577 PTIME_FIELDS tfTimeFields
,
580 int month
, year
, cleaps
, day
;
582 /* FIXME: normalize the TIME_FIELDS structure here */
583 /* No, native just returns 0 (error) if the fields are not */
584 if( tfTimeFields
->Milliseconds
< 0 || tfTimeFields
->Milliseconds
> 999 ||
585 tfTimeFields
->Second
< 0 || tfTimeFields
->Second
> 59 ||
586 tfTimeFields
->Minute
< 0 || tfTimeFields
->Minute
> 59 ||
587 tfTimeFields
->Hour
< 0 || tfTimeFields
->Hour
> 23 ||
588 tfTimeFields
->Month
< 1 || tfTimeFields
->Month
> 12 ||
589 tfTimeFields
->Day
< 1 ||
590 tfTimeFields
->Day
> MonthLengths
591 [ tfTimeFields
->Month
==2 || IsLeapYear(tfTimeFields
->Year
)]
592 [ tfTimeFields
->Month
- 1] ||
593 tfTimeFields
->Year
< 1601 )
596 /* now calculate a day count from the date
597 * First start counting years from March. This way the leap days
598 * are added at the end of the year, not somewhere in the middle.
599 * Formula's become so much less complicate that way.
600 * To convert: add 12 to the month numbers of Jan and Feb, and
601 * take 1 from the year */
602 if(tfTimeFields
->Month
< 3) {
603 month
= tfTimeFields
->Month
+ 13;
604 year
= tfTimeFields
->Year
- 1;
606 month
= tfTimeFields
->Month
+ 1;
607 year
= tfTimeFields
->Year
;
609 cleaps
= (3 * (year
/ 100) + 3) / 4; /* nr of "century leap years"*/
610 day
= (36525 * year
) / 100 - cleaps
+ /* year * dayperyr, corrected */
611 (1959 * month
) / 64 + /* months * daypermonth */
612 tfTimeFields
->Day
- /* day of the month */
613 584817 ; /* zero that on 1601-01-01 */
616 Time
->QuadPart
= (((((LONGLONG
) day
* HOURSPERDAY
+
617 tfTimeFields
->Hour
) * MINSPERHOUR
+
618 tfTimeFields
->Minute
) * SECSPERMIN
+
619 tfTimeFields
->Second
) * 1000 +
620 tfTimeFields
->Milliseconds
) * TICKSPERMSEC
;
625 /***********************************************************************
626 * TIME_GetBias [internal]
628 * Helper function calculates delta local time from UTC.
631 * utc [I] The current utc time.
632 * pdaylight [I] Local daylight.
635 * The bias for the current timezone.
637 static int TIME_GetBias(time_t utc
, int *pdaylight
)
640 static time_t last_utc
;
641 static int last_bias
;
642 static int last_daylight
;
645 RtlEnterCriticalSection( &TIME_GetBias_section
);
648 *pdaylight
= last_daylight
;
652 ptm
= localtime(&utc
);
653 *pdaylight
= last_daylight
=
654 ptm
->tm_isdst
; /* daylight for local timezone */
656 ptm
->tm_isdst
= *pdaylight
; /* use local daylight, not that of Greenwich */
658 ret
= last_bias
= (int)(utc
-mktime(ptm
));
660 RtlLeaveCriticalSection( &TIME_GetBias_section
);
664 /******************************************************************************
665 * RtlLocalTimeToSystemTime [NTDLL.@]
667 * Convert a local time into system time.
670 * LocalTime [I] Local time to convert.
671 * SystemTime [O] Destination for the converted time.
674 * Success: STATUS_SUCCESS.
675 * Failure: An NTSTATUS error code indicating the problem.
677 NTSTATUS WINAPI
RtlLocalTimeToSystemTime( const LARGE_INTEGER
*LocalTime
,
678 PLARGE_INTEGER SystemTime
)
683 TRACE("(%p, %p)\n", LocalTime
, SystemTime
);
686 bias
= TIME_GetBias(gmt
, &daylight
);
688 SystemTime
->QuadPart
= LocalTime
->QuadPart
- bias
* (LONGLONG
)TICKSPERSEC
;
689 return STATUS_SUCCESS
;
692 /******************************************************************************
693 * RtlSystemTimeToLocalTime [NTDLL.@]
695 * Convert a system time into a local time.
698 * SystemTime [I] System time to convert.
699 * LocalTime [O] Destination for the converted time.
702 * Success: STATUS_SUCCESS.
703 * Failure: An NTSTATUS error code indicating the problem.
705 NTSTATUS WINAPI
RtlSystemTimeToLocalTime( const LARGE_INTEGER
*SystemTime
,
706 PLARGE_INTEGER LocalTime
)
711 TRACE("(%p, %p)\n", SystemTime
, LocalTime
);
714 bias
= TIME_GetBias(gmt
, &daylight
);
716 LocalTime
->QuadPart
= SystemTime
->QuadPart
+ bias
* (LONGLONG
)TICKSPERSEC
;
717 return STATUS_SUCCESS
;
720 /******************************************************************************
721 * RtlTimeToSecondsSince1970 [NTDLL.@]
723 * Convert a time into a count of seconds since 1970.
726 * Time [I] Time to convert.
727 * Seconds [O] Destination for the converted time.
731 * Failure: FALSE, if the resulting value will not fit in a DWORD.
733 BOOLEAN WINAPI
RtlTimeToSecondsSince1970( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
735 ULONGLONG tmp
= ((ULONGLONG
)Time
->u
.HighPart
<< 32) | Time
->u
.LowPart
;
736 tmp
= RtlLargeIntegerDivide( tmp
, TICKSPERSEC
, NULL
);
737 tmp
-= SECS_1601_TO_1970
;
738 if (tmp
> 0xffffffff) return FALSE
;
739 *Seconds
= (DWORD
)tmp
;
743 /******************************************************************************
744 * RtlTimeToSecondsSince1980 [NTDLL.@]
746 * Convert a time into a count of seconds since 1980.
749 * Time [I] Time to convert.
750 * Seconds [O] Destination for the converted time.
754 * Failure: FALSE, if the resulting value will not fit in a DWORD.
756 BOOLEAN WINAPI
RtlTimeToSecondsSince1980( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
758 ULONGLONG tmp
= ((ULONGLONG
)Time
->u
.HighPart
<< 32) | Time
->u
.LowPart
;
759 tmp
= RtlLargeIntegerDivide( tmp
, TICKSPERSEC
, NULL
);
760 tmp
-= SECS_1601_TO_1980
;
761 if (tmp
> 0xffffffff) return FALSE
;
762 *Seconds
= (DWORD
)tmp
;
766 /******************************************************************************
767 * RtlSecondsSince1970ToTime [NTDLL.@]
769 * Convert a count of seconds since 1970 to a time.
772 * Seconds [I] Time to convert.
773 * Time [O] Destination for the converted time.
778 void WINAPI
RtlSecondsSince1970ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
780 ULONGLONG secs
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
781 Time
->u
.LowPart
= (DWORD
)secs
;
782 Time
->u
.HighPart
= (DWORD
)(secs
>> 32);
785 /******************************************************************************
786 * RtlSecondsSince1980ToTime [NTDLL.@]
788 * Convert a count of seconds since 1980 to a time.
791 * Seconds [I] Time to convert.
792 * Time [O] Destination for the converted time.
797 void WINAPI
RtlSecondsSince1980ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
799 ULONGLONG secs
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1980
;
800 Time
->u
.LowPart
= (DWORD
)secs
;
801 Time
->u
.HighPart
= (DWORD
)(secs
>> 32);
804 /******************************************************************************
805 * RtlTimeToElapsedTimeFields [NTDLL.@]
807 * Convert a time to a count of elapsed seconds.
810 * Time [I] Time to convert.
811 * TimeFields [O] Destination for the converted time.
816 void WINAPI
RtlTimeToElapsedTimeFields( const LARGE_INTEGER
*Time
, PTIME_FIELDS TimeFields
)
821 time
= RtlExtendedLargeIntegerDivide( Time
->QuadPart
, TICKSPERSEC
, &rem
);
822 TimeFields
->Milliseconds
= rem
/ TICKSPERMSEC
;
824 /* time is now in seconds */
825 TimeFields
->Year
= 0;
826 TimeFields
->Month
= 0;
827 TimeFields
->Day
= RtlExtendedLargeIntegerDivide( time
, SECSPERDAY
, &rem
);
829 /* rem is now the remaining seconds in the last day */
830 TimeFields
->Second
= rem
% 60;
832 TimeFields
->Minute
= rem
% 60;
833 TimeFields
->Hour
= rem
/ 60;
836 /***********************************************************************
837 * NtQuerySystemTime [NTDLL.@]
838 * ZwQuerySystemTime [NTDLL.@]
840 * Get the current system time.
843 * Time [O] Destination for the current system time.
846 * Success: STATUS_SUCCESS.
847 * Failure: An NTSTATUS error code indicating the problem.
849 NTSTATUS WINAPI
NtQuerySystemTime( PLARGE_INTEGER Time
)
853 gettimeofday( &now
, 0 );
854 Time
->QuadPart
= now
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
855 Time
->QuadPart
+= now
.tv_usec
* 10;
856 return STATUS_SUCCESS
;
859 /******************************************************************************
860 * NtQueryPerformanceCounter [NTDLL.@]
862 * Note: Windows uses a timer clocked at a multiple of 1193182 Hz. There is a
863 * good number of applications that crash when the returned frequency is either
864 * lower or higher then what Windows gives. Also too high counter values are
865 * reported to give problems.
867 NTSTATUS WINAPI
NtQueryPerformanceCounter( PLARGE_INTEGER Counter
, PLARGE_INTEGER Frequency
)
871 if (!Counter
) return STATUS_ACCESS_VIOLATION
;
872 gettimeofday( &now
, 0 );
873 /* convert a counter that increments at a rate of 1 MHz
874 * to one of 1.193182 MHz, with some care for arithmetic
875 * overflow ( will not overflow for 5000 years ) and
876 * good accuracy ( 105/88 = 1.19318182) */
877 Counter
->QuadPart
= (((now
.tv_sec
- server_start_time
.sec
) * (ULONGLONG
)1000000 +
878 (now
.tv_usec
- server_start_time
.usec
)) * 105) / 88;
879 if (Frequency
) Frequency
->QuadPart
= 1193182;
880 return STATUS_SUCCESS
;
884 /******************************************************************************
885 * NtGetTickCount (NTDLL.@)
886 * ZwGetTickCount (NTDLL.@)
888 ULONG WINAPI
NtGetTickCount(void)
890 struct timeval current_time
;
892 gettimeofday(¤t_time
, NULL
);
893 return (current_time
.tv_sec
- server_start_time
.sec
) * 1000 +
894 (current_time
.tv_usec
- server_start_time
.usec
) / 1000;
898 /***********************************************************************
899 * TIME_GetTZAsStr [internal]
901 * Helper function that returns the given timezone as a string.
904 * utc [I] The current utc time.
905 * bias [I] The bias of the current timezone.
912 * This could be done with a hash table instead of merely iterating through a
913 * table, however with the small amount of entries (60 or so) I didn't think
916 static const WCHAR
* TIME_GetTZAsStr (time_t utc
, int bias
, int dst
)
919 struct tm
*ptm
= localtime(&utc
);
922 if (!strftime (psTZName
, 7, "%Z", ptm
))
925 for (i
=0; i
<(sizeof(TZ_INFO
) / sizeof(struct tagTZ_INFO
)); i
++)
927 if ( strcmp(TZ_INFO
[i
].psTZFromUnix
, psTZName
) == 0 &&
928 TZ_INFO
[i
].bias
== bias
&&
929 TZ_INFO
[i
].dst
== dst
931 return TZ_INFO
[i
].psTZWindows
;
933 FIXME("Can't match system time zone name \"%s\", bias=%d and dst=%d "
934 "to an entry in TZ_INFO. Please add appropriate entry to "
935 "TZ_INFO and submit as patch to wine-patches\n",psTZName
,bias
,dst
);
939 /*** TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
942 static int TIME_GetTimeZoneInfoFromReg(RTL_TIME_ZONE_INFORMATION
*tzinfo
)
945 KEY_VALUE_PARTIAL_INFORMATION
* KpInfo
=
946 (KEY_VALUE_PARTIAL_INFORMATION
*) buf
;
949 OBJECT_ATTRIBUTES attr
;
950 UNICODE_STRING nameW
;
952 attr
.Length
= sizeof(attr
);
953 attr
.RootDirectory
= 0;
954 attr
.ObjectName
= &nameW
;
956 attr
.SecurityDescriptor
= NULL
;
957 attr
.SecurityQualityOfService
= NULL
;
958 RtlInitUnicodeString( &nameW
, TZInformationKeyW
);
959 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
)) {
961 #define GTZIFR_N( valkey, tofield) \
962 RtlInitUnicodeString( &nameW, valkey );\
963 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
964 sizeof(buf), &size )) { \
965 if( size >= (sizeof((tofield)) + \
966 offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))) { \
968 KpInfo->Data, sizeof(tofield)); \
971 #define GTZIFR_S( valkey, tofield) \
972 RtlInitUnicodeString( &nameW, valkey );\
973 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
974 sizeof(buf), &size )) { \
975 size_t len = (strlenW( (WCHAR*)KpInfo->Data ) + 1) * sizeof(WCHAR); \
976 if (len > sizeof(tofield)) len = sizeof(tofield); \
977 memcpy( tofield, KpInfo->Data, len ); \
978 tofield[(len/sizeof(WCHAR))-1] = 0; \
981 GTZIFR_N( TZStandardStartW
, tzinfo
->StandardDate
)
982 GTZIFR_N( TZDaylightStartW
, tzinfo
->DaylightDate
)
983 GTZIFR_N( TZBiasW
, tzinfo
->Bias
)
984 GTZIFR_N( TZStandardBiasW
, tzinfo
->StandardBias
)
985 GTZIFR_N( TZDaylightBiasW
, tzinfo
->DaylightBias
)
986 GTZIFR_S( TZStandardNameW
, tzinfo
->StandardName
)
987 GTZIFR_S( TZDaylightNameW
, tzinfo
->DaylightName
)
997 /***********************************************************************
998 * RtlQueryTimeZoneInformation [NTDLL.@]
1000 * Get information about the current timezone.
1003 * tzinfo [O] Destination for the retrieved timezone info.
1006 * Success: STATUS_SUCCESS.
1007 * Failure: An NTSTATUS error code indicating the problem.
1009 NTSTATUS WINAPI
RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION
*tzinfo
)
1015 memset(tzinfo
, 0, sizeof(RTL_TIME_ZONE_INFORMATION
));
1017 if( !TIME_GetTimeZoneInfoFromReg(tzinfo
)) {
1020 bias
= TIME_GetBias(gmt
, &daylight
);
1022 tzinfo
->Bias
= -bias
/ 60;
1023 tzinfo
->StandardBias
= 0;
1024 tzinfo
->DaylightBias
= -60;
1025 tzinfo
->StandardName
[0]='\0';
1026 tzinfo
->DaylightName
[0]='\0';
1027 psTZ
= TIME_GetTZAsStr (gmt
, (-bias
/60), daylight
);
1028 if (psTZ
) strcpyW( tzinfo
->StandardName
, psTZ
);
1030 return STATUS_SUCCESS
;
1033 /***********************************************************************
1034 * RtlSetTimeZoneInformation [NTDLL.@]
1036 * Set the current time zone information.
1039 * tzinfo [I] Timezone information to set.
1042 * Success: STATUS_SUCCESS.
1043 * Failure: An NTSTATUS error code indicating the problem.
1046 * Uses the obsolete unix timezone structure and tz_dsttime member.
1048 NTSTATUS WINAPI
RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION
*tzinfo
)
1050 #ifdef HAVE_SETTIMEOFDAY
1053 tz
.tz_minuteswest
= tzinfo
->Bias
;
1055 tz
.tz_dsttime
= DST_NONE
;
1059 if(!settimeofday(NULL
, &tz
))
1060 return STATUS_SUCCESS
;
1062 return STATUS_PRIVILEGE_NOT_HELD
;
1065 /***********************************************************************
1066 * NtSetSystemTime [NTDLL.@]
1067 * ZwSetSystemTime [NTDLL.@]
1069 * Set the system time.
1072 * NewTime [I] The time to set.
1073 * OldTime [O] Optional destination for the previous system time.
1076 * Success: STATUS_SUCCESS.
1077 * Failure: An NTSTATUS error code indicating the problem.
1079 NTSTATUS WINAPI
NtSetSystemTime(const LARGE_INTEGER
*NewTime
, LARGE_INTEGER
*OldTime
)
1089 /* Return the old time if necessary */
1091 NtQuerySystemTime(OldTime
);
1093 RtlTimeToTimeFields(NewTime
, &tf
);
1095 /* call gettimeofday to get the current timezone */
1096 gettimeofday(&tv
, &tz
);
1098 /* get delta local time from utc */
1099 bias
= TIME_GetBias(oldsec
, &dst
);
1101 /* get the number of seconds */
1102 t
.tm_sec
= tf
.Second
;
1103 t
.tm_min
= tf
.Minute
;
1104 t
.tm_hour
= tf
.Hour
;
1106 t
.tm_mon
= tf
.Month
- 1;
1107 t
.tm_year
= tf
.Year
- 1900;
1110 /* correct for timezone and daylight */
1113 /* set the new time */
1115 tv
.tv_usec
= tf
.Milliseconds
* 1000;
1117 /* error and sanity check*/
1118 if(sec
== (time_t)-1 || abs((int)(sec
-oldsec
)) > SETTIME_MAX_ADJUST
) {
1121 #ifdef HAVE_SETTIMEOFDAY
1122 err
= settimeofday(&tv
, NULL
); /* 0 is OK, -1 is error */
1124 return STATUS_SUCCESS
;
1130 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
1131 tf
.Year
, tf
.Month
, tf
.Day
, tf
.Hour
, tf
.Minute
, tf
.Second
,
1133 err
== -1 ? "No Permission"
1134 : sec
== (time_t)-1 ? "" : "is too large." );
1137 return STATUS_INVALID_PARAMETER
;
1139 return STATUS_PRIVILEGE_NOT_HELD
;
1141 return STATUS_NOT_IMPLEMENTED
;