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