ntdll/tests: Try to fail more gracefully on win9x.
[wine] / dlls / ntdll / time.c
1 /*
2  * Nt time functions.
3  *
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)
7  *
8  * Copyright 1999 Juergen Schmied
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
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
39
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"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
51
52 static RTL_CRITICAL_SECTION TIME_GetBias_section;
53 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
54 {
55     0, 0, &TIME_GetBias_section,
56     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
57       0, 0, { (DWORD_PTR)(__FILE__ ": TIME_GetBias_section") }
58 };
59 static RTL_CRITICAL_SECTION TIME_GetBias_section = { &critsect_debug, -1, 0, 0, 0, 0 };
60
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};
79
80
81 #define SETTIME_MAX_ADJUST 120
82
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)
85  */
86 struct tagTZ_INFO
87 {
88     const char *psTZFromUnix;
89     WCHAR psTZWindows[32];
90     int bias;
91     int dst;
92 };
93
94 static const struct tagTZ_INFO TZ_INFO[] =
95 {
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    {"MSK",
237     {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
238      'i','m','e','\0'}, -180, 0},
239    {"ADT",
240     {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
241      'm','e','\0'}, -240, 1},
242    {"AST",
243     {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
244      '\0'}, -180, 0},
245    {"MSD",
246     {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
247      'i','m','e','\0'}, -240, 1},
248    {"EAT",
249     {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
250      ' ','T','i','m','e','\0'}, -180, 0},
251    {"IRST",
252     {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
253      0},-210, 0 },
254    {"IRST",
255     {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
256      '\0'}, -270, 1},
257    {"GST",
258     {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
259      'i','m','e','\0'}, -240, 0},
260    {"AZT",
261     {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
262      'T','i','m','e',0}, -240, 0 },
263    {"AZST",
264     {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
265      'T','i','m','e','\0'}, -300, 1},
266    {"AFT",
267     {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
268      'r','d',' ','T','i','m','e','\0'}, -270, 0},
269    {"SAMT",
270     {'S','a','m','a','r','a',' ','S','t','a','n','d','a','r','d',' ','T','i',
271      'm','e','\0'}, -270, 1},
272    {"YEKST",
273     {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
274      'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
275    {"PKT",
276     {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
277      ' ','T','i','m','e','\0'}, -300, 0},
278    {"IST",
279     {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
280      'e','\0'}, -330, 0},
281    {"NPT",
282     {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
283      'e','\0'}, -345, 0},
284    {"ALMST",
285     {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
286      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
287    {"BDT",
288     {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
289      'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
290    {"LKT",
291     {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
292      ' ','T','i','m','e','\0'}, -360, 0},
293    {"MMT",
294     {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
295      'i','m','e','\0'}, -390, 0},
296    {"ICT",
297     {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
298      'i','m','e','\0'}, -420, 0},
299    {"KRAT",
300     {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
301      'd',' ','T','i','m','e',0}, -420, 0},
302    {"KRAST",
303     {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
304      'd',' ','T','i','m','e','\0'}, -480, 1},
305    {"IRKT",
306     {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
307      'a','n','d','a','r','d',' ','T','i','m','e',0}, -480, 0},
308    {"CST",
309     {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
310      'e','\0'}, -480, 0},
311    {"IRKST",
312     {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
313      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
314    {"SGT",
315     {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
316      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
317    {"WST",
318     {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
319      'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
320    {"JST",
321     {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
322      'e','\0'}, -540, 0},
323    {"KST",
324     {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
325      'e','\0'}, -540, 0},
326    {"YAKST",
327     {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
328      'i','m','e','\0'}, -600, 1},
329    {"CST",
330     {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
331      'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
332    {"CST",
333     {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','D','a','y',
334      'l','i','g','h','t',' ','T','i','m','e','\0'}, -630, 1},
335    {"EST",
336     {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
337      'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
338    {"EST",
339     {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
340      'a','r','d',' ','T','i','m','e','\0'}, -660, 1},
341    {"GST",
342     {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
343      'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
344    {"VLAT",
345     {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
346      'r','d',' ','T','i','m','e',0}, -600, 0 },
347    {"ChST",
348     {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
349      'a','r','d',' ','T','i','m','e',0}, -600, 0},
350    {"VLAST",
351     {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
352      'r','d',' ','T','i','m','e','\0'}, -660, 1},
353    {"MAGT",
354     {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
355      'a','n','d','a','r','d',' ','T','i','m','e',0}, -660, 0},
356    {"MAGST",
357     {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
358      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
359    {"NZST",
360     {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
361      'r','d',' ','T','i','m','e','\0'}, -720, 0},
362    {"NZDT",
363     {'N','e','w',' ','Z','e','a','l','a','n','d',' ','D','a','y','l','i','g',
364      'h','t',' ','T','i','m','e','\0'}, -780, 1},
365    {"FJT",
366     {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
367      '\0'}, -720, 0},
368    {"TOT",
369     {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
370      'e','\0'}, -780, 0},
371    {"NOVT",
372     {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
373      'a','n','d','a','r','d',' ','T','i','m','e',0 }, -360, 0},
374    {"NOVT",
375     {'N','o','v','o','s','i','b','i','r','s','k',' ','S','t','a','n','d','a',
376      'r','d',' ','T','i','m','e','\0'}, -360, 1},
377    {"ANAT",
378     {'A','n','a','d','y','r',' ','S','t','a','n','d','a','r','d',' ','T','i',
379      'm','e','\0'}, -720, 1},
380    {"HKT",
381     {'H','o','n','g',' ','K','o','n','g',' ','S','t','a','n','d','a','r','d',
382      ' ','T','i','m','e','\0'}, -480, 0},
383    {"UYT",
384     {'U','r','u','g','u','a','y','a','n',' ','T','i','m','e','\0'}, 180, 0},
385    {"MYT",
386     {'M','a','l','a','y','s','i','a','n',' ','T','i','m','e','\0'}, -480, 0},
387    {"PHT",
388     {'P','h','i','l','i','p','p','i','n','e',' ','T','i','m','e','\0'}, -480, 0},
389    {"NOVST",
390     {'N','o','v','o','s','i','b','i','r','s','k',' ','S','u','m','m','e','r',
391      ' ','T','i','m','e','\0'}, -480, 1}
392 };
393
394 #define TICKSPERSEC        10000000
395 #define TICKSPERMSEC       10000
396 #define SECSPERDAY         86400
397 #define SECSPERHOUR        3600
398 #define SECSPERMIN         60
399 #define MINSPERHOUR        60
400 #define HOURSPERDAY        24
401 #define EPOCHWEEKDAY       1  /* Jan 1, 1601 was Monday */
402 #define DAYSPERWEEK        7
403 #define EPOCHYEAR          1601
404 #define DAYSPERNORMALYEAR  365
405 #define DAYSPERLEAPYEAR    366
406 #define MONSPERYEAR        12
407 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
408 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
409 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
410
411 /* 1601 to 1970 is 369 years plus 89 leap days */
412 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
413 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
414 /* 1601 to 1980 is 379 years plus 91 leap days */
415 #define SECS_1601_TO_1980  ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
416 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
417 /* max ticks that can be represented as Unix time */
418 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
419
420
421 static const int MonthLengths[2][MONSPERYEAR] =
422 {
423         { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
424         { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
425 };
426
427 static inline int IsLeapYear(int Year)
428 {
429         return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
430 }
431
432 /***********************************************************************
433  *              NTDLL_get_server_timeout
434  *
435  * Convert a NTDLL timeout into a timeval struct to send to the server.
436  */
437 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
438 {
439     UINT remainder;
440
441     if (!timeout)  /* infinite timeout */
442     {
443         when->sec = when->usec = 0;
444     }
445     else if (timeout->QuadPart <= 0)  /* relative timeout */
446     {
447         struct timeval tv;
448
449         if (-timeout->QuadPart > (LONGLONG)INT_MAX * TICKSPERSEC)
450             when->sec = when->usec = INT_MAX;
451         else
452         {
453             ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
454             gettimeofday( &tv, 0 );
455             when->sec = tv.tv_sec + sec;
456             if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
457             {
458                 when->usec -= 1000000;
459                 when->sec++;
460             }
461             if (when->sec < tv.tv_sec)  /* overflow */
462                 when->sec = when->usec = INT_MAX;
463         }
464     }
465     else  /* absolute time */
466     {
467         if (timeout->QuadPart < TICKS_1601_TO_1970)
468             when->sec = when->usec = 0;
469         else if (timeout->QuadPart > TICKS_1601_TO_UNIX_MAX)
470             when->sec = when->usec = INT_MAX;
471         else
472         {
473             when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
474                                                    TICKSPERSEC, &remainder );
475             when->usec = remainder / 10;
476         }
477     }
478 }
479
480
481 /***********************************************************************
482  *              NTDLL_from_server_timeout
483  *
484  * Convert a timeval struct from the server into an NTDLL timeout.
485  */
486 void NTDLL_from_server_timeout( LARGE_INTEGER *timeout, const abs_time_t *when )
487 {
488     timeout->QuadPart = when->sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
489     timeout->QuadPart += when->usec * 10;
490 }
491
492
493 /******************************************************************************
494  *       RtlTimeToTimeFields [NTDLL.@]
495  *
496  * Convert a time into a TIME_FIELDS structure.
497  *
498  * PARAMS
499  *   liTime     [I] Time to convert.
500  *   TimeFields [O] Destination for the converted time.
501  *
502  * RETURNS
503  *   Nothing.
504  */
505 VOID WINAPI RtlTimeToTimeFields(
506         const LARGE_INTEGER *liTime,
507         PTIME_FIELDS TimeFields)
508 {
509         int SecondsInDay;
510         long int cleaps, years, yearday, months;
511         long int Days;
512         LONGLONG Time;
513
514         /* Extract millisecond from time and convert time into seconds */
515         TimeFields->Milliseconds =
516             (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
517         Time = liTime->QuadPart / TICKSPERSEC;
518
519         /* The native version of RtlTimeToTimeFields does not take leap seconds
520          * into account */
521
522         /* Split the time into days and seconds within the day */
523         Days = Time / SECSPERDAY;
524         SecondsInDay = Time % SECSPERDAY;
525
526         /* compute time of day */
527         TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
528         SecondsInDay = SecondsInDay % SECSPERHOUR;
529         TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
530         TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
531
532         /* compute day of week */
533         TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
534
535         /* compute year, month and day of month. */
536         cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
537         Days += 28188 + cleaps;
538         years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
539         yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
540         months = (64 * yearday) / 1959;
541         /* the result is based on a year starting on March.
542          * To convert take 12 from Januari and Februari and
543          * increase the year by one. */
544         if( months < 14 ) {
545             TimeFields->Month = months - 1;
546             TimeFields->Year = years + 1524;
547         } else {
548             TimeFields->Month = months - 13;
549             TimeFields->Year = years + 1525;
550         }
551         /* calculation of day of month is based on the wonderful
552          * sequence of INT( n * 30.6): it reproduces the 
553          * 31-30-31-30-31-31 month lengths exactly for small n's */
554         TimeFields->Day = yearday - (1959 * months) / 64 ;
555         return;
556 }
557
558 /******************************************************************************
559  *       RtlTimeFieldsToTime [NTDLL.@]
560  *
561  * Convert a TIME_FIELDS structure into a time.
562  *
563  * PARAMS
564  *   ftTimeFields [I] TIME_FIELDS structure to convert.
565  *   Time         [O] Destination for the converted time.
566  *
567  * RETURNS
568  *   Success: TRUE.
569  *   Failure: FALSE.
570  */
571 BOOLEAN WINAPI RtlTimeFieldsToTime(
572         PTIME_FIELDS tfTimeFields,
573         PLARGE_INTEGER Time)
574 {
575         int month, year, cleaps, day;
576
577         /* FIXME: normalize the TIME_FIELDS structure here */
578         /* No, native just returns 0 (error) if the fields are not */
579         if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
580                 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
581                 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
582                 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
583                 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
584                 tfTimeFields->Day < 1 ||
585                 tfTimeFields->Day > MonthLengths
586                     [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
587                     [ tfTimeFields->Month - 1] ||
588                 tfTimeFields->Year < 1601 )
589             return FALSE;
590
591         /* now calculate a day count from the date
592          * First start counting years from March. This way the leap days
593          * are added at the end of the year, not somewhere in the middle.
594          * Formula's become so much less complicate that way.
595          * To convert: add 12 to the month numbers of Jan and Feb, and 
596          * take 1 from the year */
597         if(tfTimeFields->Month < 3) {
598             month = tfTimeFields->Month + 13;
599             year = tfTimeFields->Year - 1;
600         } else {
601             month = tfTimeFields->Month + 1;
602             year = tfTimeFields->Year;
603         }
604         cleaps = (3 * (year / 100) + 3) / 4;   /* nr of "century leap years"*/
605         day =  (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
606                  (1959 * month) / 64 +         /* months * daypermonth */
607                  tfTimeFields->Day -          /* day of the month */
608                  584817 ;                      /* zero that on 1601-01-01 */
609         /* done */
610         
611         Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
612             tfTimeFields->Hour) * MINSPERHOUR +
613             tfTimeFields->Minute) * SECSPERMIN +
614             tfTimeFields->Second ) * 1000 +
615             tfTimeFields->Milliseconds ) * TICKSPERMSEC;
616
617         return TRUE;
618 }
619
620 /***********************************************************************
621  *       TIME_GetBias [internal]
622  *
623  * Helper function calculates delta local time from UTC. 
624  *
625  * PARAMS
626  *   utc [I] The current utc time.
627  *   pdaylight [I] Local daylight.
628  *
629  * RETURNS
630  *   The bias for the current timezone.
631  */
632 static int TIME_GetBias(time_t utc, int *pdaylight)
633 {
634     struct tm *ptm;
635     static time_t last_utc;
636     static int last_bias;
637     static int last_daylight;
638     int ret;
639
640     RtlEnterCriticalSection( &TIME_GetBias_section );
641     if(utc == last_utc)
642     {
643         *pdaylight = last_daylight;
644         ret = last_bias;        
645     } else
646     {
647         ptm = localtime(&utc);
648         *pdaylight = last_daylight =
649             ptm->tm_isdst; /* daylight for local timezone */
650         ptm = gmtime(&utc);
651         ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
652         last_utc = utc;
653         ret = last_bias = (int)(utc-mktime(ptm));
654     }
655     RtlLeaveCriticalSection( &TIME_GetBias_section );
656     return ret;
657 }
658
659 /******************************************************************************
660  *        RtlLocalTimeToSystemTime [NTDLL.@]
661  *
662  * Convert a local time into system time.
663  *
664  * PARAMS
665  *   LocalTime  [I] Local time to convert.
666  *   SystemTime [O] Destination for the converted time.
667  *
668  * RETURNS
669  *   Success: STATUS_SUCCESS.
670  *   Failure: An NTSTATUS error code indicating the problem.
671  */
672 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
673                                           PLARGE_INTEGER SystemTime)
674 {
675     time_t gmt;
676     int bias, daylight;
677
678     TRACE("(%p, %p)\n", LocalTime, SystemTime);
679
680     gmt = time(NULL);
681     bias = TIME_GetBias(gmt, &daylight);
682
683     SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)TICKSPERSEC;
684     return STATUS_SUCCESS;
685 }
686
687 /******************************************************************************
688  *       RtlSystemTimeToLocalTime [NTDLL.@]
689  *
690  * Convert a system time into a local time.
691  *
692  * PARAMS
693  *   SystemTime [I] System time to convert.
694  *   LocalTime  [O] Destination for the converted time.
695  *
696  * RETURNS
697  *   Success: STATUS_SUCCESS.
698  *   Failure: An NTSTATUS error code indicating the problem.
699  */
700 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
701                                           PLARGE_INTEGER LocalTime )
702 {
703     time_t gmt;
704     int bias, daylight;
705
706     TRACE("(%p, %p)\n", SystemTime, LocalTime);
707
708     gmt = time(NULL);
709     bias = TIME_GetBias(gmt, &daylight);
710
711     LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)TICKSPERSEC;
712     return STATUS_SUCCESS;
713 }
714
715 /******************************************************************************
716  *       RtlTimeToSecondsSince1970 [NTDLL.@]
717  *
718  * Convert a time into a count of seconds since 1970.
719  *
720  * PARAMS
721  *   Time    [I] Time to convert.
722  *   Seconds [O] Destination for the converted time.
723  *
724  * RETURNS
725  *   Success: TRUE.
726  *   Failure: FALSE, if the resulting value will not fit in a DWORD.
727  */
728 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
729 {
730     ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
731     tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
732     tmp -= SECS_1601_TO_1970;
733     if (tmp > 0xffffffff) return FALSE;
734     *Seconds = (DWORD)tmp;
735     return TRUE;
736 }
737
738 /******************************************************************************
739  *       RtlTimeToSecondsSince1980 [NTDLL.@]
740  *
741  * Convert a time into a count of seconds since 1980.
742  *
743  * PARAMS
744  *   Time    [I] Time to convert.
745  *   Seconds [O] Destination for the converted time.
746  *
747  * RETURNS
748  *   Success: TRUE.
749  *   Failure: FALSE, if the resulting value will not fit in a DWORD.
750  */
751 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
752 {
753     ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
754     tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
755     tmp -= SECS_1601_TO_1980;
756     if (tmp > 0xffffffff) return FALSE;
757     *Seconds = (DWORD)tmp;
758     return TRUE;
759 }
760
761 /******************************************************************************
762  *       RtlSecondsSince1970ToTime [NTDLL.@]
763  *
764  * Convert a count of seconds since 1970 to a time.
765  *
766  * PARAMS
767  *   Seconds [I] Time to convert.
768  *   Time    [O] Destination for the converted time.
769  *
770  * RETURNS
771  *   Nothing.
772  */
773 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
774 {
775     ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
776     Time->u.LowPart  = (DWORD)secs;
777     Time->u.HighPart = (DWORD)(secs >> 32);
778 }
779
780 /******************************************************************************
781  *       RtlSecondsSince1980ToTime [NTDLL.@]
782  *
783  * Convert a count of seconds since 1980 to a time.
784  *
785  * PARAMS
786  *   Seconds [I] Time to convert.
787  *   Time    [O] Destination for the converted time.
788  *
789  * RETURNS
790  *   Nothing.
791  */
792 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
793 {
794     ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
795     Time->u.LowPart  = (DWORD)secs;
796     Time->u.HighPart = (DWORD)(secs >> 32);
797 }
798
799 /******************************************************************************
800  *       RtlTimeToElapsedTimeFields [NTDLL.@]
801  *
802  * Convert a time to a count of elapsed seconds.
803  *
804  * PARAMS
805  *   Time       [I] Time to convert.
806  *   TimeFields [O] Destination for the converted time.
807  *
808  * RETURNS
809  *   Nothing.
810  */
811 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
812 {
813     LONGLONG time;
814     INT rem;
815
816     time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
817     TimeFields->Milliseconds = rem / TICKSPERMSEC;
818
819     /* time is now in seconds */
820     TimeFields->Year  = 0;
821     TimeFields->Month = 0;
822     TimeFields->Day   = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
823
824     /* rem is now the remaining seconds in the last day */
825     TimeFields->Second = rem % 60;
826     rem /= 60;
827     TimeFields->Minute = rem % 60;
828     TimeFields->Hour = rem / 60;
829 }
830
831 /***********************************************************************
832  *       NtQuerySystemTime [NTDLL.@]
833  *       ZwQuerySystemTime [NTDLL.@]
834  *
835  * Get the current system time.
836  *
837  * PARAMS
838  *   Time [O] Destination for the current system time.
839  *
840  * RETURNS
841  *   Success: STATUS_SUCCESS.
842  *   Failure: An NTSTATUS error code indicating the problem.
843  */
844 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
845 {
846     struct timeval now;
847
848     gettimeofday( &now, 0 );
849     Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
850     Time->QuadPart += now.tv_usec * 10;
851     return STATUS_SUCCESS;
852 }
853
854 /******************************************************************************
855  *  NtQueryPerformanceCounter   [NTDLL.@]
856  *
857  *  Note: Windows uses a timer clocked at a multiple of 1193182 Hz. There is a
858  *  good number of applications that crash when the returned frequency is either
859  *  lower or higher then what Windows gives. Also too high counter values are
860  *  reported to give problems.
861  */
862 NTSTATUS WINAPI NtQueryPerformanceCounter( PLARGE_INTEGER Counter, PLARGE_INTEGER Frequency )
863 {
864     struct timeval now;
865
866     if (!Counter) return STATUS_ACCESS_VIOLATION;
867     gettimeofday( &now, 0 );
868     /* convert a counter that increments at a rate of 1 MHz
869      * to one of 1.193182 MHz, with some care for arithmetic
870      * overflow ( will not overflow for 5000 years ) and
871      * good accuracy ( 105/88 = 1.19318182) */
872     Counter->QuadPart = (((now.tv_sec - server_start_time) * (ULONGLONG)1000000 + now.tv_usec) * 105) / 88;
873     if (Frequency) Frequency->QuadPart = 1193182;
874     return STATUS_SUCCESS;
875 }
876
877 /***********************************************************************
878  *        TIME_GetTZAsStr [internal]
879  *
880  * Helper function that returns the given timezone as a string.
881  *
882  * PARAMS
883  *   utc [I] The current utc time.
884  *   bias [I] The bias of the current timezone.
885  *   dst [I] ??
886  *
887  * RETURNS
888  *   Timezone name.
889  *
890  * NOTES:
891  *   This could be done with a hash table instead of merely iterating through a
892  *   table, however with the small amount of entries (60 or so) I didn't think
893  *   it was worth it.
894  */
895 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
896 {
897    char psTZName[7];
898    struct tm *ptm = localtime(&utc);
899    unsigned int i;
900
901    if (!strftime (psTZName, 7, "%Z", ptm))
902       return (NULL);
903
904    for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
905    {
906       if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
907            TZ_INFO[i].bias == bias &&
908            TZ_INFO[i].dst == dst
909          )
910             return TZ_INFO[i].psTZWindows;
911    }
912    FIXME("Can't match system time zone name \"%s\", bias=%d and dst=%d "
913          "to an entry in TZ_INFO. Please add appropriate entry to "
914          "TZ_INFO and submit as patch to wine-patches\n",psTZName,bias,dst);
915    return NULL;
916 }
917
918 /***  TIME_GetTimeZoneInfoFromReg: helper for GetTimeZoneInformation ***/
919
920
921 static int TIME_GetTimeZoneInfoFromReg(RTL_TIME_ZONE_INFORMATION *tzinfo)
922 {
923     BYTE buf[90];
924     KEY_VALUE_PARTIAL_INFORMATION * KpInfo =
925         (KEY_VALUE_PARTIAL_INFORMATION *) buf;
926     HANDLE hkey;
927     DWORD size;
928     OBJECT_ATTRIBUTES attr;
929     UNICODE_STRING nameW;
930
931     attr.Length = sizeof(attr);
932     attr.RootDirectory = 0;
933     attr.ObjectName = &nameW;
934     attr.Attributes = 0;
935     attr.SecurityDescriptor = NULL;
936     attr.SecurityQualityOfService = NULL;
937     RtlInitUnicodeString( &nameW, TZInformationKeyW);
938     if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr )) {
939
940 #define GTZIFR_N( valkey, tofield) \
941         RtlInitUnicodeString( &nameW, valkey );\
942         if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
943                     sizeof(buf), &size )) { \
944             if( size >= (sizeof((tofield)) + \
945                     offsetof(KEY_VALUE_PARTIAL_INFORMATION,Data))) { \
946                 memcpy(&(tofield), \
947                         KpInfo->Data, sizeof(tofield)); \
948             } \
949         }
950 #define GTZIFR_S( valkey, tofield) \
951         RtlInitUnicodeString( &nameW, valkey );\
952         if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, KpInfo,\
953                     sizeof(buf), &size )) { \
954             size_t len = (strlenW( (WCHAR*)KpInfo->Data ) + 1) * sizeof(WCHAR); \
955             if (len > sizeof(tofield)) len = sizeof(tofield); \
956             memcpy( tofield, KpInfo->Data, len ); \
957             tofield[(len/sizeof(WCHAR))-1] = 0; \
958         }
959
960         GTZIFR_N( TZStandardStartW,  tzinfo->StandardDate)
961         GTZIFR_N( TZDaylightStartW,  tzinfo->DaylightDate)
962         GTZIFR_N( TZBiasW,          tzinfo->Bias)
963         GTZIFR_N( TZStandardBiasW,  tzinfo->StandardBias)
964         GTZIFR_N( TZDaylightBiasW,  tzinfo->DaylightBias)
965         GTZIFR_S( TZStandardNameW, tzinfo->StandardName)
966         GTZIFR_S( TZDaylightNameW, tzinfo->DaylightName)
967
968 #undef GTZIFR_N
969 #undef GTZIFR_S
970         NtClose( hkey );
971         return 1;
972     }
973     return 0;
974 }
975
976 /***********************************************************************
977  *      RtlQueryTimeZoneInformation [NTDLL.@]
978  *
979  * Get information about the current timezone.
980  *
981  * PARAMS
982  *   tzinfo [O] Destination for the retrieved timezone info.
983  *
984  * RETURNS
985  *   Success: STATUS_SUCCESS.
986  *   Failure: An NTSTATUS error code indicating the problem.
987  */
988 NTSTATUS WINAPI RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION *tzinfo)
989 {
990     time_t gmt;
991     int bias, daylight;
992     const WCHAR *psTZ;
993
994     memset(tzinfo, 0, sizeof(RTL_TIME_ZONE_INFORMATION));
995
996     if( !TIME_GetTimeZoneInfoFromReg(tzinfo)) {
997
998         gmt = time(NULL);
999         bias = TIME_GetBias(gmt, &daylight);
1000
1001         tzinfo->Bias = -bias / 60;
1002         tzinfo->StandardBias = 0;
1003         tzinfo->DaylightBias = -60;
1004         tzinfo->StandardName[0]='\0';
1005         tzinfo->DaylightName[0]='\0';
1006         psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
1007         if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
1008         }
1009     return STATUS_SUCCESS;
1010 }
1011
1012 /***********************************************************************
1013  *       RtlSetTimeZoneInformation [NTDLL.@]
1014  *
1015  * Set the current time zone information.
1016  *
1017  * PARAMS
1018  *   tzinfo [I] Timezone information to set.
1019  *
1020  * RETURNS
1021  *   Success: STATUS_SUCCESS.
1022  *   Failure: An NTSTATUS error code indicating the problem.
1023  *
1024  * BUGS
1025  *   Uses the obsolete unix timezone structure and tz_dsttime member.
1026  */
1027 NTSTATUS WINAPI RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION *tzinfo )
1028 {
1029 #ifdef HAVE_SETTIMEOFDAY
1030     struct timezone tz;
1031
1032     tz.tz_minuteswest = tzinfo->Bias;
1033 #ifdef DST_NONE
1034     tz.tz_dsttime = DST_NONE;
1035 #else
1036     tz.tz_dsttime = 0;
1037 #endif
1038     if(!settimeofday(NULL, &tz))
1039         return STATUS_SUCCESS;
1040 #endif
1041     return STATUS_PRIVILEGE_NOT_HELD;
1042 }
1043
1044 /***********************************************************************
1045  *        NtSetSystemTime [NTDLL.@]
1046  *        ZwSetSystemTime [NTDLL.@]
1047  *
1048  * Set the system time.
1049  *
1050  * PARAMS
1051  *   NewTime [I] The time to set.
1052  *   OldTime [O] Optional destination for the previous system time.
1053  *
1054  * RETURNS
1055  *   Success: STATUS_SUCCESS.
1056  *   Failure: An NTSTATUS error code indicating the problem.
1057  */
1058 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
1059 {
1060     TIME_FIELDS tf;
1061     struct timeval tv;
1062     struct timezone tz;
1063     struct tm t;
1064     time_t sec, oldsec;
1065     int dst, bias;
1066     int err;
1067
1068     /* Return the old time if necessary */
1069     if(OldTime)
1070         NtQuerySystemTime(OldTime);
1071
1072     RtlTimeToTimeFields(NewTime, &tf);
1073
1074     /* call gettimeofday to get the current timezone */
1075     gettimeofday(&tv, &tz);
1076     oldsec = tv.tv_sec;
1077     /* get delta local time from utc */
1078     bias = TIME_GetBias(oldsec, &dst);
1079
1080     /* get the number of seconds */
1081     t.tm_sec = tf.Second;
1082     t.tm_min = tf.Minute;
1083     t.tm_hour = tf.Hour;
1084     t.tm_mday = tf.Day;
1085     t.tm_mon = tf.Month - 1;
1086     t.tm_year = tf.Year - 1900;
1087     t.tm_isdst = dst;
1088     sec = mktime (&t);
1089     /* correct for timezone and daylight */
1090     sec += bias;
1091
1092     /* set the new time */
1093     tv.tv_sec = sec;
1094     tv.tv_usec = tf.Milliseconds * 1000;
1095
1096     /* error and sanity check*/
1097     if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
1098         err = 2;
1099     } else {
1100 #ifdef HAVE_SETTIMEOFDAY
1101         err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
1102         if(err == 0)
1103             return STATUS_SUCCESS;
1104 #else
1105         err = 1;
1106 #endif
1107     }
1108
1109     ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
1110             tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
1111             (long)(sec-oldsec),
1112             err == -1 ? "No Permission"
1113                       : sec == (time_t)-1 ? "" : "is too large." );
1114
1115     if(err == 2)
1116         return STATUS_INVALID_PARAMETER;
1117     else if(err == -1)
1118         return STATUS_PRIVILEGE_NOT_HELD;
1119     else
1120         return STATUS_NOT_IMPLEMENTED;
1121 }