regedit: Merge getRegClass() and getRegKeyName() to form parseKeyName().
[wine/gsoc_dplay.git] / dlls / ntdll / time.c
blob1159a89bcbd8d63afa3e13c23cac9da3e3e656e5
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #include "windef.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 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)
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 {"AKST",
106 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ',
107 'T','i','m','e',0}, 540, 0 },
108 {"AKDT",
109 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
110 'i','m','e','\0'}, 480, 1},
111 {"PST",
112 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
113 'i','m','e','\0'}, 480, 0},
114 {"PDT",
115 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
116 'i','m','e','\0'}, 420, 1},
117 {"MST",
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},
120 {"MDT",
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},
123 {"CST",
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},
126 {"CDT",
127 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
128 'i','m','e','\0'}, 300, 1},
129 {"COT",
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},
132 {"PET",
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 },
135 {"EDT",
136 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
137 'i','m','e','\0'}, 240, 1},
138 {"EST",
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},
141 {"ECT",
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},
144 {"ADT",
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},
147 {"VET",
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},
150 {"CLT",
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},
153 {"CLST",
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},
156 {"AST",
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 },
159 {"NDT",
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},
162 {"NST",
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 },
165 {"BRT",
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},
168 {"BRST",
169 {'B','r','a','z','i','l','i','a','n',' ','S','u','m','m','e','r',
170 ' ','T','i','m','e','\0'}, 120, 1},
171 {"ART",
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},
174 {"WGST",
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},
177 {"GST",
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},
180 {"AZOT",
181 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
182 'm','e',0}, 60, 0},
183 {"AZOST",
184 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
185 'm','e','\0'}, 0, 1},
186 {"CVT",
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},
189 {"WEST",
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},
191 {"WET",
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},
194 {"BST",
195 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
196 -60, 1},
197 {"IST",
198 {'I','r','i','s','h',' ','S','u','m','m','e','r',' ','T','i','m','e','\0'},
199 -60, 1},
200 {"GMT",
201 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
202 0, 0},
203 {"UTC",
204 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
205 0, 0},
206 {"CET",
207 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e','a','n',' ',
208 'T','i','m','e','\0'}, -60, 0},
209 {"CEST",
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},
212 {"MET",
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},
215 {"MEST",
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},
218 {"WAT",
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},
221 {"EEST",
222 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
223 ' ','T','i','m','e','\0'}, -180, 1},
224 {"EET",
225 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
226 'e','\0'}, -120, 0},
227 {"CAT",
228 {'C','e','n','t','r','a','l',' ','A','f','r','i','c','a','n',' '
229 ,'T','i','m','e','\0'}, -120, 0},
230 {"SAST",
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},
233 {"IST",
234 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
235 'm','e','\0'}, -120, 0},
236 {"IDT",
237 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
238 'm','e','\0'}, -180, 1},
239 {"MSK",
240 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
241 'i','m','e','\0'}, -180, 0},
242 {"ADT",
243 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
244 'm','e','\0'}, -240, 1},
245 {"AST",
246 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
247 '\0'}, -180, 0},
248 {"MSD",
249 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
250 'i','m','e','\0'}, -240, 1},
251 {"EAT",
252 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
253 ' ','T','i','m','e','\0'}, -180, 0},
254 {"IRST",
255 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
256 0},-210, 0 },
257 {"IRST",
258 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
259 '\0'}, -270, 1},
260 {"GST",
261 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
262 'i','m','e','\0'}, -240, 0},
263 {"AZT",
264 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
265 'T','i','m','e',0}, -240, 0 },
266 {"AZST",
267 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
268 'T','i','m','e','\0'}, -300, 1},
269 {"AFT",
270 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
271 'r','d',' ','T','i','m','e','\0'}, -270, 0},
272 {"SAMT",
273 {'S','a','m','a','r','a',' ','S','t','a','n','d','a','r','d',' ','T','i',
274 'm','e','\0'}, -270, 1},
275 {"YEKT",
276 {'U','r','a','l','s',' ','S','t','a','n','d','a','r','d',
277 ' ','T','i','m','e',' ','(','W','i','n','t','e','r',')','\0'}, -300, 0},
278 {"YEKST",
279 {'U','r','a','l','s',' ','D','a','y','l','i','g','h','t',
280 ' ','T','i','m','e',' ','(','S','u','m','m','e','r',')','\0'}, -360, 1},
281 {"OMST",
282 {'O','m','s','k',' ','S','t','a','n','d','a','r','d',
283 ' ','T','i','m','e',' ','(','W','i','n','t','e','r',')','\0'}, -360, 0},
284 {"OMSST",
285 {'O','m','s','k',' ','D','a','y','l','i','g','h','t',
286 ' ','T','i','m','e',' ','(','S','u','m','m','e','r',')','\0'}, -420, 1},
287 {"PKT",
288 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
289 ' ','T','i','m','e','\0'}, -300, 0},
290 {"IST",
291 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
292 'e','\0'}, -330, 0},
293 {"NPT",
294 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
295 'e','\0'}, -345, 0},
296 {"ALMST",
297 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
298 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
299 {"BDT",
300 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
301 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
302 {"LKT",
303 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
304 ' ','T','i','m','e','\0'}, -360, 0},
305 {"MMT",
306 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
307 'i','m','e','\0'}, -390, 0},
308 {"ICT",
309 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
310 'i','m','e','\0'}, -420, 0},
311 {"KRAT",
312 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
313 'd',' ','T','i','m','e',0}, -420, 0},
314 {"KRAST",
315 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
316 'd',' ','T','i','m','e','\0'}, -480, 1},
317 {"IRKT",
318 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
319 'a','n','d','a','r','d',' ','T','i','m','e',0}, -480, 0},
320 {"CST",
321 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
322 'e','\0'}, -480, 0},
323 {"IRKST",
324 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
325 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
326 {"SGT",
327 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
328 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
329 {"WST",
330 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
331 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
332 {"WST",
333 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','u','m','m','e',
334 'r',' ','T','i','m','e','\0'}, -540, 1},
335 {"JST",
336 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
337 'e','\0'}, -540, 0},
338 {"KST",
339 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
340 'e','\0'}, -540, 0},
341 {"YAKST",
342 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
343 'i','m','e','\0'}, -600, 1},
344 {"CST",
345 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
346 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
347 {"CST",
348 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','D','a','y',
349 'l','i','g','h','t',' ','T','i','m','e','\0'}, -630, 1},
350 {"EST",
351 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
352 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
353 {"EST",
354 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
355 'a','r','d',' ','T','i','m','e','\0'}, -660, 1},
356 {"GST",
357 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
358 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
359 {"VLAT",
360 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
361 'r','d',' ','T','i','m','e',0}, -600, 0 },
362 {"ChST",
363 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
364 'a','r','d',' ','T','i','m','e',0}, -600, 0},
365 {"VLAST",
366 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
367 'r','d',' ','T','i','m','e','\0'}, -660, 1},
368 {"MAGT",
369 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
370 'a','n','d','a','r','d',' ','T','i','m','e',0}, -660, 0},
371 {"MAGST",
372 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
373 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
374 {"NZST",
375 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
376 'r','d',' ','T','i','m','e','\0'}, -720, 0},
377 {"NZDT",
378 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','D','a','y','l','i','g',
379 'h','t',' ','T','i','m','e','\0'}, -780, 1},
380 {"FJT",
381 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
382 '\0'}, -720, 0},
383 {"TOT",
384 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
385 'e','\0'}, -780, 0},
386 {"NOVT",
387 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
388 'a','n','d','a','r','d',' ','T','i','m','e',0 }, -360, 0},
389 {"NOVT",
390 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','t','a','n','d','a',
391 'r','d',' ','T','i','m','e','\0'}, -360, 1},
392 {"ANAT",
393 {'A','n','a','d','y','r',' ','S','t','a','n','d','a','r','d',' ','T','i',
394 'm','e','\0'}, -720, 1},
395 {"HKT",
396 {'H','o','n','g',' ','K','o','n','g',' ','S','t','a','n','d','a','r','d',
397 ' ','T','i','m','e','\0'}, -480, 0},
398 {"UYT",
399 {'U','r','u','g','u','a','y','a','n',' ','T','i','m','e','\0'}, 180, 0},
400 {"UYST",
401 {'U','r','u','g','u','a','y','a','n',' ','S','u','m','m','e','r',' ','T',
402 'i','m','e','\0'}, 120, 1},
403 {"MYT",
404 {'M','a','l','a','y','s','i','a','n',' ','T','i','m','e','\0'}, -480, 0},
405 {"PHT",
406 {'P','h','i','l','i','p','p','i','n','e',' ','T','i','m','e','\0'}, -480, 0},
407 {"NOVST",
408 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','u','m','m','e','r',
409 ' ','T','i','m','e','\0'}, -420, 1},
410 {"UZT",
411 {'U','z','b','e','k','i','s','t','h','a','n',' ','T','i','m','e','\0'}, -300, 0}
414 #define TICKSPERSEC 10000000
415 #define TICKSPERMSEC 10000
416 #define SECSPERDAY 86400
417 #define SECSPERHOUR 3600
418 #define SECSPERMIN 60
419 #define MINSPERHOUR 60
420 #define HOURSPERDAY 24
421 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
422 #define DAYSPERWEEK 7
423 #define EPOCHYEAR 1601
424 #define DAYSPERNORMALYEAR 365
425 #define DAYSPERLEAPYEAR 366
426 #define MONSPERYEAR 12
427 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
428 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
429 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
431 /* 1601 to 1970 is 369 years plus 89 leap days */
432 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
433 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
434 /* 1601 to 1980 is 379 years plus 91 leap days */
435 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
436 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
437 /* max ticks that can be represented as Unix time */
438 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
441 static const int MonthLengths[2][MONSPERYEAR] =
443 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
444 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
447 static inline int IsLeapYear(int Year)
449 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
452 /******************************************************************************
453 * RtlTimeToTimeFields [NTDLL.@]
455 * Convert a time into a TIME_FIELDS structure.
457 * PARAMS
458 * liTime [I] Time to convert.
459 * TimeFields [O] Destination for the converted time.
461 * RETURNS
462 * Nothing.
464 VOID WINAPI RtlTimeToTimeFields(
465 const LARGE_INTEGER *liTime,
466 PTIME_FIELDS TimeFields)
468 int SecondsInDay;
469 long int cleaps, years, yearday, months;
470 long int Days;
471 LONGLONG Time;
473 /* Extract millisecond from time and convert time into seconds */
474 TimeFields->Milliseconds =
475 (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
476 Time = liTime->QuadPart / TICKSPERSEC;
478 /* The native version of RtlTimeToTimeFields does not take leap seconds
479 * into account */
481 /* Split the time into days and seconds within the day */
482 Days = Time / SECSPERDAY;
483 SecondsInDay = Time % SECSPERDAY;
485 /* compute time of day */
486 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
487 SecondsInDay = SecondsInDay % SECSPERHOUR;
488 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
489 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
491 /* compute day of week */
492 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
494 /* compute year, month and day of month. */
495 cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
496 Days += 28188 + cleaps;
497 years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
498 yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
499 months = (64 * yearday) / 1959;
500 /* the result is based on a year starting on March.
501 * To convert take 12 from Januari and Februari and
502 * increase the year by one. */
503 if( months < 14 ) {
504 TimeFields->Month = months - 1;
505 TimeFields->Year = years + 1524;
506 } else {
507 TimeFields->Month = months - 13;
508 TimeFields->Year = years + 1525;
510 /* calculation of day of month is based on the wonderful
511 * sequence of INT( n * 30.6): it reproduces the
512 * 31-30-31-30-31-31 month lengths exactly for small n's */
513 TimeFields->Day = yearday - (1959 * months) / 64 ;
514 return;
517 /******************************************************************************
518 * RtlTimeFieldsToTime [NTDLL.@]
520 * Convert a TIME_FIELDS structure into a time.
522 * PARAMS
523 * ftTimeFields [I] TIME_FIELDS structure to convert.
524 * Time [O] Destination for the converted time.
526 * RETURNS
527 * Success: TRUE.
528 * Failure: FALSE.
530 BOOLEAN WINAPI RtlTimeFieldsToTime(
531 PTIME_FIELDS tfTimeFields,
532 PLARGE_INTEGER Time)
534 int month, year, cleaps, day;
536 /* FIXME: normalize the TIME_FIELDS structure here */
537 /* No, native just returns 0 (error) if the fields are not */
538 if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
539 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
540 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
541 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
542 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
543 tfTimeFields->Day < 1 ||
544 tfTimeFields->Day > MonthLengths
545 [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
546 [ tfTimeFields->Month - 1] ||
547 tfTimeFields->Year < 1601 )
548 return FALSE;
550 /* now calculate a day count from the date
551 * First start counting years from March. This way the leap days
552 * are added at the end of the year, not somewhere in the middle.
553 * Formula's become so much less complicate that way.
554 * To convert: add 12 to the month numbers of Jan and Feb, and
555 * take 1 from the year */
556 if(tfTimeFields->Month < 3) {
557 month = tfTimeFields->Month + 13;
558 year = tfTimeFields->Year - 1;
559 } else {
560 month = tfTimeFields->Month + 1;
561 year = tfTimeFields->Year;
563 cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
564 day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
565 (1959 * month) / 64 + /* months * daypermonth */
566 tfTimeFields->Day - /* day of the month */
567 584817 ; /* zero that on 1601-01-01 */
568 /* done */
570 Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
571 tfTimeFields->Hour) * MINSPERHOUR +
572 tfTimeFields->Minute) * SECSPERMIN +
573 tfTimeFields->Second ) * 1000 +
574 tfTimeFields->Milliseconds ) * TICKSPERMSEC;
576 return TRUE;
579 /***********************************************************************
580 * TIME_GetBias [internal]
582 * Helper function calculates delta local time from UTC.
584 * PARAMS
585 * utc [I] The current utc time.
586 * pdaylight [I] Local daylight.
588 * RETURNS
589 * The bias for the current timezone.
591 static int TIME_GetBias(time_t utc, int *pdaylight)
593 struct tm *ptm;
594 static time_t last_utc;
595 static int last_bias;
596 static int last_daylight;
597 int ret;
599 RtlEnterCriticalSection( &TIME_GetBias_section );
600 if(utc == last_utc)
602 *pdaylight = last_daylight;
603 ret = last_bias;
604 } else
606 ptm = localtime(&utc);
607 *pdaylight = last_daylight =
608 ptm->tm_isdst; /* daylight for local timezone */
609 ptm = gmtime(&utc);
610 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
611 last_utc = utc;
612 ret = last_bias = (int)(utc-mktime(ptm));
614 RtlLeaveCriticalSection( &TIME_GetBias_section );
615 return ret;
618 /******************************************************************************
619 * RtlLocalTimeToSystemTime [NTDLL.@]
621 * Convert a local time into system time.
623 * PARAMS
624 * LocalTime [I] Local time to convert.
625 * SystemTime [O] Destination for the converted time.
627 * RETURNS
628 * Success: STATUS_SUCCESS.
629 * Failure: An NTSTATUS error code indicating the problem.
631 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
632 PLARGE_INTEGER SystemTime)
634 time_t gmt;
635 int bias, daylight;
637 TRACE("(%p, %p)\n", LocalTime, SystemTime);
639 gmt = time(NULL);
640 bias = TIME_GetBias(gmt, &daylight);
642 SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)TICKSPERSEC;
643 return STATUS_SUCCESS;
646 /******************************************************************************
647 * RtlSystemTimeToLocalTime [NTDLL.@]
649 * Convert a system time into a local time.
651 * PARAMS
652 * SystemTime [I] System time to convert.
653 * LocalTime [O] Destination for the converted time.
655 * RETURNS
656 * Success: STATUS_SUCCESS.
657 * Failure: An NTSTATUS error code indicating the problem.
659 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
660 PLARGE_INTEGER LocalTime )
662 time_t gmt;
663 int bias, daylight;
665 TRACE("(%p, %p)\n", SystemTime, LocalTime);
667 gmt = time(NULL);
668 bias = TIME_GetBias(gmt, &daylight);
670 LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)TICKSPERSEC;
671 return STATUS_SUCCESS;
674 /******************************************************************************
675 * RtlTimeToSecondsSince1970 [NTDLL.@]
677 * Convert a time into a count of seconds since 1970.
679 * PARAMS
680 * Time [I] Time to convert.
681 * Seconds [O] Destination for the converted time.
683 * RETURNS
684 * Success: TRUE.
685 * Failure: FALSE, if the resulting value will not fit in a DWORD.
687 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
689 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
690 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
691 tmp -= SECS_1601_TO_1970;
692 if (tmp > 0xffffffff) return FALSE;
693 *Seconds = (DWORD)tmp;
694 return TRUE;
697 /******************************************************************************
698 * RtlTimeToSecondsSince1980 [NTDLL.@]
700 * Convert a time into a count of seconds since 1980.
702 * PARAMS
703 * Time [I] Time to convert.
704 * Seconds [O] Destination for the converted time.
706 * RETURNS
707 * Success: TRUE.
708 * Failure: FALSE, if the resulting value will not fit in a DWORD.
710 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
712 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
713 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
714 tmp -= SECS_1601_TO_1980;
715 if (tmp > 0xffffffff) return FALSE;
716 *Seconds = (DWORD)tmp;
717 return TRUE;
720 /******************************************************************************
721 * RtlSecondsSince1970ToTime [NTDLL.@]
723 * Convert a count of seconds since 1970 to a time.
725 * PARAMS
726 * Seconds [I] Time to convert.
727 * Time [O] Destination for the converted time.
729 * RETURNS
730 * Nothing.
732 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
734 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
735 Time->u.LowPart = (DWORD)secs;
736 Time->u.HighPart = (DWORD)(secs >> 32);
739 /******************************************************************************
740 * RtlSecondsSince1980ToTime [NTDLL.@]
742 * Convert a count of seconds since 1980 to a time.
744 * PARAMS
745 * Seconds [I] Time to convert.
746 * Time [O] Destination for the converted time.
748 * RETURNS
749 * Nothing.
751 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
753 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
754 Time->u.LowPart = (DWORD)secs;
755 Time->u.HighPart = (DWORD)(secs >> 32);
758 /******************************************************************************
759 * RtlTimeToElapsedTimeFields [NTDLL.@]
761 * Convert a time to a count of elapsed seconds.
763 * PARAMS
764 * Time [I] Time to convert.
765 * TimeFields [O] Destination for the converted time.
767 * RETURNS
768 * Nothing.
770 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
772 LONGLONG time;
773 INT rem;
775 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
776 TimeFields->Milliseconds = rem / TICKSPERMSEC;
778 /* time is now in seconds */
779 TimeFields->Year = 0;
780 TimeFields->Month = 0;
781 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
783 /* rem is now the remaining seconds in the last day */
784 TimeFields->Second = rem % 60;
785 rem /= 60;
786 TimeFields->Minute = rem % 60;
787 TimeFields->Hour = rem / 60;
790 /***********************************************************************
791 * NtQuerySystemTime [NTDLL.@]
792 * ZwQuerySystemTime [NTDLL.@]
794 * Get the current system time.
796 * PARAMS
797 * Time [O] Destination for the current system time.
799 * RETURNS
800 * Success: STATUS_SUCCESS.
801 * Failure: An NTSTATUS error code indicating the problem.
803 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
805 struct timeval now;
807 gettimeofday( &now, 0 );
808 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
809 Time->QuadPart += now.tv_usec * 10;
810 return STATUS_SUCCESS;
813 /******************************************************************************
814 * NtQueryPerformanceCounter [NTDLL.@]
816 * Note: Windows uses a timer clocked at a multiple of 1193182 Hz. There is a
817 * good number of applications that crash when the returned frequency is either
818 * lower or higher then what Windows gives. Also too high counter values are
819 * reported to give problems.
821 NTSTATUS WINAPI NtQueryPerformanceCounter( PLARGE_INTEGER Counter, PLARGE_INTEGER Frequency )
823 LARGE_INTEGER now;
825 if (!Counter) return STATUS_ACCESS_VIOLATION;
827 /* convert a counter that increments at a rate of 10 MHz
828 * to one of 1.193182 MHz, with some care for arithmetic
829 * overflow and good accuracy (21/176 = 0.11931818) */
830 NtQuerySystemTime( &now );
831 Counter->QuadPart = ((now.QuadPart - server_start_time) * 21) / 176;
832 if (Frequency) Frequency->QuadPart = 1193182;
833 return STATUS_SUCCESS;
837 /******************************************************************************
838 * NtGetTickCount (NTDLL.@)
839 * ZwGetTickCount (NTDLL.@)
841 ULONG WINAPI NtGetTickCount(void)
843 LARGE_INTEGER now;
845 NtQuerySystemTime( &now );
846 return (now.QuadPart - server_start_time) / 10000;
850 /***********************************************************************
851 * TIME_GetTZAsStr [internal]
853 * Helper function that returns the given timezone as a string.
855 * PARAMS
856 * utc [I] The current utc time.
857 * bias [I] The bias of the current timezone.
858 * dst [I] ??
860 * RETURNS
861 * Timezone name.
863 * NOTES:
864 * This could be done with a hash table instead of merely iterating through a
865 * table, however with the small amount of entries (60 or so) I didn't think
866 * it was worth it.
868 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
870 char psTZName[7];
871 struct tm *ptm = localtime(&utc);
872 unsigned int i;
874 if (!strftime (psTZName, 7, "%Z", ptm))
875 return (NULL);
877 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
879 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
880 TZ_INFO[i].bias == bias &&
881 TZ_INFO[i].dst == dst
883 return TZ_INFO[i].psTZWindows;
885 FIXME("Can't match system time zone name \"%s\", bias=%d and dst=%d "
886 "to an entry in TZ_INFO. Please add appropriate entry to "
887 "TZ_INFO and submit as patch to wine-patches\n",psTZName,bias,dst);
888 return NULL;
891 /*** TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
894 static int TIME_GetTimeZoneInfoFromReg(RTL_TIME_ZONE_INFORMATION *tzinfo)
896 BYTE buf[90];
897 KEY_VALUE_PARTIAL_INFORMATION * KpInfo =
898 (KEY_VALUE_PARTIAL_INFORMATION *) buf;
899 HANDLE hkey;
900 DWORD size;
901 OBJECT_ATTRIBUTES attr;
902 UNICODE_STRING nameW;
904 attr.Length = sizeof(attr);
905 attr.RootDirectory = 0;
906 attr.ObjectName = &nameW;
907 attr.Attributes = 0;
908 attr.SecurityDescriptor = NULL;
909 attr.SecurityQualityOfService = NULL;
910 RtlInitUnicodeString( &nameW, TZInformationKeyW);
911 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) {
913 #define GTZIFR_N( valkey, tofield) \
914 RtlInitUnicodeString( &nameW, valkey );\
915 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
916 sizeof(buf), &size )) { \
917 if( size >= (sizeof((tofield)) + \
918 offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))) { \
919 memcpy(&(tofield), \
920 KpInfo->Data, sizeof(tofield)); \
923 #define GTZIFR_S( valkey, tofield) \
924 RtlInitUnicodeString( &nameW, valkey );\
925 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
926 sizeof(buf), &size )) { \
927 size_t len = (strlenW( (WCHAR*)KpInfo->Data ) + 1) * sizeof(WCHAR); \
928 if (len > sizeof(tofield)) len = sizeof(tofield); \
929 memcpy( tofield, KpInfo->Data, len ); \
930 tofield[(len/sizeof(WCHAR))-1] = 0; \
933 GTZIFR_N( TZStandardStartW, tzinfo->StandardDate)
934 GTZIFR_N( TZDaylightStartW, tzinfo->DaylightDate)
935 GTZIFR_N( TZBiasW, tzinfo->Bias)
936 GTZIFR_N( TZStandardBiasW, tzinfo->StandardBias)
937 GTZIFR_N( TZDaylightBiasW, tzinfo->DaylightBias)
938 GTZIFR_S( TZStandardNameW, tzinfo->StandardName)
939 GTZIFR_S( TZDaylightNameW, tzinfo->DaylightName)
941 #undef GTZIFR_N
942 #undef GTZIFR_S
943 NtClose( hkey );
944 return 1;
946 return 0;
949 /***********************************************************************
950 * RtlQueryTimeZoneInformation [NTDLL.@]
952 * Get information about the current timezone.
954 * PARAMS
955 * tzinfo [O] Destination for the retrieved timezone info.
957 * RETURNS
958 * Success: STATUS_SUCCESS.
959 * Failure: An NTSTATUS error code indicating the problem.
961 NTSTATUS WINAPI RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION *tzinfo)
963 time_t gmt;
964 int bias, daylight;
965 const WCHAR *psTZ;
967 memset(tzinfo, 0, sizeof(RTL_TIME_ZONE_INFORMATION));
969 if( !TIME_GetTimeZoneInfoFromReg(tzinfo)) {
971 gmt = time(NULL);
972 bias = TIME_GetBias(gmt, &daylight);
974 tzinfo->Bias = -bias / 60;
975 tzinfo->StandardBias = 0;
976 tzinfo->DaylightBias = -60;
977 tzinfo->StandardName[0]='\0';
978 tzinfo->DaylightName[0]='\0';
979 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
980 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
982 return STATUS_SUCCESS;
985 /***********************************************************************
986 * RtlSetTimeZoneInformation [NTDLL.@]
988 * Set the current time zone information.
990 * PARAMS
991 * tzinfo [I] Timezone information to set.
993 * RETURNS
994 * Success: STATUS_SUCCESS.
995 * Failure: An NTSTATUS error code indicating the problem.
998 NTSTATUS WINAPI RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION *tzinfo )
1000 return STATUS_PRIVILEGE_NOT_HELD;
1003 /***********************************************************************
1004 * NtSetSystemTime [NTDLL.@]
1005 * ZwSetSystemTime [NTDLL.@]
1007 * Set the system time.
1009 * PARAMS
1010 * NewTime [I] The time to set.
1011 * OldTime [O] Optional destination for the previous system time.
1013 * RETURNS
1014 * Success: STATUS_SUCCESS.
1015 * Failure: An NTSTATUS error code indicating the problem.
1017 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
1019 TIME_FIELDS tf;
1020 struct timeval tv;
1021 struct timezone tz;
1022 struct tm t;
1023 time_t sec, oldsec;
1024 int dst, bias;
1025 int err;
1027 /* Return the old time if necessary */
1028 if(OldTime)
1029 NtQuerySystemTime(OldTime);
1031 RtlTimeToTimeFields(NewTime, &tf);
1033 /* call gettimeofday to get the current timezone */
1034 gettimeofday(&tv, &tz);
1035 oldsec = tv.tv_sec;
1036 /* get delta local time from utc */
1037 bias = TIME_GetBias(oldsec, &dst);
1039 /* get the number of seconds */
1040 t.tm_sec = tf.Second;
1041 t.tm_min = tf.Minute;
1042 t.tm_hour = tf.Hour;
1043 t.tm_mday = tf.Day;
1044 t.tm_mon = tf.Month - 1;
1045 t.tm_year = tf.Year - 1900;
1046 t.tm_isdst = dst;
1047 sec = mktime (&t);
1048 /* correct for timezone and daylight */
1049 sec += bias;
1051 /* set the new time */
1052 tv.tv_sec = sec;
1053 tv.tv_usec = tf.Milliseconds * 1000;
1055 /* error and sanity check*/
1056 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
1057 err = 2;
1058 } else {
1059 #ifdef HAVE_SETTIMEOFDAY
1060 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
1061 if(err == 0)
1062 return STATUS_SUCCESS;
1063 #else
1064 err = 1;
1065 #endif
1068 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
1069 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
1070 (long)(sec-oldsec),
1071 err == -1 ? "No Permission"
1072 : sec == (time_t)-1 ? "" : "is too large." );
1074 if(err == 2)
1075 return STATUS_INVALID_PARAMETER;
1076 else if(err == -1)
1077 return STATUS_PRIVILEGE_NOT_HELD;
1078 else
1079 return STATUS_NOT_IMPLEMENTED;