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