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