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