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