Documentation updates.
[wine] / dlls / ntdll / time.c
1 /*
2  * Nt time functions.
3  *
4  * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
5  * adapted to wine with special permissions of the author. This code is
6  * Copyright 2002 Rex Jolliff (rex@lvcablemodem.com)
7  *
8  * Copyright 1999 Juergen Schmied
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <time.h>
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
35 #endif
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winreg.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 CRITICAL_SECTION TIME_GetBias_section;
53 static CRITICAL_SECTION_DEBUG critsect_debug =
54 {
55     0, 0, &TIME_GetBias_section,
56     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
57       0, 0, { 0, (DWORD)(__FILE__ ": TIME_GetBias_section") }
58 };
59 static CRITICAL_SECTION TIME_GetBias_section = { &critsect_debug, -1, 0, 0, 0, 0 };
60
61
62 #define SETTIME_MAX_ADJUST 120
63
64 /* This structure is used to store strings that represent all of the time zones
65  * in the world. (This is used to help GetTimeZoneInformation)
66  */
67 struct tagTZ_INFO
68 {
69     const char *psTZFromUnix;
70     WCHAR psTZWindows[32];
71     int bias;
72     int dst;
73 };
74
75 static const struct tagTZ_INFO TZ_INFO[] =
76 {
77    {"MHT",
78     {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
79      'T','i','m','e','\0'}, -720, 0},
80    {"SST",
81     {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
82      'e','\0'}, 660, 0},
83    {"HST",
84     {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
85      'T','i','m','e','\0'}, 600, 0},
86    {"AKDT",
87     {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
88      'i','m','e','\0'}, 480, 1},
89    {"PDT",
90     {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
91      'i','m','e','\0'}, 420, 1},
92    {"MST",
93     {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
94      'r','d',' ','T','i','m','e','\0'}, 420, 0},
95    {"MDT",
96     {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
97      'T','i','m','e','\0'}, 360, 1},
98    {"CST",
99     {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
100      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
101    {"CDT",
102     {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
103      'i','m','e','\0'}, 300, 1},
104    {"COT",
105     {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
106      'd',' ','T','i','m','e','\0'}, 300, 0},
107    {"EDT",
108     {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
109      'i','m','e','\0'}, 240, 1},
110    {"EST",
111     {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
112      'd',' ','T','i','m','e','\0'}, 300, 0},
113    {"ADT",
114     {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
115      'T','i','m','e','\0'}, 180, 1},
116    {"VET",
117     {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
118      'd',' ','T','i','m','e','\0'}, 240, 0},
119    {"CLT",
120     {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
121      'd',' ','T','i','m','e','\0'}, 240, 0},
122    {"NDT",
123     {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
124      'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
125    {"BRT",
126     {'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S',
127      't','a','n','d','a','r','d',' ','T','i','m','e','\0'}, 180, 0},
128    {"ART",
129     {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
130      'd',' ','T','i','m','e','\0'}, 180, 0},
131    {"WGST",
132     {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
133      ' ','T','i','m','e','\0'}, 120, 1},
134    {"GST",
135     {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
136      'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
137    {"AZOST",
138     {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
139      'm','e','\0'}, 0, 1},
140    {"CVT",
141     {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
142      'd',' ','T','i','m','e','\0'}, 60, 0},
143    {"WET",
144     {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
145      ' ','T','i','m','e','\0'}, 0, 0},
146    {"BST",
147     {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
148     -60, 1},
149    {"GMT",
150     {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
151     0, 0},
152    {"CEST",
153     {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
154      'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
155    {"MET",
156     {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
157      'n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
158    {"MEST",
159     {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','D','a','y',
160      'l','i','g','h','t',' ','T','i','m','e','\0'}, -120, 1},
161    {"WAT",
162     {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
163      'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
164    {"EEST",
165     {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
166      ' ','T','i','m','e','\0'}, -180, 1},
167    {"EET",
168     {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
169      'e','\0'}, -120, 0},
170    {"CAT",
171     {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
172      'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
173    {"IST",
174     {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
175      'm','e','\0'}, -120, 0},
176    {"ADT",
177     {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
178      'm','e','\0'}, -240, 1},
179    {"AST",
180     {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
181      '\0'}, -180, 0},
182    {"MSD",
183     {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
184      'i','m','e','\0'}, -240, 1},
185    {"EAT",
186     {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
187      ' ','T','i','m','e','\0'}, -180, 0},
188    {"IRST",
189     {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
190      '\0'}, -270, 1},
191    {"GST",
192     {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
193      'i','m','e','\0'}, -240, 0},
194    {"AZST",
195     {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
196      'T','i','m','e','\0'}, -300, 1},
197    {"AFT",
198     {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
199      'r','d',' ','T','i','m','e','\0'}, -270, 0},
200    {"YEKST",
201     {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
202      'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
203    {"PKT",
204     {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
205      ' ','T','i','m','e','\0'}, -300, 0},
206    {"IST",
207     {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
208      'e','\0'}, -330, 0},
209    {"NPT",
210     {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
211      'e','\0'}, -345, 0},
212    {"ALMST",
213     {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
214      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
215    {"BDT",
216     {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
217      'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
218    {"LKT",
219     {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
220      ' ','T','i','m','e','\0'}, -360, 0},
221    {"MMT",
222     {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
223      'i','m','e','\0'}, -390, 0},
224    {"ICT",
225     {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
226      'i','m','e','\0'}, -420, 0},
227    {"KRAST",
228     {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
229      'd',' ','T','i','m','e','\0'}, -480, 1},
230    {"CST",
231     {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
232      'e','\0'}, -480, 0},
233    {"IRKST",
234     {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
235      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
236    {"SGT",
237     {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
238      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
239    {"WST",
240     {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
241      'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
242    {"JST",
243     {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
244      'e','\0'}, -540, 0},
245    {"KST",
246     {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
247      'e','\0'}, -540, 0},
248    {"YAKST",
249     {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
250      'i','m','e','\0'}, -600, 1},
251    {"CST",
252     {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
253      'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
254    {"EST",
255     {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
256      'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
257    {"GST",
258     {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
259      'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
260    {"VLAST",
261     {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
262      'r','d',' ','T','i','m','e','\0'}, -660, 1},
263    {"MAGST",
264     {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
265      'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
266    {"NZST",
267     {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
268      'r','d',' ','T','i','m','e','\0'}, -720, 0},
269    {"FJT",
270     {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
271      '\0'}, -720, 0},
272    {"TOT",
273     {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
274      'e','\0'}, -780, 0}
275 };
276
277 #define TICKSPERSEC        10000000
278 #define TICKSPERMSEC       10000
279 #define SECSPERDAY         86400
280 #define SECSPERHOUR        3600
281 #define SECSPERMIN         60
282 #define MINSPERHOUR        60
283 #define HOURSPERDAY        24
284 #define EPOCHWEEKDAY       1  /* Jan 1, 1601 was Monday */
285 #define DAYSPERWEEK        7
286 #define EPOCHYEAR          1601
287 #define DAYSPERNORMALYEAR  365
288 #define DAYSPERLEAPYEAR    366
289 #define MONSPERYEAR        12
290 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
291 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
292 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
293
294 /* 1601 to 1970 is 369 years plus 89 leap days */
295 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
296 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
297 /* 1601 to 1980 is 379 years plus 91 leap days */
298 #define SECS_1601_TO_1980  ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
299 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
300 /* max ticks that can be represented as Unix time */
301 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
302
303
304 static const int MonthLengths[2][MONSPERYEAR] =
305 {
306         { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
307         { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
308 };
309
310 static const int YearDays[2][MONSPERYEAR+1] =
311 {
312         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
313         { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
314 };
315
316 static inline int IsLeapYear(int Year)
317 {
318         return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
319 }
320
321 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
322 {
323         *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
324         *CarryField = (CSHORT) (*CarryField + 1);
325 }
326
327 /***********************************************************************
328  *              NTDLL_get_server_timeout
329  *
330  * Convert a NTDLL timeout into a timeval struct to send to the server.
331  */
332 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
333 {
334     UINT remainder;
335
336     if (!timeout)  /* infinite timeout */
337     {
338         when->sec = when->usec = 0;
339     }
340     else if (timeout->QuadPart <= 0)  /* relative timeout */
341     {
342         struct timeval tv;
343
344         if (-timeout->QuadPart > (LONGLONG)INT_MAX * TICKSPERSEC)
345             when->sec = when->usec = INT_MAX;
346         else
347         {
348             ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
349             gettimeofday( &tv, 0 );
350             when->sec = tv.tv_sec + sec;
351             if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
352             {
353                 when->usec -= 1000000;
354                 when->sec++;
355             }
356             if (when->sec < tv.tv_sec)  /* overflow */
357                 when->sec = when->usec = INT_MAX;
358         }
359     }
360     else  /* absolute time */
361     {
362         if (timeout->QuadPart < TICKS_1601_TO_1970)
363             when->sec = when->usec = 0;
364         else if (timeout->QuadPart > TICKS_1601_TO_UNIX_MAX)
365             when->sec = when->usec = INT_MAX;
366         else
367         {
368             when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
369                                                    TICKSPERSEC, &remainder );
370             when->usec = remainder / 10;
371         }
372     }
373 }
374
375
376 /******************************************************************************
377  *       RtlTimeToTimeFields [NTDLL.@]
378  *
379  * Convert a time into a TIME_FIELDS structure.
380  *
381  * PARAMS
382  *   liTime     [I] Time to convert.
383  *   TimeFields [O] Destination for the converted time.
384  *
385  * RETURNS
386  *   Nothing.
387  */
388 VOID WINAPI RtlTimeToTimeFields(
389         const LARGE_INTEGER *liTime,
390         PTIME_FIELDS TimeFields)
391 {
392         int SecondsInDay, DeltaYear;
393         int LeapYear, CurMonth;
394         long int Days;
395         LONGLONG Time = liTime->QuadPart;
396
397         /* Extract millisecond from time and convert time into seconds */
398         TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
399         Time = Time / TICKSPERSEC;
400
401         /* The native version of RtlTimeToTimeFields does not take leap seconds
402          * into account */
403
404         /* Split the time into days and seconds within the day */
405         Days = Time / SECSPERDAY;
406         SecondsInDay = Time % SECSPERDAY;
407
408         /* compute time of day */
409         TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
410         SecondsInDay = SecondsInDay % SECSPERHOUR;
411         TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
412         TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
413
414         /* compute day of week */
415         TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
416
417         /* compute year */
418         /* FIXME: handle calendar modifications */
419         TimeFields->Year = EPOCHYEAR;
420         DeltaYear = Days / DAYSPERQUADRICENTENNIUM;
421         TimeFields->Year += DeltaYear * 400;
422         Days -= DeltaYear * DAYSPERQUADRICENTENNIUM;
423         DeltaYear = Days / DAYSPERNORMALCENTURY;
424         TimeFields->Year += DeltaYear * 100;
425         Days -= DeltaYear * DAYSPERNORMALCENTURY;
426         DeltaYear = Days / DAYSPERNORMALQUADRENNIUM;
427         TimeFields->Year += DeltaYear * 4;
428         Days -= DeltaYear * DAYSPERNORMALQUADRENNIUM;
429         DeltaYear = Days / DAYSPERNORMALYEAR;
430         TimeFields->Year += DeltaYear;
431         Days -= DeltaYear * DAYSPERNORMALYEAR;
432
433         LeapYear = IsLeapYear(TimeFields->Year);
434
435         /* Compute month of year */
436         CurMonth = 1;
437         while (Days >= YearDays[LeapYear][CurMonth]) CurMonth++;
438         TimeFields->Day = Days - YearDays[LeapYear][CurMonth-1] + 1;
439         TimeFields->Month = CurMonth;
440 }
441
442 /******************************************************************************
443  *       RtlTimeFieldsToTime [NTDLL.@]
444  *
445  * Convert a TIME_FIELDS structure into a time.
446  *
447  * PARAMS
448  *   ftTimeFields [I] TIME_FIELDS structure to convert.
449  *   Time         [O] Destination for the converted time.
450  *
451  * RETURNS
452  *   Success: TRUE.
453  *   Failure: FALSE.
454  */
455 BOOLEAN WINAPI RtlTimeFieldsToTime(
456         PTIME_FIELDS tfTimeFields,
457         PLARGE_INTEGER Time)
458 {
459         int CurYear, DeltaYear;
460         LONGLONG rcTime;
461         TIME_FIELDS TimeFields = *tfTimeFields;
462
463         rcTime = 0;
464
465         /* FIXME: normalize the TIME_FIELDS structure here */
466         while (TimeFields.Second >= SECSPERMIN)
467         { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
468         }
469         while (TimeFields.Minute >= MINSPERHOUR)
470         { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
471         }
472         while (TimeFields.Hour >= HOURSPERDAY)
473         { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
474         }
475         while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
476         { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month,
477                               MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1]);
478         }
479         while (TimeFields.Month > MONSPERYEAR)
480         { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
481         }
482
483         /* FIXME: handle calendar corrections here */
484         CurYear = TimeFields.Year - EPOCHYEAR;
485         DeltaYear = CurYear / 400;
486         CurYear -= DeltaYear * 400;
487         rcTime += DeltaYear * DAYSPERQUADRICENTENNIUM;
488         DeltaYear = CurYear / 100;
489         CurYear -= DeltaYear * 100;
490         rcTime += DeltaYear * DAYSPERNORMALCENTURY;
491         DeltaYear = CurYear / 4;
492         CurYear -= DeltaYear * 4;
493         rcTime += DeltaYear * DAYSPERNORMALQUADRENNIUM;
494         rcTime += CurYear * DAYSPERNORMALYEAR;
495         rcTime += YearDays[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1];
496         rcTime += TimeFields.Day - 1;
497         rcTime *= SECSPERDAY;
498         rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
499         rcTime *= TICKSPERSEC;
500         rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
501         Time->QuadPart = rcTime;
502
503         return TRUE;
504 }
505
506 /***********************************************************************
507  *       TIME_GetBias [internal]
508  *
509  * Helper function calculates delta local time from UTC. 
510  *
511  * PARAMS
512  *   utc [I] The current utc time.
513  *   pdaylight [I] Local daylight.
514  *
515  * RETURNS
516  *   The bias for the current timezone.
517  */
518 static int TIME_GetBias(time_t utc, int *pdaylight)
519 {
520     struct tm *ptm;
521     static time_t last_utc;
522     static int last_bias;
523     static int last_daylight;
524     int ret;
525
526     RtlEnterCriticalSection( &TIME_GetBias_section );
527     if(utc == last_utc)
528     {
529         *pdaylight = last_daylight;
530         ret = last_bias;        
531     } else
532     {
533         ptm = localtime(&utc);
534         *pdaylight = last_daylight =
535             ptm->tm_isdst; /* daylight for local timezone */
536         ptm = gmtime(&utc);
537         ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
538         last_utc = utc;
539         ret = last_bias = (int)(utc-mktime(ptm));
540     }
541     RtlLeaveCriticalSection( &TIME_GetBias_section );
542     return ret;
543 }
544
545 /******************************************************************************
546  *        RtlLocalTimeToSystemTime [NTDLL.@]
547  *
548  * Convert a local time into system time.
549  *
550  * PARAMS
551  *   LocalTime  [I] Local time to convert.
552  *   SystemTime [O] Destination for the converted time.
553  *
554  * RETURNS
555  *   Success: STATUS_SUCCESS.
556  *   Failure: An NTSTATUS error code indicating the problem.
557  */
558 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
559                                           PLARGE_INTEGER SystemTime)
560 {
561     time_t gmt;
562     int bias, daylight;
563
564     TRACE("(%p, %p)\n", LocalTime, SystemTime);
565
566     gmt = time(NULL);
567     bias = TIME_GetBias(gmt, &daylight);
568
569     SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)10000000;
570     return STATUS_SUCCESS;
571 }
572
573 /******************************************************************************
574  *       RtlSystemTimeToLocalTime [NTDLL.@]
575  *
576  * Convert a system time into a local time.
577  *
578  * PARAMS
579  *   SystemTime [I] System time to convert.
580  *   LocalTime  [O] Destination for the converted time.
581  *
582  * RETURNS
583  *   Success: STATUS_SUCCESS.
584  *   Failure: An NTSTATUS error code indicating the problem.
585  */
586 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
587                                           PLARGE_INTEGER LocalTime )
588 {
589     time_t gmt;
590     int bias, daylight;
591
592     TRACE("(%p, %p)\n", SystemTime, LocalTime);
593
594     gmt = time(NULL);
595     bias = TIME_GetBias(gmt, &daylight);
596
597     LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)10000000;
598     return STATUS_SUCCESS;
599 }
600
601 /******************************************************************************
602  *       RtlTimeToSecondsSince1970 [NTDLL.@]
603  *
604  * Convert a time into a count of seconds since 1970.
605  *
606  * PARAMS
607  *   Time    [I] Time to convert.
608  *   Seconds [O] Destination for the converted time.
609  *
610  * RETURNS
611  *   Success: TRUE.
612  *   Failure: FALSE, if the resulting value will not fit in a DWORD.
613  */
614 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
615 {
616     ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
617     tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
618     tmp -= SECS_1601_TO_1970;
619     if (tmp > 0xffffffff) return FALSE;
620     *Seconds = (DWORD)tmp;
621     return TRUE;
622 }
623
624 /******************************************************************************
625  *       RtlTimeToSecondsSince1980 [NTDLL.@]
626  *
627  * Convert a time into a count of seconds since 1980.
628  *
629  * PARAMS
630  *   Time    [I] Time to convert.
631  *   Seconds [O] Destination for the converted time.
632  *
633  * RETURNS
634  *   Success: TRUE.
635  *   Failure: FALSE, if the resulting value will not fit in a DWORD.
636  */
637 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
638 {
639     ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
640     tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
641     tmp -= SECS_1601_TO_1980;
642     if (tmp > 0xffffffff) return FALSE;
643     *Seconds = (DWORD)tmp;
644     return TRUE;
645 }
646
647 /******************************************************************************
648  *       RtlSecondsSince1970ToTime [NTDLL.@]
649  *
650  * Convert a count of seconds since 1970 to a time.
651  *
652  * PARAMS
653  *   Seconds [I] Time to convert.
654  *   Time    [O] Destination for the converted time.
655  *
656  * RETURNS
657  *   Nothing.
658  */
659 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
660 {
661     ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
662     Time->u.LowPart  = (DWORD)secs;
663     Time->u.HighPart = (DWORD)(secs >> 32);
664 }
665
666 /******************************************************************************
667  *       RtlSecondsSince1980ToTime [NTDLL.@]
668  *
669  * Convert a count of seconds since 1980 to a time.
670  *
671  * PARAMS
672  *   Seconds [I] Time to convert.
673  *   Time    [O] Destination for the converted time.
674  *
675  * RETURNS
676  *   Nothing.
677  */
678 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
679 {
680     ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
681     Time->u.LowPart  = (DWORD)secs;
682     Time->u.HighPart = (DWORD)(secs >> 32);
683 }
684
685 /******************************************************************************
686  *       RtlTimeToElapsedTimeFields [NTDLL.@]
687  *
688  * Convert a time to a count of elapsed seconds.
689  *
690  * PARAMS
691  *   Time       [I] Time to convert.
692  *   TimeFields [O] Destination for the converted time.
693  *
694  * RETURNS
695  *   Nothing.
696  */
697 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
698 {
699     LONGLONG time;
700     UINT rem;
701
702     time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
703     TimeFields->Milliseconds = rem / TICKSPERMSEC;
704
705     /* time is now in seconds */
706     TimeFields->Year  = 0;
707     TimeFields->Month = 0;
708     TimeFields->Day   = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
709
710     /* rem is now the remaining seconds in the last day */
711     TimeFields->Second = rem % 60;
712     rem /= 60;
713     TimeFields->Minute = rem % 60;
714     TimeFields->Hour = rem / 60;
715 }
716
717 /***********************************************************************
718  *       NtQuerySystemTime [NTDLL.@]
719  *       ZwQuerySystemTime [NTDLL.@]
720  *
721  * Get the current system time.
722  *
723  * PARAMS
724  *   Time [O] Destination for the current system time.
725  *
726  * RETURNS
727  *   Success: STATUS_SUCCESS.
728  *   Failure: An NTSTATUS error code indicating the problem.
729  */
730 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
731 {
732     struct timeval now;
733
734     gettimeofday( &now, 0 );
735     Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
736     Time->QuadPart += now.tv_usec * 10;
737     return STATUS_SUCCESS;
738 }
739
740 /***********************************************************************
741  *        TIME_GetTZAsStr [internal]
742  *
743  * Helper function that returns the given timezone as a string.
744  *
745  * PARAMS
746  *   utc [I] The current utc time.
747  *   bias [I] The bias of the current timezone.
748  *   dst [I] ??
749  *
750  * RETURNS
751  *   Timezone name.
752  *
753  * NOTES:
754  *   This could be done with a hash table instead of merely iterating through a
755  *   table, however with the small amount of entries (60 or so) I didn't think
756  *   it was worth it.
757  */
758 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
759 {
760    char psTZName[7];
761    struct tm *ptm = localtime(&utc);
762    unsigned int i;
763
764    if (!strftime (psTZName, 7, "%Z", ptm))
765       return (NULL);
766
767    for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
768    {
769       if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
770            TZ_INFO[i].bias == bias &&
771            TZ_INFO[i].dst == dst
772          )
773             return TZ_INFO[i].psTZWindows;
774    }
775    FIXME("Can't match system time zone name \"%s\" to an entry in TZ_INFO\n",psTZName);
776    FIXME(" Please add appropriate entry to TZ_INFO and submit as patch to wine-patches\n");
777    return NULL;
778 }
779
780 /***********************************************************************
781  *      RtlQueryTimeZoneInformation [NTDLL.@]
782  *
783  * Get information about the current timezone.
784  *
785  * PARAMS
786  *   tzinfo [O] Destination for the retrieved timezone info.
787  *
788  * RETURNS
789  *   Success: STATUS_SUCCESS.
790  *   Failure: An NTSTATUS error code indicating the problem.
791  */
792 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
793 {
794     time_t gmt;
795     int bias, daylight;
796     const WCHAR *psTZ;
797
798     memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
799
800     gmt = time(NULL);
801     bias = TIME_GetBias(gmt, &daylight);
802
803     tzinfo->Bias = -bias / 60;
804     tzinfo->StandardBias = 0;
805     tzinfo->DaylightBias = -60;
806     psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
807     if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
808     return STATUS_SUCCESS;
809 }
810
811 /***********************************************************************
812  *       RtlSetTimeZoneInformation [NTDLL.@]
813  *
814  * Set the current time zone information.
815  *
816  * PARAMS
817  *   tzinfo [I] Timezone information to set.
818  *
819  * RETURNS
820  *   Success: STATUS_SUCCESS.
821  *   Failure: An NTSTATUS error code indicating the problem.
822  *
823  * BUGS
824  *   Uses the obsolete unix timezone structure and tz_dsttime member.
825  */
826 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
827 {
828 #ifdef HAVE_SETTIMEOFDAY
829     struct timezone tz;
830
831     tz.tz_minuteswest = tzinfo->Bias;
832 #ifdef DST_NONE
833     tz.tz_dsttime = DST_NONE;
834 #else
835     tz.tz_dsttime = 0;
836 #endif
837     if(!settimeofday(NULL, &tz))
838         return STATUS_SUCCESS;
839 #endif
840     return STATUS_PRIVILEGE_NOT_HELD;
841 }
842
843 /***********************************************************************
844  *        NtSetSystemTime [NTDLL.@]
845  *        ZwSetSystemTime [NTDLL.@]
846  *
847  * Set the system time.
848  *
849  * PARAMS
850  *   NewTime [I] The time to set.
851  *   OldTime [O] Optional destination for the previous system time.
852  *
853  * RETURNS
854  *   Success: STATUS_SUCCESS.
855  *   Failure: An NTSTATUS error code indicating the problem.
856  */
857 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
858 {
859     TIME_FIELDS tf;
860     struct timeval tv;
861     struct timezone tz;
862     struct tm t;
863     time_t sec, oldsec;
864     int dst, bias;
865     int err;
866
867     /* Return the old time if necessary */
868     if(OldTime)
869         NtQuerySystemTime(OldTime);
870
871     RtlTimeToTimeFields(NewTime, &tf);
872
873     /* call gettimeofday to get the current timezone */
874     gettimeofday(&tv, &tz);
875     oldsec = tv.tv_sec;
876     /* get delta local time from utc */
877     bias = TIME_GetBias(oldsec, &dst);
878
879     /* get the number of seconds */
880     t.tm_sec = tf.Second;
881     t.tm_min = tf.Minute;
882     t.tm_hour = tf.Hour;
883     t.tm_mday = tf.Day;
884     t.tm_mon = tf.Month - 1;
885     t.tm_year = tf.Year - 1900;
886     t.tm_isdst = dst;
887     sec = mktime (&t);
888     /* correct for timezone and daylight */
889     sec += bias;
890
891     /* set the new time */
892     tv.tv_sec = sec;
893     tv.tv_usec = tf.Milliseconds * 1000;
894
895     /* error and sanity check*/
896     if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
897         err = 2;
898     } else {
899 #ifdef HAVE_SETTIMEOFDAY
900         err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
901         if(err == 0)
902             return STATUS_SUCCESS;
903 #else
904         err = 1;
905 #endif
906     }
907
908     ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
909             tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
910             (long)(sec-oldsec),
911             err == -1 ? "No Permission"
912                       : sec == (time_t)-1 ? "" : "is too large." );
913
914     if(err == 2)
915         return STATUS_INVALID_PARAMETER;
916     else if(err == -1)
917         return STATUS_PRIVILEGE_NOT_HELD;
918     else
919         return STATUS_NOT_IMPLEMENTED;
920 }