Added another CST (Australian Central Daylight Time) to TZ_INFO.
[wine.git] / dlls / ntdll / time.c
blob158da62e46c572d9fa0b79349179fb6d3231997b
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 "winternl.h"
44 #include "wine/unicode.h"
45 #include "wine/debug.h"
46 #include "ntdll_misc.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
50 static RTL_CRITICAL_SECTION TIME_GetBias_section;
51 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
53 0, 0, &TIME_GetBias_section,
54 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
55 0, 0, { (DWORD_PTR)(__FILE__ ": TIME_GetBias_section") }
57 static RTL_CRITICAL_SECTION TIME_GetBias_section = { &critsect_debug, -1, 0, 0, 0, 0 };
59 /* TimeZone registry key values */
60 static const WCHAR TZInformationKeyW[] = { 'M','a','c','h','i','n','e','\\',
61 'S','Y','S','T','E','M','\\','C','u','r','r','e','n','t','C','o','n','t','r',
62 'o','l','S','e','t','\\','C','o','n','t','r','o','l','\\','T','i','m','e','z',
63 'o','n','e','I','n','f','o','r','m','a','t','i','o','n', 0};
64 static const WCHAR TZStandardStartW[] = {
65 'S','t','a','n','d','a','r','d','s','t','a','r','t', 0};
66 static const WCHAR TZDaylightStartW[] = {
67 'D','a','y','l','i','g','h','t','s','t','a','r','t', 0};
68 static const WCHAR TZDaylightBiasW[] = {
69 'D','a','y','l','i','g','h','t','B','i','a','s', 0};
70 static const WCHAR TZStandardBiasW[] = {
71 'S','t','a','n','d','a','r','d','B','i','a','s', 0};
72 static const WCHAR TZBiasW[] = {'B','i','a','s', 0};
73 static const WCHAR TZDaylightNameW[] = {
74 'D','a','y','l','i','g','h','t','N','a','m','e', 0};
75 static const WCHAR TZStandardNameW[] = {
76 'S','t','a','n','d','a','r','d','N','a','m','e', 0};
79 #define SETTIME_MAX_ADJUST 120
81 /* This structure is used to store strings that represent all of the time zones
82 * in the world. (This is used to help GetTimeZoneInformation)
84 struct tagTZ_INFO
86 const char *psTZFromUnix;
87 WCHAR psTZWindows[32];
88 int bias;
89 int dst;
92 static const struct tagTZ_INFO TZ_INFO[] =
94 {"MHT",
95 {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
96 'T','i','m','e','\0'}, -720, 0},
97 {"SST",
98 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
99 'e','\0'}, 660, 0},
100 {"HST",
101 {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
102 'T','i','m','e','\0'}, 600, 0},
103 {"AKDT",
104 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
105 'i','m','e','\0'}, 480, 1},
106 {"PST",
107 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
108 'i','m','e','\0'}, 480, 0},
109 {"PDT",
110 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
111 'i','m','e','\0'}, 420, 1},
112 {"MST",
113 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
114 'r','d',' ','T','i','m','e','\0'}, 420, 0},
115 {"MDT",
116 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
117 'T','i','m','e','\0'}, 360, 1},
118 {"CST",
119 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
120 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
121 {"CDT",
122 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
123 'i','m','e','\0'}, 300, 1},
124 {"COT",
125 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
126 'd',' ','T','i','m','e','\0'}, 300, 0},
127 {"EDT",
128 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
129 'i','m','e','\0'}, 240, 1},
130 {"EST",
131 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
132 'd',' ','T','i','m','e','\0'}, 300, 0},
133 {"ECT",
134 {'E','a','s','t','e','r','n',' ','C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r',
135 'd',' ','T','i','m','e','\0'}, 300, 0},
136 {"ADT",
137 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
138 'T','i','m','e','\0'}, 180, 1},
139 {"VET",
140 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
141 'd',' ','T','i','m','e','\0'}, 240, 0},
142 {"CLT",
143 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
144 'd',' ','T','i','m','e','\0'}, 240, 0},
145 {"NDT",
146 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
147 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
148 {"BRT",
149 {'B','r','a','z','i','l','i','a','n',' ','S','t','a','n','d','a','r','d',
150 ' ','T','i','m','e','\0'}, 180, 0},
151 {"BRST",
152 {'B','r','a','z','i','l','i','a','n',' ','S','u','m','m','e','r',
153 ' ','T','i','m','e','\0'}, 120, 1},
154 {"ART",
155 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
156 'd',' ','T','i','m','e','\0'}, 180, 0},
157 {"WGST",
158 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
159 ' ','T','i','m','e','\0'}, 120, 1},
160 {"GST",
161 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
162 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
163 {"AZOST",
164 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
165 'm','e','\0'}, 0, 1},
166 {"CVT",
167 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
168 'd',' ','T','i','m','e','\0'}, 60, 0},
169 {"WEST",
170 {'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},
171 {"WET",
172 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
173 ' ','T','i','m','e','\0'}, 0, 0},
174 {"BST",
175 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
176 -60, 1},
177 {"IST",
178 {'I','r','i','s','h',' ','S','u','m','m','e','r',' ','T','i','m','e','\0'},
179 -60, 1},
180 {"GMT",
181 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
182 0, 0},
183 {"UTC",
184 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
185 0, 0},
186 {"CET",
187 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e','a','n',' ',
188 'T','i','m','e','\0'}, -60, 0},
189 {"CEST",
190 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
191 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
192 {"MET",
193 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
194 'n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
195 {"MEST",
196 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','D','a','y',
197 'l','i','g','h','t',' ','T','i','m','e','\0'}, -120, 1},
198 {"WAT",
199 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
200 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
201 {"EEST",
202 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
203 ' ','T','i','m','e','\0'}, -180, 1},
204 {"EET",
205 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
206 'e','\0'}, -120, 0},
207 {"CAT",
208 {'C','e','n','t','r','a','l',' ','A','f','r','i','c','a','n',' '
209 ,'T','i','m','e','\0'}, -120, 0},
210 {"SAST",
211 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
212 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
213 {"IST",
214 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
215 'm','e','\0'}, -120, 0},
216 {"MSK",
217 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
218 'i','m','e','\0'}, -180, 0},
219 {"ADT",
220 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
221 'm','e','\0'}, -240, 1},
222 {"AST",
223 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
224 '\0'}, -180, 0},
225 {"MSD",
226 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
227 'i','m','e','\0'}, -240, 1},
228 {"EAT",
229 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
230 ' ','T','i','m','e','\0'}, -180, 0},
231 {"IRST",
232 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
233 '\0'}, -270, 1},
234 {"GST",
235 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
236 'i','m','e','\0'}, -240, 0},
237 {"AZST",
238 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
239 'T','i','m','e','\0'}, -300, 1},
240 {"AFT",
241 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
242 'r','d',' ','T','i','m','e','\0'}, -270, 0},
243 {"SAMT",
244 {'S','a','m','a','r','a',' ','S','t','a','n','d','a','r','d',' ','T','i',
245 'm','e','\0'}, -270, 1},
246 {"YEKST",
247 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
248 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
249 {"PKT",
250 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
251 ' ','T','i','m','e','\0'}, -300, 0},
252 {"IST",
253 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
254 'e','\0'}, -330, 0},
255 {"NPT",
256 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
257 'e','\0'}, -345, 0},
258 {"ALMST",
259 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
260 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
261 {"BDT",
262 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
263 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
264 {"LKT",
265 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
266 ' ','T','i','m','e','\0'}, -360, 0},
267 {"MMT",
268 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
269 'i','m','e','\0'}, -390, 0},
270 {"ICT",
271 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
272 'i','m','e','\0'}, -420, 0},
273 {"KRAST",
274 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
275 'd',' ','T','i','m','e','\0'}, -480, 1},
276 {"CST",
277 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
278 'e','\0'}, -480, 0},
279 {"IRKST",
280 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
281 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
282 {"SGT",
283 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
284 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
285 {"WST",
286 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
287 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
288 {"JST",
289 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
290 'e','\0'}, -540, 0},
291 {"KST",
292 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
293 'e','\0'}, -540, 0},
294 {"YAKST",
295 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
296 'i','m','e','\0'}, -600, 1},
297 {"CST",
298 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
299 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
300 {"CST",
301 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','D','a','y',
302 'l','i','g','h','t',' ','T','i','m','e','\0'}, -630, 1},
303 {"EST",
304 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
305 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
306 {"EST",
307 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
308 'a','r','d',' ','T','i','m','e','\0'}, -660, 1},
309 {"GST",
310 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
311 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
312 {"VLAST",
313 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
314 'r','d',' ','T','i','m','e','\0'}, -660, 1},
315 {"MAGST",
316 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
317 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
318 {"NZST",
319 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
320 'r','d',' ','T','i','m','e','\0'}, -720, 0},
321 {"NZDT",
322 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','D','a','y','l','i','g',
323 'h','t',' ','T','i','m','e','\0'}, -780, 1},
324 {"FJT",
325 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
326 '\0'}, -720, 0},
327 {"TOT",
328 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
329 'e','\0'}, -780, 0},
330 {"NOVT",
331 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','t','a','n','d','a',
332 'r','d',' ','T','i','m','e','\0'}, -360, 1},
333 {"ANAT",
334 {'A','n','a','d','y','r',' ','S','t','a','n','d','a','r','d',' ','T','i',
335 'm','e','\0'}, -720, 1},
336 {"HKT",
337 {'H','o','n','g',' ','K','o','n','g',' ','S','t','a','n','d','a','r','d',
338 ' ','T','i','m','e','\0'}, -480, 0},
339 {"UYT",
340 {'U','r','u','g','u','a','y','a','n',' ','T','i','m','e','\0'}, 180, 0},
341 {"MYT",
342 {'M','a','l','a','y','s','i','a','n',' ','T','i','m','e','\0'}, -480, 0},
343 {"PHT",
344 {'P','h','i','l','i','p','p','i','n','e',' ','T','i','m','e','\0'}, -480, 0},
345 {"NOVST",
346 {'N','o','v','o','s','i','b','i','r','s','k',' ','S','u','m','m','e','r',
347 ' ','T','i','m','e','\0'}, -480, 1}
350 #define TICKSPERSEC 10000000
351 #define TICKSPERMSEC 10000
352 #define SECSPERDAY 86400
353 #define SECSPERHOUR 3600
354 #define SECSPERMIN 60
355 #define MINSPERHOUR 60
356 #define HOURSPERDAY 24
357 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
358 #define DAYSPERWEEK 7
359 #define EPOCHYEAR 1601
360 #define DAYSPERNORMALYEAR 365
361 #define DAYSPERLEAPYEAR 366
362 #define MONSPERYEAR 12
363 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
364 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
365 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
367 /* 1601 to 1970 is 369 years plus 89 leap days */
368 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
369 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
370 /* 1601 to 1980 is 379 years plus 91 leap days */
371 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
372 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
373 /* max ticks that can be represented as Unix time */
374 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
377 static const int MonthLengths[2][MONSPERYEAR] =
379 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
380 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
383 static inline int IsLeapYear(int Year)
385 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
388 /***********************************************************************
389 * NTDLL_get_server_timeout
391 * Convert a NTDLL timeout into a timeval struct to send to the server.
393 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
395 UINT remainder;
397 if (!timeout) /* infinite timeout */
399 when->sec = when->usec = 0;
401 else if (timeout->QuadPart <= 0) /* relative timeout */
403 struct timeval tv;
405 if (-timeout->QuadPart > (LONGLONG)INT_MAX * TICKSPERSEC)
406 when->sec = when->usec = INT_MAX;
407 else
409 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
410 gettimeofday( &tv, 0 );
411 when->sec = tv.tv_sec + sec;
412 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
414 when->usec -= 1000000;
415 when->sec++;
417 if (when->sec < tv.tv_sec) /* overflow */
418 when->sec = when->usec = INT_MAX;
421 else /* absolute time */
423 if (timeout->QuadPart < TICKS_1601_TO_1970)
424 when->sec = when->usec = 0;
425 else if (timeout->QuadPart > TICKS_1601_TO_UNIX_MAX)
426 when->sec = when->usec = INT_MAX;
427 else
429 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
430 TICKSPERSEC, &remainder );
431 when->usec = remainder / 10;
437 /***********************************************************************
438 * NTDLL_from_server_timeout
440 * Convert a timeval struct from the server into an NTDLL timeout.
442 void NTDLL_from_server_timeout( LARGE_INTEGER *timeout, const abs_time_t *when )
444 timeout->QuadPart = when->sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
445 timeout->QuadPart += when->usec * 10;
449 /******************************************************************************
450 * RtlTimeToTimeFields [NTDLL.@]
452 * Convert a time into a TIME_FIELDS structure.
454 * PARAMS
455 * liTime [I] Time to convert.
456 * TimeFields [O] Destination for the converted time.
458 * RETURNS
459 * Nothing.
461 VOID WINAPI RtlTimeToTimeFields(
462 const LARGE_INTEGER *liTime,
463 PTIME_FIELDS TimeFields)
465 int SecondsInDay;
466 long int cleaps, years, yearday, months;
467 long int Days;
468 LONGLONG Time;
470 /* Extract millisecond from time and convert time into seconds */
471 TimeFields->Milliseconds =
472 (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
473 Time = liTime->QuadPart / TICKSPERSEC;
475 /* The native version of RtlTimeToTimeFields does not take leap seconds
476 * into account */
478 /* Split the time into days and seconds within the day */
479 Days = Time / SECSPERDAY;
480 SecondsInDay = Time % SECSPERDAY;
482 /* compute time of day */
483 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
484 SecondsInDay = SecondsInDay % SECSPERHOUR;
485 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
486 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
488 /* compute day of week */
489 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
491 /* compute year, month and day of month. */
492 cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
493 Days += 28188 + cleaps;
494 years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
495 yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
496 months = (64 * yearday) / 1959;
497 /* the result is based on a year starting on March.
498 * To convert take 12 from Januari and Februari and
499 * increase the year by one. */
500 if( months < 14 ) {
501 TimeFields->Month = months - 1;
502 TimeFields->Year = years + 1524;
503 } else {
504 TimeFields->Month = months - 13;
505 TimeFields->Year = years + 1525;
507 /* calculation of day of month is based on the wonderful
508 * sequence of INT( n * 30.6): it reproduces the
509 * 31-30-31-30-31-31 month lengths exactly for small n's */
510 TimeFields->Day = yearday - (1959 * months) / 64 ;
511 return;
514 /******************************************************************************
515 * RtlTimeFieldsToTime [NTDLL.@]
517 * Convert a TIME_FIELDS structure into a time.
519 * PARAMS
520 * ftTimeFields [I] TIME_FIELDS structure to convert.
521 * Time [O] Destination for the converted time.
523 * RETURNS
524 * Success: TRUE.
525 * Failure: FALSE.
527 BOOLEAN WINAPI RtlTimeFieldsToTime(
528 PTIME_FIELDS tfTimeFields,
529 PLARGE_INTEGER Time)
531 int month, year, cleaps, day;
533 /* FIXME: normalize the TIME_FIELDS structure here */
534 /* No, native just returns 0 (error) if the fields are not */
535 if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
536 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
537 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
538 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
539 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
540 tfTimeFields->Day < 1 ||
541 tfTimeFields->Day > MonthLengths
542 [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
543 [ tfTimeFields->Month - 1] ||
544 tfTimeFields->Year < 1601 )
545 return FALSE;
547 /* now calculate a day count from the date
548 * First start counting years from March. This way the leap days
549 * are added at the end of the year, not somewhere in the middle.
550 * Formula's become so much less complicate that way.
551 * To convert: add 12 to the month numbers of Jan and Feb, and
552 * take 1 from the year */
553 if(tfTimeFields->Month < 3) {
554 month = tfTimeFields->Month + 13;
555 year = tfTimeFields->Year - 1;
556 } else {
557 month = tfTimeFields->Month + 1;
558 year = tfTimeFields->Year;
560 cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
561 day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
562 (1959 * month) / 64 + /* months * daypermonth */
563 tfTimeFields->Day - /* day of the month */
564 584817 ; /* zero that on 1601-01-01 */
565 /* done */
567 Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
568 tfTimeFields->Hour) * MINSPERHOUR +
569 tfTimeFields->Minute) * SECSPERMIN +
570 tfTimeFields->Second ) * 1000 +
571 tfTimeFields->Milliseconds ) * TICKSPERMSEC;
573 return TRUE;
576 /***********************************************************************
577 * TIME_GetBias [internal]
579 * Helper function calculates delta local time from UTC.
581 * PARAMS
582 * utc [I] The current utc time.
583 * pdaylight [I] Local daylight.
585 * RETURNS
586 * The bias for the current timezone.
588 static int TIME_GetBias(time_t utc, int *pdaylight)
590 struct tm *ptm;
591 static time_t last_utc;
592 static int last_bias;
593 static int last_daylight;
594 int ret;
596 RtlEnterCriticalSection( &TIME_GetBias_section );
597 if(utc == last_utc)
599 *pdaylight = last_daylight;
600 ret = last_bias;
601 } else
603 ptm = localtime(&utc);
604 *pdaylight = last_daylight =
605 ptm->tm_isdst; /* daylight for local timezone */
606 ptm = gmtime(&utc);
607 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
608 last_utc = utc;
609 ret = last_bias = (int)(utc-mktime(ptm));
611 RtlLeaveCriticalSection( &TIME_GetBias_section );
612 return ret;
615 /******************************************************************************
616 * RtlLocalTimeToSystemTime [NTDLL.@]
618 * Convert a local time into system time.
620 * PARAMS
621 * LocalTime [I] Local time to convert.
622 * SystemTime [O] Destination for the converted time.
624 * RETURNS
625 * Success: STATUS_SUCCESS.
626 * Failure: An NTSTATUS error code indicating the problem.
628 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
629 PLARGE_INTEGER SystemTime)
631 time_t gmt;
632 int bias, daylight;
634 TRACE("(%p, %p)\n", LocalTime, SystemTime);
636 gmt = time(NULL);
637 bias = TIME_GetBias(gmt, &daylight);
639 SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)TICKSPERSEC;
640 return STATUS_SUCCESS;
643 /******************************************************************************
644 * RtlSystemTimeToLocalTime [NTDLL.@]
646 * Convert a system time into a local time.
648 * PARAMS
649 * SystemTime [I] System time to convert.
650 * LocalTime [O] Destination for the converted time.
652 * RETURNS
653 * Success: STATUS_SUCCESS.
654 * Failure: An NTSTATUS error code indicating the problem.
656 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
657 PLARGE_INTEGER LocalTime )
659 time_t gmt;
660 int bias, daylight;
662 TRACE("(%p, %p)\n", SystemTime, LocalTime);
664 gmt = time(NULL);
665 bias = TIME_GetBias(gmt, &daylight);
667 LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)TICKSPERSEC;
668 return STATUS_SUCCESS;
671 /******************************************************************************
672 * RtlTimeToSecondsSince1970 [NTDLL.@]
674 * Convert a time into a count of seconds since 1970.
676 * PARAMS
677 * Time [I] Time to convert.
678 * Seconds [O] Destination for the converted time.
680 * RETURNS
681 * Success: TRUE.
682 * Failure: FALSE, if the resulting value will not fit in a DWORD.
684 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
686 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
687 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
688 tmp -= SECS_1601_TO_1970;
689 if (tmp > 0xffffffff) return FALSE;
690 *Seconds = (DWORD)tmp;
691 return TRUE;
694 /******************************************************************************
695 * RtlTimeToSecondsSince1980 [NTDLL.@]
697 * Convert a time into a count of seconds since 1980.
699 * PARAMS
700 * Time [I] Time to convert.
701 * Seconds [O] Destination for the converted time.
703 * RETURNS
704 * Success: TRUE.
705 * Failure: FALSE, if the resulting value will not fit in a DWORD.
707 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
709 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
710 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
711 tmp -= SECS_1601_TO_1980;
712 if (tmp > 0xffffffff) return FALSE;
713 *Seconds = (DWORD)tmp;
714 return TRUE;
717 /******************************************************************************
718 * RtlSecondsSince1970ToTime [NTDLL.@]
720 * Convert a count of seconds since 1970 to a time.
722 * PARAMS
723 * Seconds [I] Time to convert.
724 * Time [O] Destination for the converted time.
726 * RETURNS
727 * Nothing.
729 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
731 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
732 Time->u.LowPart = (DWORD)secs;
733 Time->u.HighPart = (DWORD)(secs >> 32);
736 /******************************************************************************
737 * RtlSecondsSince1980ToTime [NTDLL.@]
739 * Convert a count of seconds since 1980 to a time.
741 * PARAMS
742 * Seconds [I] Time to convert.
743 * Time [O] Destination for the converted time.
745 * RETURNS
746 * Nothing.
748 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
750 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
751 Time->u.LowPart = (DWORD)secs;
752 Time->u.HighPart = (DWORD)(secs >> 32);
755 /******************************************************************************
756 * RtlTimeToElapsedTimeFields [NTDLL.@]
758 * Convert a time to a count of elapsed seconds.
760 * PARAMS
761 * Time [I] Time to convert.
762 * TimeFields [O] Destination for the converted time.
764 * RETURNS
765 * Nothing.
767 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
769 LONGLONG time;
770 INT rem;
772 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
773 TimeFields->Milliseconds = rem / TICKSPERMSEC;
775 /* time is now in seconds */
776 TimeFields->Year = 0;
777 TimeFields->Month = 0;
778 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
780 /* rem is now the remaining seconds in the last day */
781 TimeFields->Second = rem % 60;
782 rem /= 60;
783 TimeFields->Minute = rem % 60;
784 TimeFields->Hour = rem / 60;
787 /***********************************************************************
788 * NtQuerySystemTime [NTDLL.@]
789 * ZwQuerySystemTime [NTDLL.@]
791 * Get the current system time.
793 * PARAMS
794 * Time [O] Destination for the current system time.
796 * RETURNS
797 * Success: STATUS_SUCCESS.
798 * Failure: An NTSTATUS error code indicating the problem.
800 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
802 struct timeval now;
804 gettimeofday( &now, 0 );
805 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
806 Time->QuadPart += now.tv_usec * 10;
807 return STATUS_SUCCESS;
810 /******************************************************************************
811 * NtQueryPerformanceCounter [NTDLL.@]
813 * Note: Windows uses a timer clocked at a multiple of 1193182 Hz. There is a
814 * good number of applications that crash when the returned frequency is either
815 * lower or higher then what Windows gives. Also too high counter values are
816 * reported to give problems.
818 NTSTATUS WINAPI NtQueryPerformanceCounter( PLARGE_INTEGER Counter, PLARGE_INTEGER Frequency )
820 struct timeval now;
822 if (!Counter) return STATUS_ACCESS_VIOLATION;
823 gettimeofday( &now, 0 );
824 /* convert a counter that increments at a rate of 1 MHz
825 * to one of 1.193182 MHz, with some care for arithmetic
826 * overflow ( will not overflow for 5000 years ) and
827 * good accuracy ( 105/88 = 1.19318182) */
828 Counter->QuadPart = (((now.tv_sec - server_start_time) * (ULONGLONG)1000000 + now.tv_usec) * 105) / 88;
829 if (Frequency) Frequency->QuadPart = 1193182;
830 return STATUS_SUCCESS;
833 /***********************************************************************
834 * TIME_GetTZAsStr [internal]
836 * Helper function that returns the given timezone as a string.
838 * PARAMS
839 * utc [I] The current utc time.
840 * bias [I] The bias of the current timezone.
841 * dst [I] ??
843 * RETURNS
844 * Timezone name.
846 * NOTES:
847 * This could be done with a hash table instead of merely iterating through a
848 * table, however with the small amount of entries (60 or so) I didn't think
849 * it was worth it.
851 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
853 char psTZName[7];
854 struct tm *ptm = localtime(&utc);
855 unsigned int i;
857 if (!strftime (psTZName, 7, "%Z", ptm))
858 return (NULL);
860 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
862 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
863 TZ_INFO[i].bias == bias &&
864 TZ_INFO[i].dst == dst
866 return TZ_INFO[i].psTZWindows;
868 FIXME("Can't match system time zone name \"%s\", bias=%d and dst=%d "
869 "to an entry in TZ_INFO. Please add appropriate entry to "
870 "TZ_INFO and submit as patch to wine-patches\n",psTZName,bias,dst);
871 return NULL;
874 /*** TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
877 static int TIME_GetTimeZoneInfoFromReg(RTL_TIME_ZONE_INFORMATION *tzinfo)
879 BYTE buf[90];
880 KEY_VALUE_PARTIAL_INFORMATION * KpInfo =
881 (KEY_VALUE_PARTIAL_INFORMATION *) buf;
882 HANDLE hkey;
883 DWORD size;
884 OBJECT_ATTRIBUTES attr;
885 UNICODE_STRING nameW;
887 attr.Length = sizeof(attr);
888 attr.RootDirectory = 0;
889 attr.ObjectName = &nameW;
890 attr.Attributes = 0;
891 attr.SecurityDescriptor = NULL;
892 attr.SecurityQualityOfService = NULL;
893 RtlInitUnicodeString( &nameW, TZInformationKeyW);
894 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) {
896 #define GTZIFR_N( valkey, tofield) \
897 RtlInitUnicodeString( &nameW, valkey );\
898 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
899 sizeof(buf), &size )) { \
900 if( size >= (sizeof((tofield)) + \
901 offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))) { \
902 memcpy(&(tofield), \
903 KpInfo->Data, sizeof(tofield)); \
906 #define GTZIFR_S( valkey, tofield) \
907 RtlInitUnicodeString( &nameW, valkey );\
908 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
909 sizeof(buf), &size )) { \
910 size_t len = (strlenW( (WCHAR*)KpInfo->Data ) + 1) * sizeof(WCHAR); \
911 if (len > sizeof(tofield)) len = sizeof(tofield); \
912 memcpy( tofield, KpInfo->Data, len ); \
913 tofield[(len/sizeof(WCHAR))-1] = 0; \
916 GTZIFR_N( TZStandardStartW, tzinfo->StandardDate)
917 GTZIFR_N( TZDaylightStartW, tzinfo->DaylightDate)
918 GTZIFR_N( TZBiasW, tzinfo->Bias)
919 GTZIFR_N( TZStandardBiasW, tzinfo->StandardBias)
920 GTZIFR_N( TZDaylightBiasW, tzinfo->DaylightBias)
921 GTZIFR_S( TZStandardNameW, tzinfo->StandardName)
922 GTZIFR_S( TZDaylightNameW, tzinfo->DaylightName)
924 #undef GTZIFR_N
925 #undef GTZIFR_S
926 NtClose( hkey );
927 return 1;
929 return 0;
932 /***********************************************************************
933 * RtlQueryTimeZoneInformation [NTDLL.@]
935 * Get information about the current timezone.
937 * PARAMS
938 * tzinfo [O] Destination for the retrieved timezone info.
940 * RETURNS
941 * Success: STATUS_SUCCESS.
942 * Failure: An NTSTATUS error code indicating the problem.
944 NTSTATUS WINAPI RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION *tzinfo)
946 time_t gmt;
947 int bias, daylight;
948 const WCHAR *psTZ;
950 memset(tzinfo, 0, sizeof(RTL_TIME_ZONE_INFORMATION));
952 if( !TIME_GetTimeZoneInfoFromReg(tzinfo)) {
954 gmt = time(NULL);
955 bias = TIME_GetBias(gmt, &daylight);
957 tzinfo->Bias = -bias / 60;
958 tzinfo->StandardBias = 0;
959 tzinfo->DaylightBias = -60;
960 tzinfo->StandardName[0]='\0';
961 tzinfo->DaylightName[0]='\0';
962 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
963 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
965 return STATUS_SUCCESS;
968 /***********************************************************************
969 * RtlSetTimeZoneInformation [NTDLL.@]
971 * Set the current time zone information.
973 * PARAMS
974 * tzinfo [I] Timezone information to set.
976 * RETURNS
977 * Success: STATUS_SUCCESS.
978 * Failure: An NTSTATUS error code indicating the problem.
980 * BUGS
981 * Uses the obsolete unix timezone structure and tz_dsttime member.
983 NTSTATUS WINAPI RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION *tzinfo )
985 #ifdef HAVE_SETTIMEOFDAY
986 struct timezone tz;
988 tz.tz_minuteswest = tzinfo->Bias;
989 #ifdef DST_NONE
990 tz.tz_dsttime = DST_NONE;
991 #else
992 tz.tz_dsttime = 0;
993 #endif
994 if(!settimeofday(NULL, &tz))
995 return STATUS_SUCCESS;
996 #endif
997 return STATUS_PRIVILEGE_NOT_HELD;
1000 /***********************************************************************
1001 * NtSetSystemTime [NTDLL.@]
1002 * ZwSetSystemTime [NTDLL.@]
1004 * Set the system time.
1006 * PARAMS
1007 * NewTime [I] The time to set.
1008 * OldTime [O] Optional destination for the previous system time.
1010 * RETURNS
1011 * Success: STATUS_SUCCESS.
1012 * Failure: An NTSTATUS error code indicating the problem.
1014 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
1016 TIME_FIELDS tf;
1017 struct timeval tv;
1018 struct timezone tz;
1019 struct tm t;
1020 time_t sec, oldsec;
1021 int dst, bias;
1022 int err;
1024 /* Return the old time if necessary */
1025 if(OldTime)
1026 NtQuerySystemTime(OldTime);
1028 RtlTimeToTimeFields(NewTime, &tf);
1030 /* call gettimeofday to get the current timezone */
1031 gettimeofday(&tv, &tz);
1032 oldsec = tv.tv_sec;
1033 /* get delta local time from utc */
1034 bias = TIME_GetBias(oldsec, &dst);
1036 /* get the number of seconds */
1037 t.tm_sec = tf.Second;
1038 t.tm_min = tf.Minute;
1039 t.tm_hour = tf.Hour;
1040 t.tm_mday = tf.Day;
1041 t.tm_mon = tf.Month - 1;
1042 t.tm_year = tf.Year - 1900;
1043 t.tm_isdst = dst;
1044 sec = mktime (&t);
1045 /* correct for timezone and daylight */
1046 sec += bias;
1048 /* set the new time */
1049 tv.tv_sec = sec;
1050 tv.tv_usec = tf.Milliseconds * 1000;
1052 /* error and sanity check*/
1053 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
1054 err = 2;
1055 } else {
1056 #ifdef HAVE_SETTIMEOFDAY
1057 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
1058 if(err == 0)
1059 return STATUS_SUCCESS;
1060 #else
1061 err = 1;
1062 #endif
1065 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
1066 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
1067 (long)(sec-oldsec),
1068 err == -1 ? "No Permission"
1069 : sec == (time_t)-1 ? "" : "is too large." );
1071 if(err == 2)
1072 return STATUS_INVALID_PARAMETER;
1073 else if(err == -1)
1074 return STATUS_PRIVILEGE_NOT_HELD;
1075 else
1076 return STATUS_NOT_IMPLEMENTED;