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