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